Clean Code
This concept and its principles were discussed in Robert Cecil Martin's book "Clean Code: A Handbook of Agile Software Craftsmanship". After this book, the concept of Clean Code has become widespread. However, the concept of Clean Code and its principles exist long before.
Contrary to what is believed, Clean Code does not contain strict rules. Clean Code consists of a set of principles expressing easy understanding by humans and easy modification of the code.
Writing understandable code, whether by the developer of the code or by another developer, means to be understood easily. All codes are written logically and clearly. The connections between different parts of the code are clear and explicit. The task of each function, method, and variable can be easily understood by any developer.
Advantages of Clean Code
There are several advantages provided by Clean Code:
- In case of a change request on the project, required modifications are quicklyupdated.
- Clean Code has advantages like controlling and correcting the problems, maintaining the continuation of the project.
- In case of an error, the faults in the code can be easily detected.
- Maintaining Clean Code is very easy.
- Methods are predictable.
- Thanks to Clean Code, well-documented APIs (interfaces) are available.
- Prevents problems that may arise when working with old code.
- Code is reorganized with less effort.
- It is easy to read Clean Code.
- Clean Code is tested. It is safe and completely functional.
- It provides cost and time savings.
Why is Clean Code Important?
As code not understood by developers increases cost over time, writing Clean Code provides cost savings.
Developers spend most of their time trying to understand the code. This time takes more hours than necessary from each future task. Clean Code increases reliability, flexibility and performance. Therefore, it is an important issue in the software world.
Developers spend their time reading more than writing the code. Developers generally prefer to rewrite the code rather than trying to understand the existing code. This causes a waste of time. A developer who spends time writing well-written, tested, and understandable code saves countless hours, days, and weeks in the future. Therefore, every developer should be interested in the code and make the code understandable as much as possible.
Writing Clean Code
Whether it's JavaScript, CSS or Python; Backend or Frontend development, the goal should be to write Clean Code. Besides understanding the code, it also allows to add a new code in later stages. A well-written code helps to produce better results, and to save cost and time in the long run.
"Clean Code: A Handbook of Agile Software Craftsmanship" written by Robert C. Martin and "The Art of Readable Code" written by Dustin Boswell, focus on a more textual code format; while nowadays Clean Code understanding adopts a wider approach.
There are numerous principles and values. Without these principles and values, the cost of new features increases exponentially. For the Clean Code process, it is commonly said: Think first, then start coding!
What are the Principles of Clean Code?
How to write Clean Code? There are a few basic guiding principles when writing Clean Code. These principles are:
- Writing simple code
- Avoiding unnecessary repetition
- Deleting what is unnecessary
- Writing readable code
Values of Clean Code
Clean Code requires time, effort, attention, and care. The approach of Clean Code is based on four fundamental values:
- Development: Clean Code should be able to develop, adapt, and extend according to needs. Clean Code should easily adapt to conditions.
- Accuracy: The value of accuracy deals with the correct execution of functions, the correct use of classes and methods, and the requirements.
- Efficiency: Code that is easy to read, modify, extend or maintain reduces development time and increases efficiency. The value of efficiency focuses on the effort and costs in software development.
- Continuous improvement: The value of continuous improvement focuses on what improvements should be made in projects and lines of code.
How to Write Clean Code?
1. Name the rules
The first and most crucial step while writing Clean Code is to create rules that ensure all programmers understanding the variables and noting them down. In short, it is necessary to create a Clean Code manual.
The point to consider here is that there's not a single approach that fits everybody. Everyone should find their own system. When working as a team, to create Clean Code, a rule that all team members can understand should be established. For this, all developers must come together.
Tips:
- Do not use single-character variables.
- Do not abbreviate variable names.
- Be consistent.
2. Simplify the functions
Another phase of Clean Code is to simplify the functions to be small and understandable. It is also necessary to give these functions a name that explains what they do and why.
Tips:
- Summarize functions with a short name.
- Divide a function into multiple functions if necessary. Long and complex names make it harder to find the code.
- Command and query functions should be different functions. So do not use the same function for both command and query functions.
- Limit the number of independent variables.
3. Carefully create the comment line
A comment is not to explain what a line of code does, but to explain why a piece of code is created. A clearly defined comment line is a significant element for Clean Code.
If the rules and functions are well-named, comments may not be required. In other words, it is not always necessary to create a comment for each line of code. The situations where comments may be needed should be predicted in advance. Adding unnecessary comments may cause the developer to waste time.
Tips:
- Do not use a comment to explain a behavior.
- Check the accuracy of comments and do not forget to update.
4. Do not forget to test
Another important topic to write Clean Code is testing. Testing contributes to improving the code.
Tips:
- Do not evaluate multiple concepts simultaneously.
- Do not test old codes.
- Create tests to be simple and repeatable.