DEV Community

Cover image for Importance of writing a clean code and how to make code readable
SenaitEkubai
SenaitEkubai

Posted on • Updated on

Importance of writing a clean code and how to make code readable

Today, I needed to add a new feature to an old project I have worked on about three months ago. When i opened it, I could not tell which part of the code does what. It took me three hours to add a minor feature which I would have added in half an hour had the code been clean and tidy.

When it comes to writing a clean maintainable code even experienced programmers struggle. As programmers, most of the time, we work in teams weather small or big. Writing clean and readable code is important not only to us but also to people who may read our code. Below are the three top reasons why it is important to write a clean code.

Importance of writing clean code

1- It makes our work easy to maintain.
2- It makes our code easy to understand.
3- It saves time in the long run. If you write a clean code, you know which part of you code does what so it is easy to add new features.

How to write a clean code
1 Add meaningful and descriptive comments

Comments are very important when writing a code. They provide information about what you code does. They need to be clear and descriptive.

2 Add proper indentation and formatting

Indentation and formatting makes code look organised and pleasing to look at.Tools such as the vs code document formatter and the vs code extension prettier prettify code. There is also an extension called bracket pair colorizer in vs-code that colourises matching brackets and make code look colourful and pretty.

3 Avoid creating longer and complex functions

It might feel that you are saving time by writing one big function instead of breaking it down into smaller functions, but in the long run it is not the case. If you have a function that does a complex task, maintaining it will be a nightmare and time consuming. I found this out the hard way.

4 Declare global variable at the beginning of your code

Having global variables at the top makes it easy to navigate and find variables later when you code becomes longer and complex.

5 Get rid of unused code

Having pieces of code that do not do anything laying around with you other code is not a good practice. When you come back later to have a look or to maintain your code it will be confusing. As soon as you are done working with your code, review one more time to see if you have unnecessary and unused pieces of code.

Writing clean, readable and maintainable code takes practice. You do not just master it overnight. If you follow the good practices of writing clean code and try to implement them on every piece of work that you do, they will became second nature.
Write a code that is easy to read and maintain, your future self and people who might collaborate with you will thank you.

Top comments (1)

Collapse
 
saascto profile image
Joël Phillips

Nice insights - clearly taken from the pain of real world experience 😀