Kiebot Learning Solutions

The Art of writing clean code

BLOGS

By Sujith PS

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:

 

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

 

Recent Post

Laravel Unit Testing

Laravel is the most popular PHP framework for application development. From the testing perspective, it is also known to be highly [..]

Introduction to React Hooks

Hooks are a new addition in React 16.8. With Hooks, you can utilize state and other React features without writing a class. React   [..]

Reactive Programming: Why should you care?

Reactive Programming is a way to build a scalable architecture that is resilient and [..]

SOCIAL

Gallery

Scroll to Top