Writing Clean Code
Writing clean, understandable, and maintainable code is a skill that is crucial for every developer to master.
What is clean code and why do we need it?
There are many examples of bad code bringing a disaster to a good product. For example, space rockets being mislaunched due to a badly transcribed formula, or a bug in a medical machine that may cost the lives of people.
The quality of the code is directly related to the maintainability of the product. As features are added and changes are made, time passes, and the developers might forget some details of the project, or if the code quality is not good, changes become increasingly risky and more complex.
The programmer's target audience should not be the computer, it should be other programmers. Studies show that the ratio of time spent by a programmer reading code to a programmer writing code is normally 10 to 1.
Writing clean code makes the code easier to understand and is vital in creating a successful product.
Is it hard to write clean code?
Writing clean code is hard work. It needs a lot of practice and focus.
The hardest part is starting to write clean code, but once your skillset improves, it becomes easier. Writing clean code is all about readability, so even the smallest things like changing your habits for naming variables make the biggest difference.
Naming Variables
Use a name that gives information about the variable, so another can understand its meaning. This will save time in the future and when you revisit your code a few months later, it will be easier for you to understand your code.
Try avoiding one-letter variable names. But in a loop, one-letter variable names are okay to use.
Comments
Comments help people to understand the code later, and they help other programmers to work on the same project. Comments in the code mean that maybe your code is not self-explanatory.
Comments can quickly show what a complex function is doing, or maybe even explain why certain things need to happen in a certain order. However, too much commenting can have a negative effect and actually make for bad code.
Avoid Large Functions
Functions should be small, really small. The longer a function gets, it is more likely to do multiple things and have multiple side effects. The functions should do only one thing, not multiple things.
When a function is larger, the good practice is to separate it. This will make the code clean, easy to understand, and also reusable in the entire code.
For example, if we need to add and subtract two numbers, normally we can do it with a single function. However, the good practice is to divide them into two functions. Then they will be reusable in the whole application.
Fewer Arguments
Functions should have fewer arguments, the fewer the better. Avoid using three or more arguments where possible.
Arguments make the code difficult to read and to understand the function. They are even harder from a testing point of view because they create the need to write test cases for every combination of arguments.
Avoid disinformation
Be careful about words that mean something specific. Do not refer to a group of books as book_list. Even if its type is a list, “books” is a much simple and better name.
//bad practice
var book_list = [];//good practice
var books = [];
Code Repetition
This means that a code block is repeated in your code more than once. Therefore, the code block needs to be extracted into a function. Duplicate code means you need to change things in multiple places when there is a logical error.
Capitalize Constant Values
This is another convention that we need to follow. Always use fully capitalized names for constants.
const DAYS_IN_A_YEAR = 365;
Commented-out code
Others who see the commented-out code will not delete it because they do not know if it is there for a reason. That code will stay there for a long time. Therefore, just delete it.
“Anyone can write code that a computer can understand. Good programmers write code that humans can understand.” -Martin Fowler
Clean coding is not a skill that can be mastered instantly. It should be a habit that needs to be developed by following these principles and applying them whenever you write code.