Friday, August 3, 2007

Both, either or any

I recently was adding functionality to a part of our product that was full of lines like this:

  • lBool_vert_in = this.IsBoathBitsSet(iArrAllow[key], iArrDeny[key],
  • top.mTDSDefines_CheckInAction);
At first I was just annoyed by the obvious typo in there. I'm sure a boath is something (maybe a very expensive boat) but it has no place in our code. Making a typing error is not strange, it's actually quite normal. But not correcting it when you're copy/pasting it at least a few dozen times is a kind of laziness that I don't like very much.

Later I also needed to know what the function does. At first I thought the name was actually pretty self-describing. But I couldn't explain the behavior I was seeing in a part of the code that invoked this function.
  • this.IsBoathBitsSet = function(iBit1Value, iBit2Value, iDefineType)
  • {
  • return (
  • (
  • (iBit1Value & iDefineType)||
  • (iBit2Value & iDefineType)
  • ) == iDefineType )
  • }
Now that strikes me as odd... the function name suggests that both bits need to be set. This would normally translate into an && and not into an || like in this code. So instead of just a simple typo, this function name is actually plain wrong. And it also immediately explained the behavior I was getting.

I was thinking of renaming the function. But what shuld I rename it to? At first I was thinking IsEitherBitSet, since it returns true if either bit it set. But that is also not entirely correct. Since it also returns true if both bits are set.

So maybe it should be called IsAnyBitSet. What do you think? How do you translate boolean operators into English?

No comments: