The Boy Scout Rule

Leave the code cleaner than you found it. – Uncle Bob

The Boy Scout Rule is one of my favorite clean code rule. It takes a different mindset to apply it. The code becomes shared responsibility. You don’t reach out for the blame button of your IDE to check who wrote the code. Instead you look at the code and ask yourself “what can I do to make it a little bit better”. You think about the person who will look at that code after you made your changes and you want to make their life easier. It shows care for the code, kindness for the people you work with and team work.

If all these arguments don’t really speak to you, let me appeal to the pragmatic side of you and give you some more reasons why this is a good practice. Over time code quality decreases. The quick fixes add up and the once beautiful code turns into a mess. If every time you touch the code, you also clean a little bit of the code around your changes, then you prevent this from happening. Getting time to do a big refactoring is not easy. If you don’t take care of your code daily, over time you will start seeing the effects of it. Code will be harder to understand because of all the special cases added to fix certain scenarios. Adding new features will be more difficult and error prone.

Here are some changes you can make:

  • rename variables, methods or classes
  • split methods that are becoming too big
  • create an abstraction
  • remove some duplication
  • remove unused code or bad comments
  • remove coupling

Small changes that continuously improve the code make a big difference in the long term. Instead of asking for time to refactor your code, do a little bit every day and make your and your colleague’s life easier.

All this sounds great. There is one warning that comes with this rule: keep your changes small. It’s easy to start making a small change only to find yourself down a rabbit hole, changing way more than expected. That is not what this rule is about. Keep your changes small and remember to do it every time you touch the code and you will soon see the benefits.


Leave a Reply

Your email address will not be published. Required fields are marked *