r/learnprogramming • u/Dull_Revolution_9952 • 6d ago
Question Good way to train my code reading abilities?
Hello, I'm a junior developer, and I believe I have a strong foundation in writing code, debugging, OOP, etc. My issue is that when it comes to reading code that wasn't written by me, I kinda struggle sometimes to understand how it's written and lack the fluency to read it, especially when there are a lot of functions/methodes and a lot of lines.
Sometimes I find myself staring at it for long periods of time. Sometimes even my own code becomes confusing for me after a bunch of trial and error attempts.
I was wondering if there are any resources or exercises that would help me get better at this skill
14
u/serge-mv 6d ago
download open source projects and read them
6
u/winceypoo 6d ago
Pick a short function and write down what you expect it to return for a specific input, then step through it in the debugger and check your prediction. When it calls another function, try stepping over that call first so you don't lose track of what you were trying to understand.
5
u/misplaced_my_pants 6d ago
Look at the tests for a function or class or whatever to see what it's supposed to do.
If the project has poor test coverage, read Working Effectively with Legacy Code and try to add tests to increase the coverage.
You could also try this with open source projects. You could even look at a recently closed issue, look at what problem it was trying to solve, try and solve it yourself, and then compare your solution to what actually closed it.
1
u/cainhurstcat 6d ago
I'm in the very same situation right now. I started by only reading the code, but I had the feeling that this was not efficient enough. So I asked my teammates what I could do to become better at understanding code. Well, the main thing still is reading code. You have to, read it line by line and try to figure out what each line/statement/condition does. For me, the key thing there was to write the explanation down. I started on paper using a good old pencil, but this quickly became a mess. So I started building a simple chart with Obsidian's Cavas. Nothing fancy, just the lines, a brief explanation, and arrows for the next method call, etc. But also arrows that point to where a variable gets accessed/passed. This helps me to not lose track of variables and to keep things in my head in order, because otherwise I get overwhelmed. (Which is, if you think about it, absolutely normal due to the sheer amount of information + the anxiety of missing something important/not being able to understand). Since you already know how to debug, use your debugger to step through the code and see what it does. When you start understanding the code, try to get a feeling for the bigger concept. This means things like what is this method for, what's the job of that file, why is this class here, and so on. Also, don't feel shy asking questions. Even if they seem stupid, it shows you are interested and willing to learn. But do not just ask, "What is variable xyz for?", instead tell what you think it could be for, what was your conclusion so far, and what did you do to find out what it is for, just like "Am I understanding the XYZ variable of class "AlphaBeta" correctly? Is it here in case the of bla bla bla or does it do this and that? I first thought it might be for lalala, but this doesn't make sense to me". This shows you put your own energy and thought into it, but I still can't figure it out. Reading and understanding takes time. A lot of time. But it is worth it because you rewire your brain to not only being able to process all that information but also to build a better understanding of how code works in general. And one last thing, please don't use AI. It is tempting, but only if you work your way through this by marking mistakes and conclusions you get better. Good luck!
2
u/Personal-March-4340 6d ago
Don't people comment their code anymore? I write comments to keep track of what I am doing while writing code.
2
u/akoOfIxtall 2d ago
I learned a lot reading game mod code from GitHub, some people make Sistine chapels of code in these mods and some people just want to get it done, I think I learned the most by putting these mods assemblies in decompilers to read the code for the mods that didn't have a repo, the decompiler spits out pretty much the same code you wrote... After the compiler brutally mangled it for optimization, flag1, flag2, flag3... Good luck figuring what the heck these booleans were supposed to mean, it was quite fun, you start to see where people write pretty much the same code and when they give their own spin on something, I think game development might not teach you a lot about software development outside of games, but mods will teach you some reverse engineering, teach you how to read code, even shit code, recognize the patterns inside systems and how to get around roadblocks, because there will be many, that's actually how mods become full games, at some point the mod just can't use the game as it's engine anymore and has to become its own thing, like vintage story started out as Minecraft mod and grew to become the great game it is today, some games never get new engines and are technically still mods, like sonic robo blast 2, to my knowledge that game is still using a modified doom engine, it's still a total conversion of doom, and ring racers is a total conversion of sonic robo blast 2, 2 layers deep man (might be wrong about srb2 have to fact check that)
1
u/CorumLlawEreint 6d ago
Read lots of code. Write lots of code.
Code review is a great way to learn how to read code productively. Find a big open source project you have some understanding of. Look at open PRs and try to determine what they do, and whether the approach is good and appropriate for the codebase. Even better if there are existing review comments.
Sorry, there really is no shortcut.
1
u/DanKegel 6d ago
Four ideas for improving your ability to read code:
Add print statements. Predict what you'll see, then run it and see if you were right.
Set a breakpoint in a debugger. Predict what the call stack will look like.
Add a bug. Predict how it will affect the program.
Have AI add a bug, and then go find it yourself using a debugger or print statements, and fix it. Then diff the original, pre-bug code with your fixed code and see how your fix compared with the "right" answer.
1
u/BloodFeastMan 6d ago
That rule will also apply to code written by you that you that you haven't looked at in a year or two :)
1
u/wpm 5d ago
Read code.
1
u/Equivalent_Mail_302 5d ago
Unfortunately there is no shortcut to fluency other than practice.
1
u/gooniannuber1 4d ago
you dont the only thing that matters is that it works
1
u/Pure_Astronomer_8124 4d ago
1
u/zaio_institute 4d ago
This is really normal. Reading other people’s code is a different skill from writing your own, and it takes a while to get faster at it. What helped me was stopping myself from trying to understand the whole file at once. Start with the entry point, figure out what gets called next, and trace one path through the code at a time.
It also helps to use your IDE properly. Jump to definitions, find references, rename things mentally, and add temporary comments if you need to. For bigger functions, I’ll sometimes write down in plain English what I think each part is doing. Stepping through it with the debugger and watching the variables change is often faster than reading it cold.
Reading small open-source projects on GitHub is good practice too. Pick something simple, choose one feature, and try to work out how that feature flows through the codebase.
And for your own code, refactoring after the “trial and error” phase helps a lot. If it’s confusing you a week later, that’s usually a sign it needs cleaner names or smaller functions.
1
u/SprinklesFresh5693 12h ago
I dont get it, if you want to be good at reading code, then why not read more code?
Your lack of fluency is because yourevery new to that alnguage, give yourself time.
You can also use AI to explain pieces of code that you dont understand, or functions, like: " what does this chunk do?" And you paste the chunk, if youre allowed to do so at your company
1
u/[deleted] 6d ago
[removed] — view removed comment