Follow the Boy Scout Rule with Visual Studio 2013



If you develop software using an agile approach you may have heard of the ‘Boy Scout’ rule.   The often quoted rule says “Leave the code a little better than you found it”


I tried to find the original source of this.  I think that it may have come from a letter that the founder of the Scout movement Baden Powell wrote.  This letter was found after his death as had the passage "Try and leave this world a little better than you found it."


It certainly is a great way to approaching existing code.  When we come to work with a code base, it often has issues.  These issues are the results of decisions and trade-offs made by those who came before us.  Often changes are needed to make the code cleaner.  I am not sure about you but I never seem to get the projects that start with a phase called “Clean Up”, and have a customer who is happy to invest significantly in tidying up the existing code base before we start.


So what do we do?  This is where our ‘Boy Scout’ rule applies.  Whenever we are working in an area of code (in order to implement a new feature) we leave it cleaner than it was.


How do we know that it’s cleaner?  Well we might improve naming, reduce dependencies, simplify and shorten methods etc and then at some point we decide that we have cleaned things enough and move on.  Sometimes it is very easy to know that you have made a significant improvement, and sometimes it is much more difficult.  (Try simplifying a 10,000+ line method….)


Here is where Visual Studio can help us.  The new Code Lens feature in Visual Studio 2013 RC provides a heads up display of useful information about the code that we are viewing.  This includes Test Status, References, Authors etc.  This data helps us understand the context that we are working in.


A new ‘indicator’ has been published to DevLabs .  DevLabs is an experimental delivery channel for Visual Studio add-ins.  The new indicator is focused on ‘Code Health’ and it shows me things like


Maintainability Index, Cyclomatic Complexity, Lines of Code and Halstead Volume (yes this one was new for me too).  Here is what you see in your code




Click on the maintainability indicator to see more details




I can hover over any of the measures to get more details and an understanding of the formula behind it.


But what about the Boy Scout rule?  Well you can see that before I touched this method, the maintainability index was 65.  I have added another ‘if’ condition to make it worse.  My code now looks like this




Notice the new statement?  Also look at my Code Health indicator.  It now has dropped to 60 and there is a red indicator telling me that I have MADE IT WORSE!  I have broken the rule! 


If I click into the indicator it shows me which indicators I just moved.  I have increased Cyclomatic Complexity by 1 etc.




Well that won’t do!  I am now going to go in and change things to move my statement out to a method call and try and clean things up.


Now my indicator shows an improvement




If I open it the indicator I can see that I have improved all of the health measures.  I am a Boy Scout!




I am already finding this addictive.  This is changing the way that I code.  Who wants to leave things worse than you found them?  Method by method, this keeps you honest and makes you want to improve things as you change them.  There is satisfaction from seeing the arrow point up.  You can continue coding safe in the knowledge that you have left things a little better behind you.  That is a good feeling.


You can see more details at David Starr’s blog here.  Also be aware of some disclaimers.  As a feature delivered through DevLabs, this is an experiment.  It also only works on methods and only on C#.


No comments :

Post a Comment