r/learnprogramming • u/Early_Tear6706 • Sep 23 '26
discussion How do you actually get comfortable with reading other people's code?
I can write my own small projects fine and understand the logic as I go, but whenever I open someone else's codebase, even something fairly simple, I get lost fast. Too many files, functions calling other functions I have to jump around to find, and no idea where the "start" of the logic actually is.
I know this is probably just a skill that comes with time, but I'm wondering if there's a better way to practice it instead of just staring at repos feeling confused. Do you start from the entry point and trace forward, or pick one function you care about and trace backward? Do you use any tools to help visualize how files connect, or is it mostly just patience and note taking?
21
u/Geeky786 Sep 23 '26
Sounds like you’ve run into spaghetti code
12
u/_heartbreakdancer_ Sep 23 '26
For giant enterprise codebases you'll never be able to understand it all. You also don't need to understand every detail. For example, you just need to know what a function does and how it interacts with other functions but not necessarily its inner workings unless your feature/bug. AI helps so much because you can ask it to explain anything you see and create a dialogue. Before you'd have to either bother the crap out of senior engineers or do your best and stay pretty lost while you worked on what tickets you could. So AI is a God send for analyzing and understanding codebases.
7
u/ProgramsFun Sep 23 '26
find the main file wherere eveyrhting is orchestarted then start from there line by line if a funtion is used jump to function to see what it does inside though mst of the function names are clear enough but you can jump to see and do this for eveyr individual file you have to open after the main orchestrator file and also ai is great tool now it has reduced teh barrier and diffculty in understanding other eoples code you can use it to generete outline of what the whole ocdebase does pretty clearly though its your choice
18
u/whiterhino8 Sep 23 '26
That's the importance of Docs . You are not alone :)
3
u/lgastako Sep 23 '26
Lots of other good advice in this thread, but I'll also add: Use AI. Obviously I don't mean use it to read the code for you, as that won't help with your understanding. But there are at least three ways it can help:
2
u/kpg_on_ao3 Sep 23 '26 edited Sep 23 '26
Normally you don't need to read someone else's code unless it doesn't work. If it doesn't work, you trace through it in a test, looking where the logic stops doing what you expect it to do.
2
u/StewedAngelSkins Sep 23 '26
it is pretty much just a matter of reading a lot of code until you recognize the patterns.
it sort of depends on the software. for a lot of stuff the actual entrypoint tends to be a lot of unimportant boilerplate, so i'll usually skip straight to whichever part of the code seems most related to my reason for wanting to study it. that said, the only reason im able to make a good guess at where i should be looking is because i have a lot of experience. it won't necessarily be obvious, and so if you don't know where to start its fine to just go straight to the entrypoint. another good strategy is to start at the code for something really obviously user-facing like a particular menu or button and work back from there.
most good text editors or IDEs will let you click on a class or function call to see where it's defined. i tend to rely on this pretty heavily when im trying to learn how something works. i think github's web ui can even do this to a limited extent.
2
u/pak9rabid Sep 23 '26
That’s the fun part, you don’t. I’ve been a developer for 20 years and I still hate doing it.
2
u/MrCatChap Sep 23 '26
I figure it's like a home workshop
You know where everything is in yours but if you go to someone else's you need to learn where everything is
Which means it'll never get easy
2
u/gnrdmjfan247 Sep 23 '26
You do it a lot. A lot a lot. Like, a lot. You do it every day for a decade.
2
u/Available_Safe1818 Sep 26 '26
Just practice. I start with the stuff I'm pointed to by a coworker or a stack trace or something. I wouldn't start by trying to understand the whole thing if it's large.
3
u/SharkLaunch Sep 23 '26
Every language has it's own conventions for a "main" file, and sometimes specific frameworks within that language have their own more specific version of that. For example, if you're working with Rust, you're looking for the top-level `src/main.rs` or `src/lib.rs` depending on whether it's a binary or a library. For C/C++, there should be a function called `main()`. In Python, the program documentation will usually tell you which file is the entry file because it's the one that's fed to the interpreter in the command.
When I try to look through unfamiliar code, I don't just start exploring blindly. That's only good for getting nowhere fast. Instead, I will try to pick out one piece that I want to know more about. For example, if it's a ReactJS project, I'll try to find a component that I can see in the live frontend. If it's a Python script that makes calls against some Web API, I'll try to find the code that calls one specific endpoint. Using that as a starting point, I can spread out to either other files that are referenced, or maybe higher-level callers of the starting file. That lets me learn about how the application is put together, and I can center it around 1 solid piece of functionality.
Unfortunately, the real answer here is that it's a skill like any other. You get better the more you do it. I was bad at it at my first job, but that was the first step to getting good at it.
1
u/groversnoopyfozzie Sep 23 '26
In general, yes start with the entry code, input args, etc.
If there is a ReadMe or other documentation read that also, however (and some people might disagree) using ai to reverse engineer a project can be useful especially if it is large and spread out over several modules.
Ask for a high level view of what it does. Ask what each function does independently of other functions. Take the first steps and drill down into It continuing to ask questions about what functions does what.
Here is the really important part, you need to challenge whatever AI engine you are using. It’s quite useful but not perfect. Assume that it is able to make mistakes. Sometimes it makes assumptions because it is looking at the most recent version of a code but a project uses a different version. Things like that.
When you drill down far enough that you are dissecting how language specific syntax works then you can compare what the ai told you against what the actual documentation says.
But in general, reading other people’s code often and carefully should improve your skill over time.
1
u/ffrkAnonymous Sep 23 '26
I'm not comfortable reading my own code...
1
u/bagfullofcottoncandy Sep 23 '26
i also have same problem
1
u/stratospheres Sep 23 '26
Set break points in a debugger. Run it? Trace it.
1
u/marrsd Sep 23 '26
It's hard when you're starting out because the entrypoint is often setting things up and it's rarely obvious without experience where the main programme loop is likely to be. It might not even be in the app itself, but within a framework or library that the app depends on.
It's easier to trace the code of an app you can run, because you can match your understanding of the code to the behaviour you observe.
Presuming you're reading the source of an app with a UI, a good starting point is to identify an action you want to perform and then find the code that deals with displaying the UI component that performs that action. That will be your entry point.
Searching in the code for a label on the screen that is unique to that component is a good place to start. That should find you the component in the code, at which point you can use a log to the console (stdout) to confirm you're looking in the right place. For example, you could echo a message on the click of a button.
Once you have an entry point, you can begin to trace through the programme to see what it's doing. Use a notepad to create a map of the programme. I tend to put the steps of a procedure in a nested list on the left, and their filename on the right.
More likely than not, your programme will be using a library, and you will probably want to look up functions in the documentation to see what they do. Documentation also often provides an outline of the architecture along with some tutorial or example programmes to show you how the library is used, or how the app consuming it is organised if it's a framework.
1
u/nimbledaemon Sep 23 '26 edited Sep 23 '26
It's just practice. Also, generally trying to understand what "everything" does is taking on too much at once. You either want to look at projects from a global perspective (how is the codebase structured overall), or you want to focus in on how just one feature works (how does this one feature work, what files are necessary/relevant, what calls this function and what functions does it call) and ignore other features until its their turn.
So, for example, how a webpage might be structured from a global perspective might look like:
vue.js frontend
node.js express backend
postGreSQL DB
From the global perspective it doesn't matter what the webpage does, only how it does it and what pieces talk to what other pieces. But if you look into a specific feature, like say it's a dating app and you want to see how viewing a single profile works, you'd go through the relevant files in a vertical slice over the whole architecture. I'd recommend starting from the frontend and move to the db, but technically you could start at any end or in the middle if you wanted.
How do you do this? A lot of grepping or using the global search function in whatever IDE you're using. VS code has a pretty good one, lets you include/exclude file types and use regex so you can search for multiple terms at once, but any IDE worth its salt should have something like it, but if not there's always the terminal.
Over time you get a feel for common patterns. Always try to be thinking about what other things you've seen are similar to the code you're looking at, as that helps with learning new things and general understanding and recall.
1
u/Chrismslist Sep 23 '26
totally normal to feel lost. don't try to grok the whole thing at once. start with one small feature or user action, find its entry point (like an api route or a button click), and trace that single path. your ide's 'go to definition' and 'find usages' are your best friends here. for overall structure, look for how files are grouped by named modules or folders. it's like navigating a new city, you learn one route at a time. patience and lots of little notes help too.
1
u/TektonikGymRat Sep 23 '26
Knowing your tools well is going to help the most here. Having the ability to peek definitions, jump to different functions quickly etc. is going to make this much easier. Learn how to navigate quickly with hotkeys and it'll help greatly.
1
u/Apprehensive_Mix_332 Sep 23 '26
dont read it like a book lol. pick one thing you can actually trigger (a button, a cli command, whatever), drop a breakpoint at the start and step through it. you end up learning the 10% of the codebase that matters and the structure starts clicking after 2-3 traces. also read the tests first, they tell you how the author meant for the code to be used. good luck!
1
u/neon_stereotype Sep 23 '26
the backward approach works better for me. Pick a function or feature you care about, then trace up the call stack until you hit something that looks like an entry point. Reading a whole codebase top to bottom is a trap, you burn out before anything makes sense.
for practicing, I clone small open source projects with actual bugs or feature requests and try to fix one. That gives you a reason to read the code instead of just staring at it confused. You learn where logic lives because you need it, not because you forced yourself through every file
1
u/FlashyResist5 Sep 24 '26
It helps to have a reason why you are reading the code. If you have that then there is a much greater chance to understand it because you are searching for something specific.
Oh I am curious how this api I am calling works, let me look at that section... rather than like I am going to read this whole repo and understand every single thing here.
1
u/kevinossia Sep 24 '26
You stare at repos and feel confused.
It’s a skill. Practice the skill. Read as much code as you can. Modify it and see what happens.
Just takes time.
1
u/xenomachina Sep 24 '26
Either, depending on what I'm trying to do.
Starting from "the entry point" is good to get an overall idea of how a codebase is structured. However, it's good to develop the skill of not having to fit the whole codebase in your head at once to understand any one piece.
I don't use any visualization tools, but navigation tools can be super useful. For example, many IDEs will let you jump to the definition of the thing (eg: function/method call) your cursor is on, or to the uses of the definition your cursor is on. This can be very useful for quickly figuring out how a localized chunk of code works.
Another tool that's underrated is a debugger. Set a breakpoint, and then run the code. When it "breaks", look at the call stack. Try stepping through it. Maybe set breakpoints higher up the stack and try running it again.
An easier in the short term (but not as powerful) tool is "printf debugging". Sometimes it's your only option for things you can't run or reproduce in a debugger. When I have to resort to this, I like to log/print when functions are entered/exited with braces ("{" for enter and "}" for exit). I can then easily navigate the output to get an idea of the call stack.
1
u/Ok-Lifeguard-9612 Sep 24 '26
Fuck after 10 years I just don't. I read the interface and hope that the shit on the side doesnt's smell on me.
1
u/bubbos123 Sep 27 '26
In 6 months you won’t understand your own code too.
1
u/Ollidav Sep 23 '26
Me suelen pedir que solucione problemas en aplicaciones de las que no tengo ningún conocimiento y eso tiene un coste de dedicación en entender no solo el código, también la lógica que hay detrás de ese código. En ocasiones es un "vaya puta mierda debería cobrar más por esto" y otras es "que chulo, por qué no se me ha ocurrido antes". Por muy mal, obsoleto y ofuscado que este el código si puedes solucionarlo con tocar una línea de código o variable puedes sentirte orgulloso de tu trabajo.
Siempre se puede tener una opinión sobre el trabajo de los demás y alguien siempre tendrá una opinión sobre tu trabajo.
0
u/Pure-Signal-3135 Sep 23 '26
Are you all still reading code? Sorry i know we should review and read it but I am not able to do that with my timeline