Clean Code: Eye-relaxing code
Introduction
After discussing how do we define a good-looking system design, I will show how to structure a good-looking code on your project. A more common term on the developer world is clean code. In large projects, clean code is necessary so that fellow developers can easily read, understand, and add to. Anyway, we need a simple, yet comprehensive definition of what clean code is. So, without further to do, let’s talk about what clean code is.
Features
I learn this definition of features from the Advanced Programming: CSCM602023 course I enrolled in the 4ᵗʰ semester. These features refer to the additional learning source, refactoring.guru, which consists of 5 compulsory features:
1 Clean code is obvious for other programmers: you should minimize poor variable naming, bloated classes and methods, magic numbers, and any other parts of your code that reduce its tidiness.
2 Clean code doesn’t contain duplication: a.k.a DRY (Don’t Repeat Yourself) which reduces code patterns, avoiding repetition by using abstractions.
3 Clean code contains a minimal number of classes and other moving parts: this is parallel to the 3ʳᵈ feature, as less code means less maintenance, fewer bugs, hence it is easier for the developers to maintain.
4 Clean code passes all tests: you know your code is messy when the percentage of successful tests is not 100%.
5 Clean code is easier and cheaper to maintain: ease of maintenance means a low budget allocation for maintenance, too.
Practical Steps
These 5 features are yet still abstract, so there are steps, referring to the Gojek Engineering blog, on how do we apply simple clean code.
1 Variables: meaningful variable names ease maintenance, it helps the developer find bugs.
2 Functions: the modularity of a function is a must, also remember not to make function duplications.
3 Comments: use comments when the function/module name is not enough to communicate how does the function work.
4 Exception Handling: ensure every function has it’s own try and catch blocks, again, it eases debugging.
5 Code organization and design: (1) code from left to right and top to bottom, (2) local variables placed as close to its desired scope, (3) instance variables placed at top of the class, (4) function caller should be above the callee, and (5) The Principle of Least Surprise.
This is the end of my blog with the topic of clean code. I am open to suggestions in this story’s comment section. Cheers!