Clean Code : Investing Time within Codes

Image source : morioh.com

“So if you want to go fast, if you want to get done quickly, if you want your code to be easy to write, make it easy to read.” — Robert Martin

I used to think as long as the code works, then all is well. But writing a functioning code isn’t enough. Turns out, there’s some quality attached to it and I have to say, I used to write bad ones. A code should not only be functioning, but it should also be understandable by other people, only then can a code be called clean.

A clean code has these four characteristics :

  • Elegant — The code is neat and easy to read. It should look pretty as if you’re reading a well-written poem.
  • Simple — The code does one thing only and follows the Single Responsibility Principle (SRP)
  • Readability — The code can be read even for people who don’t know the context
  • Testable — The code is runnable on all test

When writing a clean code, there are principles to be followed :

  • KISS (Keep It Simple Stupid) — Write a code that avoids complexity. After writing, you should evaluate the code and think, “Could this be done in a simpler way?”
  • SRP (Single Responsibility Principles) — Write a code that has one purpose. Don’t mix codes that has different functionality into one function or class.
  • DRY (Don’t Repeat Yourself) — Write a code that is unambiguous and representing a single case. This principle is similar to the ones mentioned above. Violation of this principle would create be WET (Write Everything Twice).

To improve the “cleanliness” of your code, there are several important points that you need to apply to your code. Here are the ways with examples from my PPL project to help you write better :

Create meaningful names

Name every variable or functions a name that gives the reader information about what the variable is or what the function is for. Finding a name may take some time, but it will save time for later when other people have to continue your work. Here’s a few example of how we name variables and functions in our PPL project :

A Function for LastModified Component

Rather than making a comment on what the variable is used for, it’s better to name it descriptively :

  • Don’t
Bad example of naming a variable and using a comment to describe it.
  • Do
Descriptive name for fileTest const.

Comment only when necessary

Sometimes a comment is used to explain a bad code, but rather than making a comment, why don’t we rewrite the code into a better one? Ideally, a code should be self-explainable so that a comment wouldn’t be required unless there are a few things other programmer should be know of like important points or the consequences the code will make.

On contrary, if the code is already readable then don’t write any comments :

  • Don’t
Using a comment on a readable function
  • Do
Not using a comment on a readable function

One of the most, if not the most, important thing about this point is to make sure that you don’t leave any code commented if you’re not using it. It’s better to erase them completely than commenting it.

  • Don’t
Commenting an unused code
  • Do
    Delete it! :D

Write a simple yet effective functions

According to the Single Responsibility Principle (SRP), a function should focus on only one thing. It’s also important to write a function that won’t have any side effects so that it only does what the name suggests. In case of anticipating an error, it’s better to throw an exception rather than returning another different codes inside the function.

  • Don’t
    Write a function like stated above :D
  • Do
One function to do one thing only : making a pagination

Layout Formatting

Have you ever judge a book by its cover? Let’s be honest, I think we all have. It’s the same case when we see a code, if it looks messy with no clear structure, then we might lose interest before reading them. To make a neat code, we can use a tool called linter. In this project, my team also use prettier as a tool to fix any inconsistent indentation, tabs, keyword spacing, and maximum length of a line.

prettier and linter work while committing a change in code

Clean code is extremely important when you’re working in a group project. Often times, it’s easier to start than to continue. It must be taken into consideration when working with other people, “how much time will they need to understand this code and pick it up?” In order to make my team mates can continue where I left off in a short time, I try to make the code as clean as possible. Fortunately, there hasn’t been a question asked about what this function or variable for or why a function is working in a certain way. It benefits me too when I try to revisit old work and refactor them. I wouldn’t have to take notes on what the codes do because it’s easily readable. It’s a win-win situation for everyone!

Writing a clean code may take some time, but it will save yourself from potential problems in the future. With this, we can say that writing a clean code is one form of time investment.

Referensi :

csui 18

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store