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

Comments

10/19/2011 1:04:29 AM #

Hi, it was very handy. Thanks a lot.

Christopher Portor United States |

11/4/2011 7:09:54 PM #

Duarte@yahoo.com

Andy Glantz United States |

11/8/2011 3:00:20 PM #

Great post!  I love your "squirrel code".

Maybe this will be the start of a new IT employment subset:  Animal Programming
[LOL]

Fast Way To Lose Fat United States |

11/9/2011 10:11:06 PM #

Way to go... Your code does look good!

Case Interview United States |

11/15/2011 9:46:25 AM #

Hi I just wanted to find out on what is the difference between blogenenigne and wordpress blogs? Is it easier to use or more efficient?

ollywright United States |

11/19/2011 6:46:56 AM #

The other is broccoli rice casserole. I’d say that fills at least a quarter of my plate leaving only 3/4 left for the other assortment of goodies.

Wilton Demiter United States |

11/20/2011 1:08:18 PM #

just a thought - i live in the southeast, and there's no lack of biomass here sitting around in Kudzu and bittersweet (and other weeds), as well as multiflora rose and japanese honeysuckle-and what of hemp?

Deangelo Sterle United States |

11/21/2011 1:22:16 AM #

Great share, Thanks

SJR Luxuria project United States |

11/28/2011 7:14:27 AM #

Just to say I like your blog and found it interesting checking out some of the content, I'll spend some more time here when I've got a few minutes to spare.

Clark United States |

12/3/2011 5:25:57 PM #

Thanks  for this super site. Seems like there\\\'s always something new I learn even after being in the field for 10 years.

Stephen |

Comments are closed

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.