Becomes Better with Clean Code

Jonathan Chandra
6 min readMay 2, 2021
Photo by AltumCode on Unsplash

This article is made for the Software Project course assignment, Faculty of Computer Science, Universitas Indonesia

Have you ever wonder why you become confuse with your own codes? Or you don’t even want to read your codes anymore because it’s too complex and messy? Maybe it’s because your codes is “dirty”. Dirty code is a code that is contain code smells and hard to maintain because it’s hard to read and and comprehend in the first place. You can check more about code smell in here. Dirty codes is not good when you developing a software in a team because it will slow down the process of development because most of the time will be used to re-comprehend the codes that are already written when you need to fix a bug or maintain the codes.

To deal with dirty codes, we can clean it and make it to become clean codes. How to clean it? Here is several tips to write a clean code:

1. Use Meaningful Names

I think this is the simplest way to help you write a clean code. You will be write a lot of variables, methods, classes, package, arguments, and many more. When you write those things, make sure you always name it with a descriptive name. Here is the example:

Example of meaningful name for a variable

From the example above, we know that variable is used for saving a price of a product.

2. Single Responsibility Principle (SRP)

When creating a method, make sure it just have one purpose or one responsibility. A method with so many task to do will be make the method becomes too complex and hard to understand. Here is the example:

Example of SRP on a method

Suppose updatePendingProductStatus(‘add’, isChecked, quantity) is a method that add the quantity of a product by one. Function plusClicked is a method that called when user click a ‘+’ button on specific product. As you see plusClicked method is only do one task that is to add the quantity of a product by one by calling updatePendingProductStatus method, no more than that.

3. Avoid Writing Unnecessary Comments

You don’t have to write a comment for every line of your codes to help others understand your codes. Unfortunately, it helps them to become more confuse to when they read your codes. If you already apply meaningful names, I think it already makes your codes become readable and easy to understand. You can write a comment if you for example dealing with third party API like this:

Example of writing comment when dealing with third party API

4. Write Readable Code For People

Try formatting your codes with proper indentation, whitespace, or line break. It will also help other people to read your code because your codes will not be messy. Here is the example:

Example of formatted codes

5. Write Tests

If you don’t want to write a messy codes, then you should write the simple one. How do we write a simple code? The answer is to implement Test Driven Development (TDD) on your development process. TDD is a process where you always create tests before you write the implementation codes. The tests will consist of expectations of the things or requirements that should be implemented. With this method, you tend to write the implementation codes to pass all the tests and you will avoid to write unnecessary codes and your codes will be simple and robust. If you want to know why TDD helps to make your code become clean, you can read my article about TDD here.

6. Use D.R.Y Principle

D.R.Y (Don’t Repeat Yourself) principle is a principle where we should not re-write same codes in many places. In some cases, there may be a chance that your codes will be used more than once. If the codes has so many lines and repeated in many places, your codes will become messy for sure. To implement D.R.Y principle, one of the example is to extract the codes to become a method and called that method in anywhere you need. By doing that, you reduce the lines of code to become only one line, isn’t that cool? Here is the example of using D.R.Y example:

Example of method from extracting some line of codes
Example of updatePendingProductStatus method is called in many places

From the example above, we can see that the code become simple and easy to read. Imagine if the codes inside of updatePendingProductStatus method is inside methods on the second screenshot, the codes will have a lot more lines and become messy so you probably don’t want to see it because you already confuse from the moment you see it.

7. Exception Handling

If your code contain an exception handling, there are rules to make your exception handling code become clean:

  • We should not catch top-level Throwable because when we encounter an exception, we don’t know the meaning of that exception and it’s hard to fix. And also we can catch memory error or internal error.
  • Try to avoid catching general exception because we might catch some runtime exceptions that should not be caught. For example, we should not catch NullPointerException because NullPointerException is the result of programming error, our own mistake. If we catch NullPointerException, then it’s means we cover up our own mistake and that’s not a good thing. Here is the diagram of which exceptions that should be catch.
  • It’s better to use only one catch block, it’s make our code become more simple. If we want to give different message for different exception, we can checked the type of the exception inside the catch block. With this way, your try catch code become more neat, simple, and easy to understand. Here is the example of clean try catch code. Your catch block also should not be empty, only consist of comments, and contain unhelpful code.
try {
//do something
} catch (IOException | ArithmeticException e) {
if (e instanceof ArithmeticException) {
// do something
}

if (e instanceof IOException) {
// do something
}
}

That’s it for this article, I hope this article helps you to understand how to make your codes to become clean and you can start writing a clean codes for your own goods. Thank you!!

References

--

--