Clean Code : Investing Time within Codes

Audilla Putri
6 min readApr 3, 2021

--

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. Rather than making a comment on what the variable is used for, it’s better to name it descriptively. For example, this is how we name a variable that’s used to store current date.

  • Don’t
Bad Example of Variable name
  • Do
Good Example of Variable Name

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
Bad Example on How to Use Comment
  • Do
    Name your variables and functions accordingly.

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 on 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
    Write function with one purpose. For example, this is how we write a function to display pagination. It focuses only on showing the pagination element.
Write a function with only one purpose

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

Benefits of Clean Code

  • Easy Maintenance — By having a clean code, that means you’re following the best practices. With best practices, lies unwritten documentation that’s easy to understand. So in the future, whenever you feel like visiting old work to do some modifications, you won’t have to think hard on what some line of code does. You know what this leads to? Yup! A better use of your time.
  • Clearness — Imagine if you encounter a bug and you have to find it among hundreds of lines of your code. This task will be heavy to carry if you have a messy code with ambiguous name or multi-purposes function. Having a clean code will help you find what’s wrong in your code faster. Additionally, when working as a group, there will be times where other members will see your code. Clean help will help your team mates to understand what you write better
  • Personal satisfaction — When you code, you don’t code just for your own private view. You may code for an assignment or for a client. If you write a code that looks pretty and simple, you’ll feel more confident to share it with others. Your code will reflects your professionalism and might show that you enjoy working on it

Conclusion

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?” 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 :

--

--

No responses yet