The essence of readable code

 

Readable code is a “never-get-old” concept that every developer knows by heart that if they want to move forward in their career, they need to practice the ability to write clean code. Like Martin Fowler has said: “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”

How quick the code is written isn’t important, it is the level of the code readability that matters. Rational, simple and consistent code draws out plenty of benefits. Practice of clean and clear code brings good programming patterns, contributing to easier testing and maintenance process. This all leads to cost-savings and increase in the overall value of creating and maintaining software.

Now it’s time to deep into some good tips to deliver readable code.

We always held the belief that commenting our code plays a significant role in assisting other developers in the team to understand what we did, and no one will turn away in terror while doing maintenance. Yes, it’s undeniable that commenting is crucial, however, if the code is really readable, it shouldn’t have a huge amount of comments. Instead, we should consider style and naming before thinking about comment.

#1 Style

Style can vary but needs to stick to at least some consistent rules including indentation and spacing. Try to think like how easy you can read newspaper because the text is divided into multiple columns with short but complete sentences. If you want your code to be simple and comprehensive, here is the golden rule: “Don’t exceed 80 characters on a single line of code.”

Also, always align your code. It’s small detail but creates big difference!

Is better than

Remember the DRY rule. DRY equals to “Don’t Repeat Yourself”. Don’t rewrite your code again and again, instead, put it in variables and functions. This makes your code easy to understand and update.

#2 Naming

Expressive variable name doesn’t make your code more readable. Good names should reflect and explain their content at a glance. In other words, we should be able to see what is contained in the variable, function, class, object, method or property just by looking at the name. Don’t be afraid of long names because modern programming languages and databases support names of more than 50 characters. To improve readability, try to avoid abbreviation. Also, avoid naming variables var, foo, x or mv. All of these contribute to the concept of “self-documenting code” which is very popular nowadays.

#3 Structure

Code structure is a wide concept including of data structure, abstraction, control structure and the use of temporary variables. But keep in mind that you need to take care of small things first before you can handle big things. The most practical way is to document with code instead of comments.

Above comments can be changed into functions:

Functional programming is extremely useful in terms of making code short but effective but we don’t dig into it in this short guide.

#4 Comment

Finally, we can talk about comments after mentioning it few times in this guide. We all undoubtedly know that comments are very helpful in explaining and reporting what we are doing. However, it is unnecessary and time-consuming to list all the details that we already knew. Comments should only state what the expected result is and why it is like that.

To conclude, these are just few but important tips for you to write better code. Through the time along with practicing, you would be able to write readable code. This benefits your code base growth as well as help you refactor your existing code at ease.

 

Reference

Bridgman, D 2015, ‘Writing highly readable code’, Medium The Startup, accessed  March 2019 from url:

https://medium.com/swlh/writing-highly-readable-code-94da94d5d636

Janeen, N 2019, ‘What Is Readable Code?’, Medium, accessed March 2019 from url:

https://medium.com/s/story/readability-as-usability-78c5a2a373cc

Huford, P, ‘How do you write readable code?: 13 Principles’, accessed March 2019 from url:

https://gist.github.com/peterhurford/3ad9f48071bd2665a8af