I can write and debug code, but I lose track when an unfamiliar codebase has lots of functions and methods. What exercises can help me get more fluent at following it?
How can I get better at reading unfamiliar code?
Answers
Pick a short function and choose a specific input. Write down what you think it will return, then step through it in a debugger and compare the result with your prediction.
When it calls another function, step over the call first. That lets you follow the current function without disappearing down a chain of dependencies. Once you know what the call seems to do, step into it if you need to check the details.
Reply to this answer
Start with the tests for a function or class. They can give you concrete inputs, expected outputs, and examples of how the code is used. Then trace the implementation for one test case.
If there aren't useful tests, try adding one for behavior you want to understand. Working Effectively with Legacy Code is a resource for dealing with code that's difficult to test.
Reply to this answer
Reading changes can be less overwhelming than reading a whole project. Pick an open source project you roughly understand and look at a proposed change. Figure out what it does and whether it fits the surrounding code, then compare your thoughts with any existing review comments.
Tavmori-8c:
A recently resolved issue can work too. Try figuring out a fix from the problem description before looking at the actual change.
Reply to this answer
Try introducing a small bug on purpose and predicting how it will affect the program. Then use the debugger or print statements to track down the effect and fix it. Comparing your final code with the original gives you a way to check what changed.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Torviko-5b:
Print statements work for this too. Predict what each one will show before running the code, rather than just adding a bunch and staring at the output.