Tidy Up Your Code : Clean Guide to Clean Code
Sometimes, all we care about our codes is whether it works. Using random names of variables, many unused comments, inconsistent typing, and other “dirty code” things can be commonly seen in early programmers’ works. Because who cares about all that when the code itself works? But sometimes, as a programmer, we forget that we spend more time reading codes than writing code! We must spend more time reading documentation, understanding the code, debugging, and conducting stack-overflow searches. Moreover, when it comes to a group project, we will do code reviews and continue others’ code in which readability is critical. Thus, in software development, especially which requires many programmers, clean code implementation is a must.
Code is clean if it can be understood easily — by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility, and maintainability.
Indeed, to get familiar with clean code, there’s no better way than keep practicing. To help you better practice, here are some rules you can do to improve your clean code!
Follow Standard Conventions
Every programming language has its conventions which were later considered the best practice. Code conventions weren’t made for nothing. Following standard code conventions will improve readability and allow other engineers to understand the code more quickly and thoroughly.
Naming Rules
Use descriptive and unambiguous names to make it more meaningful to name your variable. Use pronounceable and searchable names to make it easier to understand. Avoid encodings, don’t append prefix or type information.
Keep the Function Simple and Stupid!
Make sure every function is small and only done one thing and has no side effects on other functions. Like on naming rules, use descriptive names on your function to make it easier to understand. The fewer arguments, the better your function will be. Also, avoid using flag arguments and split the method into several independent functions.
Comment Rules
Sometimes we use the comment to explain or ocde. But, to reach clean code, always try to explain your works in code, not in the comment. Just use comments as an explanation of intent and to clarify your code. Commenting sometimes can result in redundant and noticeable noise, so use as slight comments as possible. Never use closing brace comments and never comment on your code. If your code wasn’t not intended to be run, remove it.
Neat code structure
Clean code also closely related to how the code is structured. Always separate concepts vertically, and make sure related code appears vertically dense. To increase readability, declare variables and dependent functions close to their usage. Keep your line short of making it readable, and don’t break indentation. If there is any white space in your code, better use it to disassociate weakly related.
Code Smells
One of the indicators we can use to see if our code is clean enough is through seeing how much code smell we have. Code Smells is any characteristic in the source code that possibly leads to a deeper problem in our program. There’s several things described as a code smells — first, rigidity or how a software is difficult to change. The next is fragility which means any single change might cause an error in other place. Code smells also means needless complexity and repetition. Last but not least, a code that hard to understand might lead to code smell as well.
In order to detect code smells, thanks to sonarcube we can identify code smells in our software with suggestion to solve it as well.
All and all, it’s not really hard to implement clean code in our project. It’s just the matter of time and practice. In my experience, using clean code really helps me to understand what am i actually do and debugging. Aside from things i explain in this blog, there are some more ways to improve code cleanliness, but using those things really helps!