Disclaimer: No boy scouts were harmed in the production of this post
The Boy Scout Rule is in my opinion a highly valuable mechanism for ensuring that a code base remains effective over time. The Boy Scouts of America have the rule “Leave the campground cleaner than you found it.”* Applying this to the code as it is maintained provides an efficient mechanism for ensuring code quality. The marginal effort required is relatively low as you are already working with it and hence the effort to understand the purpose and structure of the code is already required.
At this point I want you to imagine a boy scout cleaning a camp site while under the influence of significant quantities of amphetamines. Such a hypothetical boy scout is going to get their camp site very, very clean. And probably clean other camp sites as well even if, for instance, there are still people sleeping in the tents being packed away. Now that you’ve imagined all of this, you have some idea of the reputation I have for editing existing codebases.**
Continuous cleaning of a codebase is beneficial over the lifetime of a project but it must also be recognised that there are costs involved. In particular the additional changes complicate source control activities and require additional communications within the development team. There is also the inherent risk involved in any change to a software systen, The existence of these costs doesn’t mean that you should not improve the code as you go. It does however mean that you should be measured in how you do so.
When joining a project I will often go through a lot of the code and “tidy” it. This may involve cosmetic changes such as applying consistent formatting and removing unused namespaces. It may also involve restructuring of logic like reducing the level of nesting and extracting methods. I find this helpful in getting a handle on the structure of a system. Sometimes I may touch over half the files in a system. What I have learnt however is that many of these changes should be undone using the magic of source control. Generally I will keep a few key modifications related to the features on which I am working and discard the rest. This is not wasted effort as I have gained a better understanding of the system from the changes. Additionally many of the changes are made with automated tools and hence can be repeated efficiently later.
By distributing the changes over time the costs and risks involved are dramatically reduced. This is a good thing, even if it does go against my fundamental nature. My challenge now is not to block my inner boy scout but to ensure that he stays in rehab.
* I have no idea if this is true in Australia as during my brief time in the scouts I was The Worst Scout in the Entire World™
** And I do it all without drugs. Apart from my hopeless caffeine addiction.