Wayne's World Methods

Excellent! ... NOT!

I've just been bitten by a common coding style: methods that take an argument which negates the meaning of the method's name. This makes the implementor's job easier but makes code that calls the method harder to read, and therefore makes maintenance more difficult than it should be.

I came across this style when using a test framework to write functional tests that drive the GUI of a large application. The framework provides methods to find components by name, make assertions upon their state and fake user activity. The framework provided several methods like:

void assertButtonIsEnabled( String buttonName, boolean isEnabled );

Which resulted in test code that looked like:

...
assertButtonIsEnabled( "editButton", false );
...

I was pairing with Dan Abel at the time, and we were both immediately reminded of an annoying catchphrase from the Wayne's World movie. We couldn't help reading the assertion as "assert that the edit button is enabled... not!" Not only was this aggravating; the assertions written in this style made the tests harder to read. A better approach would have been to define two methods:

void assertButtonIsEnabled( String buttonName );
void assertButtonIsDisabled( String buttonName );

And then the test would have read:

...
assertButtonIsDisabled( "editButton" );
...

I've written Wayne's World methods in the past, and I'll use then all time now I've seen the problems they cause... Shaaah! And monkeys might fly out of my butt!

Update: here are two good articles about the problems caused by boolean parameters and, more importantly, what to do instead:

Copyright © 2004 Nat Pryce. Posted 2004-08-09. Share it.

Comments powered by Disqus