{{announcement.body}}
{{announcement.title}}

The Boy Scout Software Development Principle

DZone 's Guide to

The Boy Scout Software Development Principle

Leave your code in a better state than you find it, in every single pull request

· Agile Zone ·
Free Resource

Boy scouts have an easily understood philosophy, which is that they should always leave their campsites in a better condition than the state which they found it in. Cleaning away one piece of garbage each, or something equivalent, creates a situation where a visit from a boy scout group, becomes a great experience for everybody else. Developers can learn a lot from this philosophy.

Every single time you create a pull request, the code you commit should "pick up one piece of garbage", by refactoring it somehow, making it more easily read, and/or performing better somehow. If you make a habit out of this, then over time your codebase will slowly become better.

Pick up other people's garbage

If you do it, and you instruct your developers to also do it, after a while everybody will do it. Below is an example to get you started.

C#
 




x
10


 
1
void Foo(int val)
2
{
3
  if (val != 0)
4
  {
5
    /* ... Do stuff with val ... */
6
  }
7
  else
8
  {
9
    throw new ArgumentException("Val is 0");
10
  }
11
}


For the seasoned developer, spotting the garbage in the above method is fairly simple. Hint: Invert your if! Let me show you a simpler version, with less garbage, to illustrate my point.

C#
 




xxxxxxxxxx
1


 
1
void Foo(int val)
2
{
3
  if (val == 0)
4
    throw new ArgumentException("Val is 0");
5
  /* ... Do stuff with val ... */
6
}


By simplifying and inverting our above if statement, we reduced the size of the code from 11 lines to 6 lines. 50% of the code gone! And we did it without changing any logic what so ever. The code is more easily read, which makes it more easily maintained, and the method as a whole more easily fits into a single screen, without scrolling, making it a much more pleasant experience to go back to it 6 months down the road, to add another feature to it ...

I could add more to this article - However, that would only add garbage to it - So I'll leave it here for now :)

Topics:
architechture, refactoring

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}