Clean Code: Tips to keep your code clean

React JS

Clean Code: Tips to keep your code clean

Today we will talk about a subject that I really like, which is the Best Seller Clean Code by Robert C. Martin, better known as Uncle Bob, a book that is a classic in the programming area. In it, I learned valuable lessons about some practices and how to keep our software as healthy as possible and these lessons complement the SOLID principles.

It is important to note that this article is only an overview of the contents of the book and that I strongly recommend that you read it.

cleanCode-book.jpg

What is Clean Code

In his book, Uncle Bob reports that with his many years of experience, he realized that the biggest problem in software development was in the maintenance of the code. Therefore, a code that was born written in a bad way and that can work, in the future, will cause problems and generate losses.

That said, Clean Code is not a design partner but a development philosophy and its goal is to make writing and reading code easier, with simple techniques that in the end make code easy to understand.

But how to use Clean Code?

Most of a programmer’s work is in maintaining and reading code, this consumes many more hours than developing a code from scratch. And therein lies the key to a programmer’s productivity, in his book Uncle Bob states that “The ratio of reading and writing code is 10 to 1”. This means that programmers spend more time reading than coding new things.

Within this context, Clean Code applies precisely to new developments and maintenance of existing code, in order to prevent the code from being recommended for a long time, making it difficult to maintain, and in these cases, it is better to throw it away and start over. Then proceeding with bad code.

Clean Code general rules

I have listed some good practices that are listed in the Clean Code book and that will help you to have a clean code.

Hold tight to conventions

Staying true to the conventions you or your team have set for a project is essential. Because of so many variable names and patterns, functions, parameters, enumerators, classes, and methods are very important for the project to proceed! It doesn’t matter if it’s capitalized or if it has any prefix, just keep the default.

But the important thing about setting a standard is that it should be accurate and get your main idea straight and don’t be afraid to have big names. It’s better to have something well described than something that takes time to understand.

Scout rule

Maybe you’ve heard that expression “When you leave, keep the camp cleaner than when you arrived”. The same applies to our code, whenever you finish your change, make sure you left it better than you found it.

Solve the problem

Always try to solve the problem definitively and never solve it superficially. We know that in the day-to-day rush, we tend to not solve problems the way they should be solved and this often leads to rework.

Always try to identify the real problem and do whatever it takes to definitively solve it.

Comment only what is necessary

With the dynamics of constant code maintenance, comments end up being left behind, and in this way, they fail to portray what is actually being done in the code.

Therefore, this principle says that comments can be made, but only if they are something necessary and that it is reviewed along with the code.

KISS

This concept I’ve met elsewhere, but for this context, it’s totally valid and it says Keep it Stupid Simple or “Keep it stupidly simple”. It’s a good way to try to see the solution of problems starting from the principle that everything should be simpler without trying to complicate things.

Error handling

We know that in development things can go wrong, but when they do, we are responsible for ensuring that the program continues to do what it needs to do. So handling exceptions well is a very important part of the process.

Clean tests

Part of the development process are the tests, this is a very important step, where a code can only be considered clean after being properly validated through tests and that must be clean.

There are some rules for that, which are:

  • Fast — It must be fast and can be performed as many times as necessary.
  • Independent — Must be independent in a way that avoids a ripple effect when a failure occurs.
  • Repeatable — Repeat tests and different environments.
  • Self-Validation — Tests with true and false answers, to avoid ambiguity.
  • Timely — They must follow the punctuality rule and preferably be written before the code itself.

Other important points

There are other points that are important to have a clean code and they are:

  • Keep configuration data at a high level;
  • Use polymorphism in place of IFs;
  • Use Async as a suffix;
  • Avoid unnecessary settings that you haven’t defined yet;
  • Use dependency injection;

Conclusion

Clean Code is a very important concept, even more so when looking at agile development methodologies. The Clean Code principles solve many of the problems we encounter in most projects and together with the SOLID principles, it becomes mandatory for any programmer who wants to take it to the next level.

I hope this post was useful to you and if you liked it share it with your friends and don’t forget to access other content on the blog.

References

Explore mais