It is every developer’s responsibility to write clean code. Code is clean when it is easy to read and understand by the team members.

Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. …[Therefore,] making it easy to read makes it easier to write.

― Robert C. Martin

When writing a piece of code, the developer must understand that it’s not his/her code, it’s the team’s code. Those team members (current or future) are going to spend time with it. If the code is not readable, then the team is going to struggle.


The Problem

Why programmers are slow?

It is very easy to start from scratch and implement new features to a new project. But, as the code base increases, the developers might take more time to implement new features.

Why so? 🤔

Developers 💻 normally do the following things while writing code:

  • Read the existing code and understand how it is working now (even if the code is written by the same developer). 📖

  • Try to find a way to solve the current problem. 🏗

  • Try to implement a solution, which won’t break existing features. ❎

As the number of lines in the existing code increases, it automatically adds up time in steps 1 and 3. Which will easily slow down the developers. ⏲


The Solution

How to make the step1 faster? Write readable code.

How to write readable code?

There are several practices/principles to write readable code.

  • 1. Take time to review the code

    One can assure writing readable code only by asking others to read it. Implement a review process by the colleagues so that each/some of the team members read other’s code and understand those. Make this process frequent.

  • 2. Avoid unnecessary complexity

    Simple code communicates what it does clearly without exposing confusing/surprising details.
    One best way to make the code simple is by following YAGNI (You Ain’t Gonna Need It). At any point in time, before implementing something complex, ask whether it is really important to implement now or can it be delayed. How this help? 🤔. This depends on “how much do you know now” and “the cost of implementation”. If this is better to be implemented later with more detail then it is better to do so. YAGNI helps the developer to implement a simple and fast solution.

  • 3. Avoid code duplications (DRY — Don’t Repeat Yourself)

    Extract the duplicate code and replace with higher-level abstractions. This will save time to read and understand the duplicate code again and again.

  • 4. Focus on SRP (Single Responsibility Principle)

    Whenever writing functions or classes it often tend to implement multiple things into one abstraction. Which clearly adds complexity and increases the chance to write longer functions.

    Following SLAP (Single Level of Abstraction Principle) helps the developer to write short methods. SLAP Which says, each statement in a function should belong to the same level of abstraction. Which will help the readability, because it is cohesive and helps to extract non-cohesive code as other functions.

  • 5. Write Tests

    This is one of the best ways to write readable code. Code is clean when it contains tests. We can think of tests in this way: tests are the explanations of our code. If you want to understand a particular logic in a piece of code, just look at the tests and which will explain why that logic is there. One can follow these steps to write good tests.


Author | Sujith PS | Co-founder & CTO, Kiebot

Leave a Reply

Your email address will not be published.

Comment

Name

Email

Url


Call Now Button