Clean Code is a software development philosophy that consists of applying simple techniques that facilitate the writing and reading of code, making it easier to understand.
When did the term Clean Code come up? Clean code techniques first appeared in the book "Clean Code: A Handbook of Agile Software Craftsmanship", released in 2008. It was written by Robert Cecil Martin, known in the community as Uncle Bob. The author has been working with development and programming since 1970 and is one of the professionals who led the Agile Manifesto in 2001.
What is considered Clean Code for? A code with many adjustments for a long time becomes impossible to maintain, so it is better to create a new code and not to continue with a problematic version. A clean code avoids unnecessary expenses and prepares the software for updates and improvements.
Know the 7 main rules of Clean Code
Uncle Bob's book lists good practices for clean code. The main ones are:
1 - Names are important The name definition is essential for the understanding of a code. Here it does not matter the type of name, be these: Variable; Function; Parameter; Class; Method.
When defining a name, it is necessary to keep in mind two main aspects: It must be precise and deliver the central idea. That is, it should get straight to the point; Don't worry about big names. If the function or parameter needs a long name to demonstrate what it really represents, that's what you should do.
2 - Boy Scout Rule There is a principle that states that, if you leave the area you are camping in, you should leave it cleaner than when you found it. If we bring this rule to the programming world, we can adapt it as leaving the code cleaner than it was before editing it.
3 - You must be the real author of the code Human beings are used to thinking narratively, so code works the same way. This is a story and, as programmers are its authors, they must be concerned about how this story will be presented. In short, to structure a clean code, it is necessary to create simple, clear, and small functions. There are two rules for creating the code narrative: Functions must be small; They must be even smaller. Do not confuse the terms "name" and "function". As we said in the first principle, big names are not a problem, but functions are.
4 - DRY (Don't Repeat Yourself) This principle can be translated as "don't repeat yourself". An expression, which was first described in a book called The Pragmatic Programmer and applies to areas of development, such as: Databank; Testing; Documentation; Coding.
DRY defends that each part of a system's knowledge must have a unique representation and be free of ambiguities. In other words, it defines that no two parts of the program can perform the same function.
5 - Comment only what is necessary This principle states that comments can be made; however, they must be necessary. According to Uncle Bob, comments lie; and this has a logical explanation. What happens is that, while codes are modified, comments are not. They are forgotten, and therefore, they do not portray the real functionality of the codes. So, you know; if you are going to comment on the code, let it be only what is necessary and let it be reviewed together with the version of the code that accompanies it.
6 - Error handling There is a phrase by the author Michael Feathers, well known in the web development area, which states that things can go wrong; but when this happens, the programmers are responsible for ensuring that the code continues to do what it needs to do. In other words, handling exceptions correctly is a big step for the developing programmer.
7 - Clean tests Performing tests in the programming area is a very important step. A code is only considered clean after being valid through tests, which must also be clean. For this reason, they must follow some rules, such as:
Fast: The test must be fast, allowing it to be performed many times and at any time.
Independent: It must be independent, in order to avoid causing cascade effect when a failure occurs, which hinders the analysis of the problems; Repeatable: It must allow the repetition of the test, many times and in different environments.
Self-Validation: Well-written tests return true or false answers so that the error is not subjective.
Timely: Tests should strictly follow the timeliness criteria. In addition to this, they should ideally be written before the code itself, as this avoids it being too complex to perform the test.
Clean Code is a concept that came and stayed. Its principles effectively solve one of the main problems that most system projects face: maintenance.