Hello World!!!

It is a great pleasure to talk to you about this subject: Clean Code.

Who works programming should understand how it is important and how it impacts on productivity. A messed code can be a demotivator and make the programming duties pretty boring. I believe that a good programmer should be worried about it.

Robert C. Martin, the Uncle Bob, wrote a book about Clean Code and it will be my reference to this post.

Bad Code

Did you ever have to:

  • Implement a huge messed code so you could deliver a product on time?
  • Spend a lot of time trying to understand a senseless code and, in the end, you still couldn't?
  • Stop the work because you had been impeded by bad code?
  • Add more features in a bad code until it was not possible to manage it any longer?

If you work developping apps then these scenarios can be familiar to you. You know that a messed code can make the app unstable, and one minor change can break other parts of the code.

So why we write bad codes? Do we think that we have no time? Do your boss pressure you?

Hey, Programmer!! Defend the code with passion.

You are responsible for it.

Say the truth.

You will be unprofessional if you don’t warn your manager about the risks of making messes.

Effects of a bad code

If you think that one day you will go back and clean it up later you should be aware that usually it won’t happen. If it is really necessary to the deadline then you should have a good plan to guarantee that it will happen very soon.

LeBlanc’s law: Later equals never.

The messed code affects the productivity of the team. The book shows common solutions used by managers:

  1. First solution: add more staff to the project. But that new staff don’t know the app.
  2. Second solution: redesign.
    1. New situation: One team to the new architecture and another to maintain the current app. The first team must build a new app that does everything that the old app does and they have to keep up with the changes that are continuously being made to the old app. Management won't replace the old app until the new app can do everything that the old one does.

Now you understand what uncle Bob says:

“Spending time keeping your code clean is not just cost effective; it’s a matter of professional survival”.

The only way to go fast is to keep the code as clean as possible at all times.”

What is a Clean Code

Many times we recognize a dirty code but we don’t know how to write clean code! It requires “code-sense”.

A programmer who writes clean code is an artist who can take a blank screen through a series of transformations until it is an elegantly coded system.”

Uncle Bob gets some specialist’s opinions about that and adds in his book:

  • Bjarne Stroustrup, inventor of C++: I like my code to be elegant and efficient. The bad code tries to do too much, it has muddled intent and ambiguity of purpose. Clean code is focused.
  • Grady Booch, author of Object Oriented Analysis and Design with Applications: Clean code is simple and direct.
  • Dave Thomas, founder of OTI, god father of the Eclipse strategy: Clean code can be read, and enhanced by a developer other than its original author. It has unit and acceptance tests.
  • Michael Feathers, author of Working Effectively with Legacy Code: Clean code always looks like it was written by someone who cares.

Also in his book, Uncle Bob remember something very important about “The Boy Scout Rule” that we can use in day by day when we are programming:

Leave the campground cleaner than you found it.

Then, leave the code better then you found it. It doesn't matter if you are not the author. Don't be afraid to refactor.

Conclusion

Clean code is an important subject that I really like to explore. It is part of every programmer’s day.  If you are a programmer you cannot neglect this subject.

I highly recommend you to read the book.

See you!!!

Reference


Summary