DEV Community

Cover image for Bar Programming #03 — Clean Code Chapter 1
Thomas Lincoln
Thomas Lincoln

Posted on

Bar Programming #03 — Clean Code Chapter 1

In the today post, I will write a resume of what I learn and understood about the first chapter of “Clean Code”, probably I will write more texts like this about the other chapters. So, if you have interest in Clean Code or want to understand a little about, you are more than welcome to continue reading this possible series.

The Clean Code book was written by Robert Cecil Martin, aka Uncle Bob, in this book he covers technics, rules, and ideas. Which aim to improve the readability of codes and to improve the efficiency of the development.

In the first chapter, the following topics are covered:

  • What is a Bad Code?
  • Which is the cost of have a bad code?
  • What is Clean Code?
  • Recommendations

What is a Bad Code?

To explain the importance of a good code, the Uncle Bob quotes an example of an application developed in the 80s, that became some popular. But, interval between the updates start to increase, the bugs were not fixed, and the time of loading and crashes start to increase so much. Until it reaches a point, it was unusable.

After a time, he meet one of the workers, and found that the reason behind this chaos, it was as expected. The code start to became incomprehensible, more and more makeshift, so that it was not possible to update or do maintenance.

Why we write a bad code?

But, why? Why we write a bad code? The bad code has power to retard us, a good possible reason can be the hurry, because, we have deadlines to finish our tasks, the demands need to be completed, we can't lose our time cleaning the code.

Or same, the wear and tear of working in that project is turning big, to the point what we want just end this once and for all, and throw it aside.

The cost of having a bad code

We can see the cost of having a bad code, when we read someone else's code, and the fact that it is confusing, makes us work more slowly, or same, our own code can produce the same effect. Even when we start fast, we can suddenly to perceive that we are more and more delaying to achieve progress.

This happens, because these errors are like a snowball, the more we leave aside, the more we postpone the cleaning time, the slower it will our progress. According to the author, these bugs make the team productivity get closer to zero.

Therewith in mind, the author says that even if we think “I need to fulfill the demand, if not I will be fired”, we need to communicate to our superiors, and to warn that such a demand need more time. We need to protect our code, we need to defend that it requires more attention, so that so, don't happen the same thing that happen with the press before mentioned.

What is Clean Code?

Let's say you believed in me, that a bad code is an obstacle for the creation of a software, and think you want to develop a good code, so, how to write a clean code?

It should be noted that write a clean code, is not as simple as flipping a switch in our heads, is necessary to have a sensitivity in relation to the code. Understand which are the things that are turning our code in a bad code, and which technics can be applied to resolve.

Write a clean code is like to draw, be able to evaluate if a drawing is good or not, don't make you able to draw, so, is necessary to have knowledge of technics and strategies.

So, going to the main question, we can summarize a clean code in a code that follows that four rules:

  • Without duplication, do not exist any repeated features.
  • One task, the code must have only one function, and your functions too, everything must do only one thing.
  • Expressiveness, the code must be very clear in relation with the name of the things.
  • Little abstractions, this rule involves the creation of methods and classes that encapsulate specific features in a clear way, without becoming excessively complex or multifunctional.

In general, we can describe a clean code, paraphrasing Ward Cunningham:

A clean code, he makes exactly what you think, is like if the language was be made for make that thing, nothing surprise you, everything is how you expected.

Recommendations

Of course, the rules to make a clean code can vary too much, don't exist only the rules mentioned in the “Clean Code” book, because, this is the author vision. Of course, his vision is supported and used for many people, but still, not all will think that this is the better way to make a clean code. But the author recommends, that we read it, learn with him and decide for ourselves.

So, he says that for him and your colleagues, these rules are the best, but, don't recommend that we think in this way, because it is possible that we can reject one or other rule.

Uncle Bob also says to us, that we have the responsibility to write a code that is easy to read, in the end, while we write a code, a good part of the job is read another codes. When we are reading, we want this reading to be easy to understand. So, we need to write a code thinks that someone will read it, aiming to write something that is easy for other people to read.

Finally, the Uncle Bob also say, to us follow boy scout rule:

Make the area of camp better that when you find it.

The idea behind this recommendation, is that instead of we only write a clean code, we must do maintenance. In other words, we must constantly think in clean improvements to our code, and this doesn't need to be a big thing, change a name of a variable is enough.

Conclusion

With this, we can understand the need and importance to have a clean code, besides understanding what is a clean code. In the next chapter, we will understand how we can give better names to our children (variables and functions).

Top comments (0)