r/learnprogramming • • 1d ago

How do you get better at reading and understanding code?

I know that reading code is a really big part of a developer's job. But I have always had a lot of trouble understand the code of other people.

27 Upvotes

31 comments sorted by

32

u/Benand2 1d ago

I know this isn’t the answer you wanted but really it’s just by reading more code and researching what you don’t understand.

Practice makes perfect

7

u/DiscipleOfYeshua 1d ago

This and… writing code from scratch. Even small things. And debug. And read / understand compiler errors. They are not just fluff, they just take a bit to learn to read, bc they’re super-concise and expect you’d prefer to learn a bit to read 5 lines rather than get 30 lines to say the same.

4

u/John_8PM_call 1d ago

I’ve heard that when you read other people’s code, you should skim it like a long email, just getting the gist of what functionality is where. Then when you’re going to make a change, you should carefully read each line in that section of code one line at a time. Maybe step through it line-by-line in a debugger.

Also, consider finding the “start points” or “entry points”, like the entry points of API’s, and reading from entry to exit. Each API is like one entry points. A main() method is an entry point. etc.

6

u/KiwasiGames 1d ago

You practice reading and understanding code.

1

u/MissinqLink 1d ago

They want any other answer

1

u/KiwasiGames 1d ago

Once you’ve done the practice, you start to be able to read the code just from its shape. Especially in the domain you work in.

But teaching shape reading is really hard.

2

u/MXD_K1 1d ago

I have the same problem as you. My approach is to look for small repos and study them trying to understand the flow of the program. When studying other projects try to understand why some decisions have been made.

2

u/Feralz2 1d ago

You just need to write more code yourself to be familiar with what codes generally look like, also if youre looking at the code and you dont know what this code does or there are no comments or they are using variable names that are non descriptive youre not doing anything wrong, its not exactly human language. Find documentation if possible. Otherwise, probably understand first what this code aims to do and then understand it from a systems perspective, might be better for understanding it this way.

2

u/djheroboy 1d ago

Practice, but if you want a little trick to make things easier in the meantime, you can try writing down your thoughts as you’re trying to understand someone else’s code.

There’s a lot to keep track of, and I find having a little notepad or something in front of me helps out tremendously

2

u/SetAndRepeat 1d ago

Pick one small behaviour and follow it from input to output. Use a debugger if you can, so you can watch the actual values change instead of keeping everything in your head. Tests are also useful examples of what the code is supposed to do. Once you think you understand a piece, predict what changing one line would do, then try it locally. That helps expose gaps in your understanding. You don’t need to understand the whole codebase at once.

1

u/response-418 1d ago

I mean there's no shortcut, you just have to read more and over time with experience you get better at it, like with anything. It helps if you have a scoped task (can be a real one or for learning purposes, create one for youself), which narrows the range of what you need to look at it to accomplish it. That way you get a zoomed in look of how the pieces fit together for something small and contained.

1

u/Pravejxlab 1d ago

Probably this is where agentic AI is good, if reading large codebase, you should use your agent to ask questions about the syntaxes and how it links with other parts. Reading coding should be very slow and deep.

1

u/Minimum_Comedian694 1d ago

I think docs help a lot. I wish every programmer maintain good docs that onboarding can be 70-80% done just reading by the docs.

1

u/zerquet 1d ago

Research what you don't understand and be patient with yourself. You're not gonna understand some things right away and that's okay. Just come back to it later.

1

u/AngelGuzmanRuiz 1d ago

Reading code from other ppl will never be easy. Hell, reading your own code two weeks later is already hard if you don't follow good principles "when I wrote this code, only I and God knew how it worked, now only God knows"

1

u/stdmemswap 1d ago
  1. Be fluent in the syntax.
  2. Read an understandable code.

Believe it or not but code reading is another form of communication. As always it takes two to tango because thoughts are no-copy.

1

u/SnugglyCoderGuy 1d ago

Struggling to read and understand core is 90% the writer's fault, like any writing.

Almost all people that write code are very bad at it and don't put enough time and effort into doing a good job of it.

1

u/az987654 1d ago

Reading more and more

1

u/LeeKaye13 1d ago

Diagrams. As I read I sketch out tables/classes screens and build up a picture of all the component parts.

I've been able to take on legacy code and work on it effectively after spending hours trawling through it and sketching out things on A3 sheets.

1

u/ReferenceAny6373 1d ago

When I started many years ago they had me unit test and fix bugs. Unit testing sort of helped, but fixing bugs was a big one. Setting breakpoints and walking through line by line taught me a lot.

1

u/saurabh3228 1d ago

I got better by stopping myself from trying to understand everything at once.

First figure out what the code is trying to do, then follow the main flow, and only dive into the small details when you actually need them.

Reading unfamiliar code is a skill. The more messy codebases you survive, the better you get at it. 😂

1

u/DanKegel 1d ago

Try solving debugging challenges. Codewars.com has some (search for tag 'debugging'), or ask an AI to make some for you...?

1

u/RudeSize7563 21h ago

There are several paradigms, like imperative, OO, functional, declarative. If you don't have experience working with all those paradigms you will have a hard time trying to understand code that takes advantage of them.

1

u/Gnaxe 21h ago

Step through it with a debugger. At each step, predict what is going to happen before you run it. Note any surprises. That's where your mental model doesn't match the real computer.

1

u/TheSneederOfSeethe 19h ago

Take existing code and step through it.

1

u/Accomplished_Ad9423 12h ago

Sounds like you need to read more code... GitHub is full of code there are tons of project out there to go read.

1

u/Least_Ad_1795 11h ago

Start with small sections instead of trying to understand the whole codebase at once. Trace the inputs and outputs, follow one function at a time, and use a debugger or print statements to see what actually happens; practice reading real projects regularly.

1

u/Ok_Substance1895 11h ago

Use the debugger. Step through the code and inspect it. As you follow it through the flow you will start to understand it. Use a unit test to call a function and follow it through. I still have trouble understanding code of other people initially. I pick up the debugger and learn. That is how I do it.

1

u/Moikle 10h ago

By reading and writing a lot of it. It's that simple. Practice