A very first guideline for clean code beginners
Author: Justin Khanh – UPP Global Technology
"Working code" is not necessarily good code. Code needs to be easy to understand so that the issue you want to further investigate by coding can be digestible. No matter how well your program runs, it is inevitable for your code to be rechecked and changed at some point. For example, it requires new features, rare bugs fix, or just simply in-depth clarification of the way it works. Similarly, you will have to read others' code to do the same. And in the case of a bad piece of code, it takes you longer to understand what coders are doing at the time. Comprehensively, things get mixed up when an artist cannot understand his own work because his code is not regarded as clean code.
So, what is clean code?
Clean code is not about whether it works, but whether it is easy to interpret. Specifically, during the working period, a developer spends quite a lot of time reading and understanding code. Hence, it should be comprehensible to boost productivity and time efficiency. Ultimately, the worst scenario is that entire code bases can die when it makes sense to no one.
The backbone of clean code
So as to be regarded as "clean", it has to be, firstly, readable and understandable. Furthermore, with clean code, developers can reduce the cognitive load they have to go through during their work. Besides, clean coders' outcomes should also be concise. Another crucial point of clean code is that it should avoid unintuitive names, complex nestings or big code blocks. Last but not least, only when code follows common best practices and patterns can it be understandable and considered "clean".
This is the opening of a multi-part blog post that will go through all these backbone areas of clean code. However, more than just challenges, we are going to dive into several solutions for clean code writing!
Naming things correctly and in a way that's understandable is a hugely important part of writing clean code.
Indeed - if the bad name is chosen - almost all the other concepts taught in the course won't help much.
Comments & Formatting
You might think that comments make code readability easier. In fact, the opposite is usually the case though.
Proper code formatting (i.e. keep lines short, add blank lines, etc) on the other hand is very helpful for reading and understanding the code.
Functions (& Methods)
Functions and methods are at the core of any piece of code we write. After all, all our code is part of some function or method. And we use functions to call other functions, build reusable functions and more. That's why it's so important to write clean functions.
Conditionals & Error handling
You will almost certainly use control structures in your code, such as if statements, for loops, possibly while loops, or switch case statements change, regardless of the type of application you are developing.
You should utilize control structures because they are crucial for organizing the flow of code.
Control structures are crucial when designing clean code because they can also result in poor or suboptimal code.
Classes, Objects & Data Containers
There are a few guidelines and ideas you should be aware of while working with classes and objects and creating clean classes and objects.
1. Data structures and objects should be distinguished from one another.
2. Take into account polymorphism
3. Classes should be small.
4. There should be strong cohesion in the classes.
5. Be mindful of the "Law of Demeter"
6. Write SOLID classes, please
To end this blog, there are core practices used by experienced developers to write clean code.