Clean Code for Better Understanding

Krisna Dwitama
3 min readMay 2, 2021

--

Source: https://kbimages1-a.akamaihd.net/624ce3ac-1151-489a-8b61-7b24168205e5/353/569/90/False/art-of-clean-code.jpg

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”

— Martin Fowler

Have you wrote a code and try to understand it a month or a year later? Did you could understand it? If no, then maybe you should check if your code is “clean”. Clean code can help you and other to understand your code better and make your application easy to maintain.

Clean Code not only help in understanding the code, but also can make the architecture of your code more “elegant”, so that we feel more comfortable when we see the code for a long time and it is easier to modify the code. Here is 6 example about Clean Code that can help improving your code quality:

1. Meaningful Names
In our code, we name our variables, functions, classes, files, etc.
Because we often naming things in our code, we must use a meaningful name so that we can easily recognize what is these things made for. Some of the tips in naming things are:
- Use intention-revealing names
- Pronounceable and searchable
- Make meaningful distinctions
Example:

In that example, the class name match what that class will contain, that is the Page for Pahlawan’s Notification (NotifikasiPahlawan in Indonesian) and that page state. The variables also use meaningful name, such as:
Aktivitas: Contain all of the user activities
Name: Contain user name
Address: Contain user address
recentActivity: Contain only user recent activities

2. Simple Function
Function should only do one job. The function also must not have any side-effect to other functionality so that other team can understand the behavior of these function and can modify it without affecting other section.
Example:

That function only do one thing, that is to retrieve all of the activities of a user.

3. Don’t Repeat Yourself (DRY)
Avoid code duplication. If you will use some code in many places, you can make a function that will cover all those code so you can just call one function instead of writing many lines of code.
Example:

In the page, there is dividing line between one section and the other. Because there can be many sections, then also need many dividing lines. Rather than we write the code for making dividing line in many places, we can instead make one class called “DividingLine” and in every places that need the dividing line, we can just write “DividingLine()” to place those line.

4. Layout Formatting
Using formatting code (layout) that match the format convention of the programming language so that there is consistency in the code and you or your team will be familiar with those format. Linter tool is one of the tool for ensuring that our code meet the standard of the programming language layout convention.
Example:

That is the example of linter for flutter in my project. At first, there are some issues that violate the flutter layout formatting standard. With linter, we know exactly where the code that violate the standard and what is the cause of the violation so we can easily fix them and our code will have the same layout with the code of the other team member.

That is some of the tips for clean code. I hope after you follow these tips, you can better understand your code and other people who read your code can also be happy when reading your code.

--

--