Monday, January 14, 2008

No changes, just a digression...

So I was coding today (sadly on a project related to RealLife (tm) and not DroidDraw), and I was thinking about something:

One of my big pet-peeves is people who write:
if (boolean-exp)
  return true [or even worse] boolean-exp
else
  return false

Because, obviously, it should simply be:
return boolean-exp;

But what about (assuming 2 unrelated boolean expressions boolean-exp-1 & boolean-exp-2):
if (boolean-exp-1)
   return boolean-exp-2;
else
  return false;

Now, by boolean logic, you could combine these into:
return (boolean-exp-1) && (boolean-exp-2);
which is equivalent. 

But should you?  I can't decide.

On the one hand, the latter is more concise.  But I feel like it looses some of the meaning, because it merges the two boolean conditions into a single condition which might be confusing to later maintainers of the code who don't have the original insight.

By the way, I know that there is another option in many languages:
return (boolean-exp-1)?boolean-exp-2:false;

But that's just magic concisi-fying syntactic sugar applied to the earlier code.

1 comment:

Machi said...
This comment has been removed by the author.