Clean your code!
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
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.
Method Names — Name of methods should be a verb handleClick, validateField, fetchData, etc.
In a clean code, a method should be/have:
- 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).
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
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.
SOLID is an acronym. It is the core principle of Object-Oriented Programming. S stands for Single-responsibility principle. O stands for Open–closed principle. L stands for the Liskov substitution principle. I stands for Interface segregation principle. D stands for Dependency inversion principle. I am not going to explain it here to keep the length of this article short.
Design Pattern is a layout or scheme of entities (class, methods, etc.) that are established to solve a reoccurring problem in object-oriented programming. It is a reusable solution for a specific problem. Design patterns are classified to four: creational patterns, structural patterns, behavioural patterns, and concurrency patterns.
Refactoring Guru has provided us with explanations of design pattern and the code examples in many languages. You can check it out in the link below.
Design patterns are typical solutions to common problems in software design. Each pattern is like a blueprint that you…
Clean code should have tests that follow the FIRST principles.
- Fast — It should be run quickly.
- Independent — No dependencies between tests
- Repeatable — Tests should be able to be run in any environment and computers.
- Self-Validating — Tests should automatically validate itself whether they pass or fail. You should not need to check manually.
- Timely — Tests need to be written in a certain time in the development cycle. in TDD, developers write the tests first before developing the application.
Are we done?
No! This is just a summary of the clean code topic and it does not cover everything about the topic. Most of this information is gotten from articles that summarized a book called Clean Code by Robert Cecil Martin. Many developers recommend that book as it is considered as the main resource to learn about the Clean Code topic. Read it!
Mengenal Clean Code Lebih Dekat
Selamat malam semuanya, “Clean Code” sebagai salah satu aspek penting dalam software development tentu ada bukan tanpa…
The basics of writing Clean Code
A summary of the fundamental principles of writing great code, from the book by Robert C Martin
How to write clean code? Lessons learnt from “The Clean Code” — Robert C. Martin
There are two things- Programming and Good Programming. Programming is what we have all been doing. Now is the time to…
Clean Code Makes a Green Code
Cleanliness is very important in our life. Cleanliness is important for a healthy mind, body and spirit. Everybody loves…
by the author.