2 July 2007

The Complete Code

by mo

Last weekend I started reading Code Complete by Steve McConnel. I’m loving it, I just started chapter 22, but I wanted to take a moment and share some of the tidbits of information and quotes from this book that I found quite interesting. If you haven’t already, I recommend you give it a try!

“Avoid creating omniscient classes that are all-knowing and all-powerful. If a class spends its time retrieving data from other classes using Get() and Set() routines (that is, digging into their business and telling them what to do), ask whether that functionality might better be organized into those other classes rather than into the god class …”

This paragraph immediately struck home to me. Have you ever stepped into a new team, and found that there are classes in some project that are over 500 lines long? How about 1000? How about 3000? When I saw this, it screamed… REFACTORING to me! When I asked one of the authors to describe to me what this class did, he listed off a list of responsibilities. (On a side note… I can’t wait to read Refactoring by Martin Fowler! It was way to scary to try to change that 3000+ line class because no one knew what would happen if we did. Enter… unit tests!)

I pulled out a pad of paper and a pen and listed off the responsibilities, and read them back to the author. 1… 2… 3… 4… This subtle activity helped the author see, that “hey, this class should probably broken out into smaller pieces”. Bingo! Even as a junior my code smell was tingling…

“Avoid empty catch blocks… either the code within the try block is wrong because it raises an exception for no reason, or the code within the catch block is wrong because it doesn’t handle a valid exception. Determine which is the root cause of the problem, and then fix either the try block of the catch block.”

I’ve got a bit of a pet peeve when it comes to code that swallows exceptions.. check out my previous post on this.

“… C++ uses short-circuit evaluation: if the first operand of the and is false, the second isn’t evaluated because the whole expression would be false anyway. In other words, in C++ the only part of

if( SomethingFalse && SomeCondition )

that’s evaluated is SomethingFalse. Evaluation stops as soon as SomethingFalse is identified as false.”

This is also true in C#, if you have a case where you need both statements to execute regardless of whether it fails or passes, use a single & character. This is dangerous however, if your second condition depends on the first condition to be true.

For example…

  if ( !string.IsNullOrEmpty( name ) && name == "mo" ) {}

In this scenario regardless of if the first condition is true or false, the second condition will be executed. This could raise a null reference exception if name is not initialized.

“One measure of ‘ programming complexity’ is the number of mental objects you have to keep in mind simultaneously in order to understand a program. This mental juggling act is one of the most difficult aspects of programming and is the reason programming requires more concentration than other activities. It’s the reason programmers get upset about ‘quick interruptions’ - such interruptions are tantamount to asking a juggler to keep three balls in the air and hold your groceries at the same time.”

Enough said… I like to call it “being in the zone”. When you’re in and hammering away at the problem, the slightest interruption makes it so difficult to get back into “the zone” at the same speed. It’s so important to find a good work area, where distractions are at a minimal. It shows in your work!

“The surprising implication is that people actually do what you ask them to do. Programmers have a high achievement motivation: They will work to the objectives specified, but they must be told what the objectives are.”

I think this paragraph could be interrupted in many ways. When I first read it, my interpretation was about leadership. Having a good leader to direct and the team. It might be fun to watch chickens running around with their heads cut off, but it sure isn’t fun being one of the chickens. Finding a good mentor or experienced leader to guide the team sure isn’t easy to find.

Code Complete, Second Edition by Steve McConnell Read more about this title…