Saturday, March 8, 2008

Parambiguity...

Have you ever looked at some unfamiliar source code and found yourself confused about the purpose of some parameters in method invocations? Take the following little snippet:
PuppyBlender pb = new PuppyBlender();
Puppy pup = new Puppy("Fido");
pb.blend(pup, true);
What on earth does that “true” parameter mean? The only way you can find out, given the code above, is to go look at the documentation or source code for the PuppyBlender class.

There's an easy way to write such code without the ambiguity:
PuppyBlender pb = new PuppyBlender();
Puppy pup = new Puppy("Fido");
boolean pulseMode = true;
pb.blend(pup, pulseMode);
One little extra line of code, and now we've made the purpose of that parameter quite clear. Modern compilers will optimize that variable out, so there's no performance impact at all.

Just clarity.

1 comment:

  1. Don't know where to leave this comment so I will make it here.

    Interesting new anti-casino blog started last week:
    http://crankyaboutcasinos.blogspot.com/2008/03/introduction.html

    Worth a look and a link?

    Mike Weber

    ReplyDelete