Clean Code = Clean Soul and Mind

Yafi Ahsan Hakim
Portelier
Published in
4 min readOct 21, 2020
source: https://res.cloudinary.com/practicaldev/image/fetch/s--pq7nbJmD--/c_imagga_scale,f_auto,fl_progressive,h_420,q_auto,w_1000/https://dev-to-uploads.s3.amazonaws.com/i/s2bgjgyu60e1yltnhoa3.jpg

As a programmer, we all should write what is considered as a “clean code”, but what exactly is clean code? why is it so important for everyone to write clean codes? well, that is what I will talk about in this article.

What is Clean Code?

Clean code is to put it simply, a code that is easy to understand, easy to change, and easy to maintain. Your code shouldn’t make other people scratch their heads when they see it, instead, they should understand what the code does easily just by reading your code.

Why Clean Code?

Imagine you are working on a project, usually, you will not work on the project alone, you have teammates to work with, now let’s say that you write a code and then your teammates want to continue your work, if the code you write is clean then your teammates wouldn’t need to spend more time to understand the code you write, that’s why clean code is so important, it saves time for everyone and it enables everyone to work more efficiently.

How to Write Clean Code?

There are a lot of things that we can do to make our codes clean, here is a list of some things that we can do to make our code clean:

1. Write Codes for People to Read

when we are writing a code, it is important that the computer can read the code and do exactly as it says, but it is just as important for people to be able to read it, not only for others but for yourself as well, because you may come back to work on a code you worked years ago and if you can’t read it, then who would understand what it does.

for example in my current project, instead of declaring every prop in one line like this

it would be easier to read if I write it in a couple of lines like this

2. Pick the Right Name for Your Variables and Functions

You can pick names that are descriptive enough so that other people will be able to understand the purpose of that variable or function. In other words, the name itself should tell us what the variable contains or what the function is used for.

for example in my current project, instead of naming my variables like this

it would be easier to understand if I write it like this

3. Don’t Repeat Yourself (DRY)

When we write a code we should not have any duplicate codes in our code. duplicate codes will make our code harder to maintain because if we want to make changes, then we need to change multiple codes just so that there are no error. One of the ways to eliminate duplicate code is by isolating the code into a function and whenever we want to use the code we could just call the function.

4. Informative Comments

We can give some comments on the important parts of our code. If our code is already clean we shouldn’t have to write any comments, but sometimes it is helpful to write comments to help us remember how the code works. That said, writing a lot of comments will not help us transform poorly written code into a better code. If the code is bad, we should fix the problem by improving the code instead of just adding comments of instructions on how to use it.

Writing endless lines of comments will not help us transform poorly written code into a clean code. If the code is bad, we should fix the problem by improving the code, not by adding set of instructions on how to use it.

--

--