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 :
Rather than making a comment on what the variable is used for, it’s better to name it descriptively :
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 :
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.
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.
Write a function like stated above :D
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.
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.
Clean, high quality code: a guide on how to become a better programmer
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."…