The Boy Scout Rule

I am reading a book titled "Clean Code". It was written by Robert C. Martin (aka Uncle Bob).
The goal of this book is to explain his method of writing clean code with concrete examples. It is very well written.

One thing I like about reading books like these is that they put words on things that I do or think. They express them in a way I did not think about.

One thing that I have been doing naturally is to follow the Boy Scout Rule. I did not call it that. I did not even know I could give it a name. I did it because I thought it was the right thing to do. Uncle Bob gave it a name and explained it well.

When I work on code, I open up a file and sometimes see a mess. The file's formatting is wrong. The class is just one big method of 100 lines.
Whatever the case, one of the first thing I do before I change it... I clean it up. I fix the formatting first. I remove all the tabs, make sure it follows a single style.

Then, if I have trouble understanding the code, I refactor it a bit.

But... I could break the code. Introduce new bugs. So how can I clean it up and be confident it still works? Even before I clean it up, I create unit tests (if none existed). If they existed, I run them to make sure they run now (before I make any changes).

The Boy Scout Rule (or Law) is to never leave a camp without first making sure it is cleaner than it was when I got there.
"Always leave the campground cleaner than you found it."

Why do you think we MUST follow this rule? Why would we (programmers) care?

Because code is like an apple. It rots. It degrade. And the more bad apples there are around it, the faster it will degrade.

You don't want your code to become a mess, do you? I hope you don't!

So, in your quest to become a better, professional programmer... why don't you start (or continue) to follow this rule?!? :) I dare you! :-D

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote>
  • You may post PHP code. You should include <?php ?> tags.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options