Do you know the difference between a junior developer and an experienced one? The first one's eager to show off his/her newly acquired coding “expertise” by writing everything himself/herself. Whereas an experienced coder would seek to leverage already existing work. So, if you're aiming to abide by a senior developer's principles, you should strive to constantly improve your code reading skills.
For if you want to write great code, you first need to be able to read other people's code, to fully understand it:
- how do other developers think and operate?
- how do they address particular issues in their code?
- what better solutions could they have come up with?
- what are their coding styles, the patterns they've developed etc.?
And yet, I know what you might be thinking right now:
“But it's terribly boring and so frustrating! I'd rather write my own code, from scratch, than “waste” priceless time getting tangled up in others' lines of code.”
Yet, if you're determined to reach that level:
- where you easily detect errors and issues in your own code way before you've hit the “do compile” key
- where you're proficient enough in reading any other developer's code so that you can leverage valuable work that's already been done (and therefore save time and money)
Then it's a must: you need to practice your code reading.
And here are some good (if not the very best) exercises that you could do in this respect:
1. Run the Code
How would just running the code improve your code reading skills?
You'd get a clear picture of all the 3rd party libraries that that specific project uses and the particular frameworks that it relies on etc.
In other words: in case, let's say, you'll ever want to build a similar project, you'll know just what libraries, frameworks, extensions and other 3rd party services to go with.
2. Use the Right Tools for Exploring and Visualizing the Source Code
Do you feel as if you're venturing into “alien” territory once you start reading another developer's code?
In this respect, having the right software at hand can make navigating unfamiliar code a lot smoother.
That being said, would you care for 2 recommendations?
- IntelliJ IDEA: it enables you to advance through source code just “like a fish in the sea”; to search by part of a word, abbreviation, by entire words, you name it
- Sourcegraph: a tool geared at helping developers dealing with other devs' poorly documented code to better visualize it and understand it
3. Review Your Fellow Devs' Code Before They Roll Out to Production
Another great practice technique to improve your code reading skills.
Doing code review with your peers will help you:
- come up with the right observations
- make the right suggestions to improve the codebase's overall quality
- ask the right questions, as well
All while learning from your fellow devs' coding styles and the solutions they've found for handling certain challenges.
And even from the errors and issues that you, yourself, might detect, that skipped your team's vigilance, while reviewing their code.
In short: reviewing other developers' code is a way too powerful “exercise” not to include it into your (hopefully) daily code reading practice.
4. Know that Specific Language's Coding Conventions
Being more than just familiar with the syntax and the coding conventions specific to the programming language that piece of code has been written in is simply... common sense.
And also one of the many viable answers to your question: “How to understand code quickly?”
Let me give you just one “enlightening” example here:
Method names start with an uppercase letter in C#, whereas in Java they start with a lower case letter. Knowing just this “detail” here alone will contribute to enhancing your understanding of that piece of code.
5. Debugging: One of the Best Ways to Improve Your Code Reading Skills
Detecting errors, identifying and fixing issues that your fellow devs might have overlooked — in short: debugging another one's code — is probably the best type of practice for improving your code reading skills.
Note: use “F6” and not exclusively “F8”; this will take you through each line of code. As you advance through code, do take your time to analyze each variable's values.
Put debug pointers in all the called methods — if you're dealing with multiple method calls — then check where a particular method is called and place the debug pointers right there.
This way, when executed, it's there that the calls will get directed.
Needless to add that the whole point of this backtrace exercise is getting to understand how that given piece of code works.
How its core method-call/return mechanism functions.
6. Comment Your Code Liberally
Does your own code need further explanations? Comment it!
Write comments on its:
- used standards
- core structure
- used logic
Add 1-2 explanatory lines to each function that you've used, thus describing both:
- the arguments
- and what it returns
Not only that such comments will improve your code's quality, but your code reading skills as well.
And that without you deliberately practicing in this respect: commenting code is common practice, after all.
7. Temporary Refactoring
To get to understand the construct of that particular method:
- What's the intent behind it? What is it meant to do?
- And what does it “end up” doing precisely?
Once you've closely “inspected” all its smaller pieces and you've found the answers to the above questions (and ideally even wrote them down for further “analysis"), feel free to rollback the changes.
The END! These are 7 handy, yet effective “exercises” that you could do to improve your code reading skills.
Don't take it as some sort of “recipe for success”, though. Just remember that it's daily practice that makes these techniques effective!