Clean Code: A Helpful Concept that Make Us a Better Developer

Mohamad Rifqy Zulkarnaen
5 min readApr 5, 2021

Do you ever found yourself reviewing a code that has been made by your friend are quite difficult to understand? Have you ever tried to debug your program, but it seems you can’t find the source of the problem because you’re having a hard time read your own code?

Well my friend, rejoice! Clean Code is the answer for us! By practicing clean code, we can make our code becoming more understandable and more meaningful. Let’s dive deeper into this helpful concept.

Image source: https://github.com/julkwel/clean-code

Clean Code and It’s Benefit

Actually, there is no formal definition of what is Clean Code. In my perspective, Clean Code is a concept in software development that enables us to produce elegant and easy to read code so that our code becoming more understandable to the one who tried to read it.

Clean Code can helps us if we’re going to work in team to build a software. Surely, when we’re working on one feature for the software, we must integrate it with our teammates feature to connecting the puzzles of the software we’re building.

Characteristics of Clean Code

There are some characteristics of a code that “clean”. I will provide the detail along with some of example that i have already practiced in my journey to becoming a better developer.

Meaningful Names

When we declare a variable, we should name the variable by its intention and its purpose. For example, when we’re going to declare a variable that function as a list, we should give the variable a prefix or suffix “list” on its variable name so that we know that this variable contains list rather than using a name that does not describe it, e.g. lst, a, i, etc.

Here’s the example of meaningful names in one of my source code.

Meaningful name example (1)
Meaningful name example (2)

Functions

When we’re going to solve a problem by program, it’s better for us to split the problem into functions. A function should be small and only do one job. It should not do multiple job because we’re going to have hard time to if we want to reuse only several line on the function. We also should name the function in a descriptive name so that we know what the function does.

Here’s the example of a good functions:

A function that purpose as GET request in controller.

Comments

Comments are a text on our source code that intentionally we created so that helps the one who read our code knows what does our code do. Ideally, we should not write comments on our code. But since our code probably going to be read by other people, we should explain our intention. The comments we write usually categorized as:

  1. Clarification comments
  2. TODO comments
  3. Explanation of intent
  4. Informative content such as documentation
  5. Warning of consequences

There are common mistakes that we often found. When we found a bug in our code, we usually commented the code so that the line that has a bug won’t be executed. After we commented it, we tried to figure the problem and sometimes we forgot to delete the commented code that we just made. We should not do this because our code are becoming more sloppy.

Example of comments for function documentation

Error Handling

When we create a function or method, we should expect that something must gone wrong. We must get ready to face what kind of error that we’re going to see when we code something. Therefore, it’s better for us to handle the error first before we code.

Some of the best practices on error handling are:

  1. Usually, we try to use exceptions rather than return codes.
  2. Implement our try-catch-finally line first, then we implement our code.
  3. Don’t return null and don’t pass null.
  4. Define the normal flow of our program.

Here’s the example of error handling in my practice.

We handle an error that occured when the data is invalid by returning HTTP status code.
When doing negative test, we intentionally make the function fail so that it raises exception.

Testing

“Unit testing is a developers first line of defense for sleeping safe at night.”

Dan Leonardis

We should write tests for code that we’ve just made. We usually write tests for class and functions that we defined. Each tests should test the expected output from input that we define in tests. By testing our code, we can prevent unwanted behavior when we have changes in our code.

For example, when we noticed that are tests that failed, we will know that one of our functions is not working as expected. Therefore it’s more easy for us to debug it.

Tests using stubbing in Springboot
Tests example for model behavior
Negative tests for REST API

Conclusion

I think clean code is a “must to do” thing when we develop a software. I already experienced the benefit of this wonderful clean code thing and now, my code is more easier to debug and easier to be understand by my team mates that reviewing my code. I also think that clean code should be taught to the beginner programmer too so that they can develop a code with less code smell from the moment they study programming.

--

--

Mohamad Rifqy Zulkarnaen

just your typical curious and storytelling loving software engineer.