Making Life Easier With Clean Code
If I give you 2 rooms and you have to pick one of them quickly, which one would you choose, the clean one or the mess one? That’s an easy question, right? Of course you (or even everybody) will pick the clean one. The reason is clear, who likes the messy room? No one! Is it comfortable? Of course, no! Is it maintainable? Hmmm, not really. Those room will only make your life become harder.
Just like code, clean code is something that all of programmers want. Because if the code is not clean, it will be hard to understand, difficult to maintain, give a lot of bugs, and even have some impacts on business. It’s terrible, right? When we write a code, we want it to solve a lot of people’s problem. But, how can those things happened if the code that we write somehow turns into our problem and the other programmer who use it? That is why clean code is very important.
To implement clean code, it may need some times and practices. But once you already master it, the benefits are enormous. The inventor of C++, Bjarne Stroustrup, said that clean code makes your code elegant and efficient. By implementing clean code, your code will be fast and easy to understand whether it is for you or someone else. The logic are straightforward so the bugs are easier to find. Also, the maintenance will be easier and cheaper because the dependencies are minimal. Apart from this, there are still a lot of benefits that we can get from clean code.
Now, how to implement clean code? Here are some principles of clean code that can make your code cleaner.
Use Meaningful Names
In a code, there must be so many names, whether it for classes, variables, or methods. If you want to make those elements are easy to identify, you have to give them a meaningful name. But, how to give a meaningful name? What are the criteria? Here they are.
Use Intention — Revealing Names
Give a name that represent something that the function does! As much as possible, the name should be able to give the other people an explanation of what is being done inside of the methods or classes without having to look into the implementation details. Use nouns as names for classes and verbs as names for methods.
Here is an example of bad naming for method.
With a name like that, people who read it will be harder to know what is being done inside of that method. So, to configure it, they have to look at the implementation details and it requires time.
You should write the method’s name like this.
For variables, you have to give a name that represent something that stored inside of it. For example, you want to store a string of someone’s nick name into a variable.
Don’t write like this.
But, write like this.
By using intention — revealing names. It will be easier to identify the purpose from methods or variables without explore them further, not only for us, but also another programmer who read our codes.
Use Pronounceable and Searchable Names
Give a name that easy to pronounce and easy to search! This means the name must be easy to read, easy to be typed, and easy to be said. The first thing that you have to do is write the name in a camelCase format to make it easier to read. For example, you want to store an Integer of someone’s salary per month into a variable.
Don’t write like this.
But, write like this.
Second, don’t use bad abbreviations. Bad abbreviations can make the name become harder to type, harder to be found, and harder to be said. For example:
By using pronounceable and searchable names, it will be easier for us to read or find our methods and variables. So, the developing process will become faster.
When you write a function, it has to be small. This means your function will only do one job (or only has single responsibility). By having only one responsibility in every function, it will make them easier to maintain. But, why?
A function that has many responsibility will be very complex. When you change one of the functionality of that entire function, the others will be affected. Same with the bugs, if you have a bug in one of the functionality of the entire function, the other will also be interrupted. So that is why it will be harder to maintain.
But if you assign each of your function with only one responsibility. When there is a change, you only have to focus on a function that responsible with that change, and it will not affect the other functions.
Also, if you want to make your function small, use less line of codes! For example in my case, I want to make a function that will return a list of menu from one store. Here is the code.
That function has 5 line of codes. But actually, we can make it less. Here it is.
By writing small functions, it will be easier for us to maintain those functions. When there is a bug, we can easily find the method that is responsible for that bug. So, the development process will be more efficient.
Don’t Repeat Yourself (D.R.Y)
When you write a code, make sure that the code is only in one place. So, once there is a change, you don’t have to do the same thing in another place. For example, I want to create a function to delete all menus in one store. But before that, I have to list all of the menus that I want to delete. So, here are the codes.
There are two functions, the first function is to list all menus in one store and the second is to delete the menus. That picture shows that the codes for list all menus are in more than one place. This is not clean. Once there is a change of code in the first function, you also have to change the code in the second function.
So, to fix it, you can turn those codes into this.
By calling the first function in the second function, once you have the change in the first function, you only have to focus on a first function. The rest will follow.
Make Clear Comments for Guidelines
Last tips from me, when you create a code, provide comments on the beginning of every function and every important commands. The comment should not be too long, but the most important thing is to provide clues for anyone who reads them. Here is the example.
By making clear comments, it will be the guideline for the other people who read your codes. It also can help you once you want to develop your project again after not opened it for a long time
That’s all from me, hopefully it is useful. Happy coding!!!
Martin, R. C. 2009. Clean code: a handbook of agile software craftsmanship. Pearson Education.
Perhatian Programmer! Inilah Pentingnya Menerapkan Clean Code | Berita | Gamelab Indonesia
Mungkin teman-teman programmer sudah pernah mendengar istilah clean code, tetapi kita harus tahu betapa pentingnya…
Panduan Pemrograman PPL 2021 | Scele Fasilkom UI
Guest Lecture’s Notes