Alt Text

7 ways to improve your code reading skill / Bazlur Rahman

Reading Time: 4 minutes

Reading source code is one the job description of a software developer. However, this experience is not always pleasant. Not everyone would like to read someone else’s code because they find it boring, sometimes frustrating. There are cases when you start reading someone else’s code but end up getting a bitter feeling for not being able to understand or the code is not well written. Most developers want to focus on mostly writing code rather reading, not realizing the fact that reading code also a skill to have.

However, reading source code has manifold benefits. The payoffs of reading code are enormous. One can compare coding with literature. You would see, most of the writers focus on reading great masterpieces while they write. The reason is you cannot just create anything out of a vacuüm. You have to absorb things from different sources and work on top of that and create your own masterpiece. Writing code is not anyway different from this.

To be able to write good code, you have to read a lot of good code. This way you can learn how other developers think and how they solve a particular problem and where they lack. You can take up different ideas, styles, patterns from reading others code and let your knowledge grow gradually.

If we want to put it in simple words– code reading skill improves the coding skill.

There are several ways to improve reading skill, in this article, I would like to mention a few-

1. Run the code – Well this is the first step of reading code. Being able to run a particular project immediately gives you some mental ratification to explore more. However, this might not give you a lot of details about the project, however, you will know how to build it and run it, also you will get to know about the libraries it uses, framework it depends on, etc. which is a great way to improve your understanding of a particular project. If you want to write your own piece of software similar to the particular project you are exploring, you might catch some idea about the possible framework or libraries you may use.

2. Find the high-level logic – You may not jump into each detail when you start reading a project just yet, rather you should focus on the high-level structure. Start from the entry point. Most projects have a main method, start from there. Besides, if this is a web application, start looking into different packages like where is the business logic code, where is the UI code, where are the controllers, etc. Basically, skimmed through the whole project and gain a primary idea and then ask a question to yourself where do you want to focus, which part you want to read first. You may not read the entire code base, rather a portion of it may interest you. When you find your spot, you can start by method calling and see where it leads to you.

3. Use/ know tools– There are plenty of tools out there for reading and exploring source code that can help to visualize code. For example– IntelliJIdea has really a great capability of navigating source code where you can search by words, part of a word or even abbreviation. You should learn the keyboard shortcuts as well. Navigating source code with the mouse can be pretty boring and slow where working with the keyboard shortcuts can be faster. You can jump from one part of the source code to another quickly.

There is another great software to read code, called Sourcegraph, which was created by two Stanford grads, Quinn Slack and Beyang Liu, who, after spending hours hunting through the poorly documented code, decided to build a tool to help them better reading and understanding the code.

4. Know the language/ conventions– Knowing a particular language deeply helps you to improve your code reading skill. Every language has its own set of conventions, styles, and syntax. Knowledge of those helps to be familiar with a particular code quickly. For example – in Java, method name starts with lower case, wherein C# , it starts with upper case. Having known this distinction, helps you to identify the methods from the source code.

5. Read the best practices/ design patterns– The code you are reading, a certain construct of it, may seem obscure or unknown which is natural. There are plenty of good practices and design patterns that people use to do things rightly, optimally. For example, there is a pattern called Singleton where the constructor is kept private. You may ask, why on earth someone has to keep constructor private, I have always seen it is as public, otherwise, how would I create an instance out of a class. Well, there is a reason behind it. Singleton pattern prevents to instantiate a class more than once which has many practical use cases. If you know the pattern, you would not bang your head against the wall for that. So, having the knowledge of certain patterns– for example– Gang of Four has 23 documented design patterns, can help your code reading skill dramatically.

6. Code Review– Software development is a very collaborative work. No one can build a large or a significant software alone. Every software is built in a team. In a team, everyone contributes to shaping a project. End of the day, everyone’s contribution get merged and become a good piece of work which has a real value to the customers. Besides doing the actual coding, there is another practice that every team these days does, which is, review each other’s code making observations, suggestions and learning from one other. This is a powerful tool for building knowledge of a code base, creating strong bonds in a team and improving code quality that leads to fewer bugs in the system, and happy customers.

Doing code review, you are forced to read someone else’s code in your team which ends up improving your code reading skill.

7. Temporary Refactoring– Temporary refactoring also can help you improve your code reading skill. You can start by taking a long method and then keep breaking the method into multiple one. Keep doing that until you get a sense for what the intent of the larger method is about. After that, you can take a few notes of what the method does and then rollback the changes.

This can help your understanding of the method construct and improve your knowledge of refactoring as well.

This post is also available on DEV.