Amitav Roy

Blog on web and travel

Why is reading code important for every developer?

Posted on 30 Jun 2022 by Amitav Roy

Why is reading code important for every developer?

As a developer the habit of reading code can do wonders to your coding ability and accelerate your speed of learning. You spend only about 1 hour coding for every 10 hours you read. And hence, the skill of reading code becomes very important. Let's understand how it helps, how it works and how you can improve it.

As a developer the habit of reading code can do wonders to your coding ability and accelerate your speed of learning. It is believed that the ratio of time spent on reading code to writing code is about 10:1 as said by Robert C Martin. This means, you will only code for an hour for every 10 hours you read code. You might think - wow that’s a lot of reading and yes, that’s how things generally work.

As a developer, you will spend time reading code in many different ways like:

  • Understanding how things work based on reading the documentation of a framework or how a particular language works by learning the syntax of a programming language. That’s like the basic level of understanding.
  • If you are working on an existing project, you will spend a lot of time understanding different parts of the application and its behaviour before you add any new code for any new requirements.
  • Then, you might also spend a lot of time reading code of your team members as part of code review, to help them with their current tasks and also to look for scope of improvement in their existing implementation.
  • You might also spend time reading code from different open source projects.

If you look at our learning experience as well, it’s all about reading code whether it’s reading the documentation, a blog post, going through a code snippet on Stack overflow or we are looking at code inside a video while looking at a video tutorial just to name a few.

So, let us try to find out what are the benefits of reading code:

Read source code faster

The most obvious one is that it allows you to read source code faster and develop the ability to understand what is happening inside a code block. This helps you as a developer in multiple ways.

Let’s just say you are asked to change a behaviour inside an existing application. It’s important for you to first understand the flow of the code, how the request starts, and how the entire request lifecycle goes through the different aspects of the application. You won't be able to make necessary changes unless you have a clear understanding about that part of the application.

Now, the better you get as a code reader, the faster you will be able to understand the application and the faster you will ship your code. So, as you can understand this is a very important skill and that you should give importance to.

Learning architecture and understanding how complex problems are solved

This is one of the major reasons why I personally feel this skill is important. Thanks to the Open source community, there are so many applications which we can refer to and learn.

For example, when I was trying to understand the best possible way to implement Event Driven Development (if you want to know more about what is Event Driven Programming, you can read this article), I started looking for examples of applications where this particular pattern was followed. When I was learning this paradigm, I had a lot of questions about the correct architecture like

  • Where should the event be fired from?
  • Should it be raised from a Controller or from inside the Service?
  • Should I send different data to every listener or should it be consistent?

So on and so forth. And, I was able to find answers to these questions while reading code. It also helped me understand a few more concepts which I was not even aware of at that time.

Just to give you an example, the famous authentication package Breeze of Laravel raises an event User registered while handling the basic registration requirements and allowing you to add any additional behaviour that you want to attach to it, like maybe sending that information to a CRM.

Okay, now that we understand the importance and advantages of reading code, let’s try to understand why a lot of programmers find it difficult and how we can improve this.

Felienne Hermans who is an associate professor at Leiden University gave a keynote about the programmer’s brain and how memory works at QCon Plus November 2021. According to her, when we read code, three different processes of our brain play a key role: Short-term memory (STM), Long-term memory and working memory (WM). In fact, this process is not specific to reading code but in general.

Now, Long-term memory plays a very interesting role because it’s the one which tells us things like an array index starts from zero, we need a semicolon at the end of each line when we are writing code in PHP etc. Short-term memory on the other hand is like a buffer which is used to briefly store information which we see or hear. And, working memory is used primarily to process things.

For example, if we are looking for some code solutions, chances are that we will store that information in our Short-term memory and then use that in our code and then forget that after some time. However, Short-term memory and working memory heavily depends on Long-term memory.

So, coming back to our question - why do a lot of programmers find reading code difficult? Well, while reading this article did you ever feel that reading this is difficult? The answer will generally be no.

Why? Because you are familiar with all the alphabets and words of this language. And hence, you will see these patterns and you don’t find that difficult to read. But, if you don’t know French and you are reading a sentence or paragraph in French, then even though you might be able to recognise the characters, the words won’t make sense.

This is what happens when you read code. Every developer has a different way of writing code and so initially it can take time to read that. While reading code, it’s not only about the syntax but also about understanding what the developer is trying to do and what was his/her thought process behind writing that piece of code in a particular way.

So, to overcome this problem the only solution is that we read a lot of code. When we do that, we are making our Long-term code more familiar with these patterns. And as our Long-term memory gets better at it, reading code and quickly understanding it becomes that much easier.

Just imagine, if you have never used the single line ternary if else condition or the destructuring of Javascript, won’t you find it difficult to read that part of the code? To expand this example, in PHP and Javascript we can only return one data. However, in Python we can return multiple variables. When I read a code in Python for the first time, I was surprised. My initial reaction was - What the hell happened?

Then, it required me to look up Python syntax and then it realised that Python can do such things. And, after that it became easy for me to understand the code. This is how we can train our brain to be more efficient, and with more practice of reading code, we are accelerating our pace of learning and becoming a better programmer.

I highly recommend and encourage you to read code from other programmers. And, that is something which I tell a lot of junior developers as well whom I mentor from time to time. So, let’s store more valuable information in our Long-term memory to become a better programmer.

Photo by Firos nv on Unsplash