Clean your code!
By: Figo Muhammad
Tags: Guides And Tutorials, Clean Code, Software Development, Oop, Programming

What does the title mean?

The word clean, according to Merriam-Webster means, to rid of dirt, impurities, or extraneous matter or to rid of corruption. Then, what does code cleaning means? To clean our code means to get rid of impurities, “dirt”, or corruption from our code.

There are actually several terms as the alternatives of impurities which are code smells, bad codes, dirty codes, etc. In this article, I will stick with the term dirty codes, but do not be confused if you found other terms. It usually means the same things.

“Clean code is code that has been taken care of. Someone has taken the time to keep it simple and orderly. They have paid appropriate attention to details. They have cared.” — Robert C. Martin

What are those dirty codes?

First, dirty codes are codes that are hardly understood. A code is considered dirty if it satisfies one of the following statements:

The execution flow or how different objects collaborate is hardly understood. The role and responsibility of the class(es) are hardly understood. The purpose of the method(s) is hardly understood. The purpose of the variable(s) is hardly understood. The purpose of the expression(s) is hardly understood. And other statements that make the code hardly understood.

Second, dirty codes are codes that are nonadaptive to changes. A code is considered dirty if it satisfies one of the following statements:

It has duplication of codes. Classes and methods have more than one responsibility. Classes and methods are tightly coupled and low cohesion. It has an inefficient number of classes and methods. It has no tests, or The tests are hardly understood and hard to change.

Why do we have to clean our code?

Making the code easily understood by other people. Most of the time, developers read to understand the existing code and then write their improvements. Hence, the code easily understood may help them to understand it easily and may increase the speed of development.

It will also help you to understand your own code easily. Commonly, developers develop many applications at once. Hence, they may forget how some of what they have written and have to reread them.

“If you want your code to be easy to write, make it easy to read.” — Robert C. Martin

Principles for a clean code

I have this project that I am currently developing called Sistem Informasi Penilaian & Database Praktikum. The project uses Django (Python) and ReactJS (JavaScript) as the main frameworks. I am going to use that project as an example of the principles below.

Proper Naming

The names of entities (classes, methods, variables, etc.) should be descriptive and meaningful. The name should represent the usage or purpose of the entity.

The names of entities (classes, methods, variables, etc.) should be searchable and unambiguous. The name should be specific. For example, naming a variable button is a bad one because there may be many other variables named button in that code. Change it to loginButton, etc.

Replace magic numbers with named constants. For example, in a method that returns the value of the area of a circle. the π (pi) value should be defined in a variable named PI_VALUE at the start of the method and the next lines should use that variable instead of 3.14.

Class Names — Name of classes and object should be a noun, such as Administrator, Role, Notification, etc.

Django Models

Method Names — Name of methods should be a verb handleClick, validateField, fetchData, etc.

Proper Methods

In a clean code, a method should be/have:

Small, Only does one thing or responsibility (Single-responsibility principle), Avoiding using the flag parameter, having a boolean type parameter, Not returning null, Few parameters (preferred).

Commenting

A code should be self-explanatory without the help of comments. Although, you can still use comments for things, such as an explanation of intent, clarification of code, and warning of consequences.

There are five things we have to do to achieve a clean code in commenting:

Don’t be redundant Don’t add obvious noise Don’t use closing brace comments Don’t comment out code

Bad comments

Source Code Structure

Every line should be short. Related code should be together and blank line(s) is/are used to separate weakly related code. Variables are declared close to their usage. Each method should be separated vertically. Dependent and similar methods should be close and the if a function calls another function, the caller should be on top of the callee.

Don’t Repeat Yourself (DRY)

DRY means avoid duplication anywhere in our code. We should have not the same piece of code in different places. Using this principle improves the code’s adaptability to changes. If changes are needed, we can just change it in one place, rather than change it in every piece of the code.