Clean Code and Squirrel Programming

by Luke 5. October 2011 03:24

I’ve been readying through the Clean Code book by Robert C. Martin. I am only a chapter or two into the book and I feel like my code is becoming much more manageable and easier to ready. Method length was probably my biggest offender. I also used to comment a lot in code but now I am writing more self describing methods and variables so I don’t need to comment as much.

I saw a squirrel this morning and it did the usual thing of running to the nearest tree. For whatever reason I thought it would be an interesting exercise to write some pseudo-code about an event they would handle; here it goes.

   1: protected void OnHumanSpotted(object sender, EventArgs e)
   2: {
   3:   List<Trees> localTrees = GetSurroundingTrees();
   4:  
   5:   Tree closestClimbableTree = GetSafestTreeToClimb(localTrees);
   6:  
   7:   if (closestClimbableTree != null)
   8:   {
   9:     Climb(closestClimbableTree);
  10:   }
  11:   else
  12:   {
  13:     FleeOnFoot();
  14:   }
  15: }

I like how you don’t need to know the implementation of any of the methods, the intent of the code is communicated through the method names. I am sure this could be improved even more by encapsulating the methods even more but it works for this simple example.

Cheers!

Tags:

General | Clean Code

About Luke

C# .NET developer who really likes kettle style potato chips. Current favorite is Cape Code potato chips with a Henry Weinhards root beer on the side, mmmm delicious

 
 
The views and opinions on this blog site are my own and are not that of my employer.