Tuesday, March 07, 2006

Sometimes it takes forever to sink in... Boolean flags are not readable.

So today I was looking at some code and some advice I read more than a year ago finally clicked. Quick, what does this code mean?

Provider provider = Enum.Parse(typeof(Provider), mySettings.ProviderName, true);

Back from looking up that overload of Enum.Parse yet? So my point is this, you don't know what that true means without looking it up. How much more obvious is this code?

   CaseSensitive = 0
   , IgnoreCase = 1
} MatchCase;
Provider provider = Enum.Parse(typeof(Provider), mySettings.ProviderName, IgnoreCase);

The moral of this story is that instead of building methods that take boolean flags, construct them to take enumerations that clearly document the meaning of the flag. This way you don't have to do an API lookup, or just know. I first saw this idea about a year ago... if my head wasn't so full of things I had to just know it might have sunk in earlier.

1 comment:

Anonymous said...

Interesting -- I've never thought about that since I'm so used to intellisense, but still a good idea.

Paul Wilson