Sometimes when we write a piece of code, it may be tempting to only write a code that works and not care about its readability. At first glance, spending more time on writing a good clean code may seem time and effort consuming, but in reality, clean code can actually offer a lot more than meets the eye.
Back when I was just a beginner to programming, my only objective was that my code could do the things that I needed it to do. I didn’t care about being organized with my code and had a bad habit of writing my code sloppily. Because of this, my friends and project partners would often be confused when they had to read my code. Sometimes, even I would also struggle to read my own code. This would also cause me to consume a lot of time to debug any errors. After a while, I learned about clean code. By using clean code, I actually saved a lot more time and effort in the long run.
OK, so what is clean code?
A common definition of clean code is code that not only works well, but is also well written. Clean code should be easy to understand and modify. The code is not only understandable to its writer, but also to anyone else. Your friends and coworkers should be able to understand and modify your code without you having to explain anything to them. Clean code comes with several other benefits, such as increased code maintainability, easier to refactor and extend, and also less time needed for debugging.
Characteristics of clean code
Clean code should have low coupling and high cohesion. Coupling refers to how dependent your code is to other modules. Low coupling is necessary because when you need to change a certain part of your code, it won’t cause any side effects. Cohesion refers to how well the elements in a class or module belong together. High cohesion is needed so that the code structure is simpler and easier to understand.
Classes and functions in clean code should be made as small and compact as possible. Smaller classes and functions are intended to make them have a more defined and clear purpose. They should only do one thing and do it well. Bigger classes and functions tend to do a lot of things, when they can actually be broken up into smaller pieces.
Another characteristic of clean code is that there are little to no comments. Our code should be clear enough that it can explain itself without the comments. When you add comments to explain code that is already crystal clear, they really don’t add any extra value. Comments should only be used in necessary situations, such as noting how important a function is or clarifying why a certain value is chosen as the default value.
Some clean code principles
KISS (Keep It Simple Stupid)
Simplicity is a must in clean code. Complex functions and unnecessary lines will only lead to code that is hard to read, change, and maintain. Whenever you write a new piece of code, you should always ask yourself “Can I make this simpler?”.
A misconception that some people have about code simplicity is that shorter is simpler. This is something that some people use to justify using single alphabet variables such as x, y, z. In reality, it doesn’t work like that. The main reason we need simplicity is to maintain readability. If your code is short but extremely hard to read, what’s the use of it.
DRY (Don’t Repeat Yourself)
Don’t Repeat Yourself means that we should steer clear from duplications. When you want to change a certain part of the code that has a duplication, you will also have to change its duplications. In this type of situation, there’s always a possibility that we forget to change these duplications, creating an error prone case. My trick to getting rid of these duplications is to make them into a function and call this function whenever needed. This way instead of having code duplications, we have a reusable piece of code.
How to keep your code clean
Use Meaningful Names
As stated above, clean code should be able to explain itself without additional comments. One way to achieve that is by using meaningful names when creating variables and functions. Meaningful names should be intuitive and can clearly describe what the intention of the variable/function is.
The name of the variable below is concise and clearly states what it is. It is a styled form label, hence the name StyledFormLabel.
Create Single Responsibility Functions
Single responsibility functions are functions that only do one thing. To know if a function does one thing or more is by checking if the function can be split up into other classes. If it can be split up, that means your function still does more than one thing. If it can’t, congratulations you have a single responsibility function.
The function above only does one thing, which is to create a logout button. It can’t be split up into a smaller function, which is why you can tell that it is a single responsibility function.
Each programming language has their own set of naming conventions. For example, Java uses camel case (logoutButton), whereas Python uses snake case (logout_button). These naming conventions help maintain consistency within your code. It also makes it easier for you to remember your variable names.
Other than using standard naming conventions, in order to keep your code consistency, use one word for names with similar functions. For example, get and fetch have the same meaning, so just pick either one for your names.