Detecting and Naming Boolean Parameters
After a recent discussion on the Scala-Internals mailing list on the pros and cons of methods that take boolean arguments, the consensus was that they should always be passed as named arguments. The compiler doesn’t enforce this, so it’s up to us IDE and tools developers to provide a solution. The code-analysis branch for the Eclipse Scala IDE can now warn you of such boolean arguments that are passed to Scala methods:
The warning comes with a quick-fix that inserts the parameter name:
Of course, the competition isn’t sleeping either, the IntelliJ Scala plug-in just got a bunch of new Intentions.
What makes Boolean special compared to other types?
Do you mean why the quickfix is only available for booleans or why boolean arguments shouldn’t be used in APIs? First, I should really make that available for all parameters, that’s a good point 🙂 For why they’re bad in API, it just makes it really hard to use, it’s often better to use an enum or two separate methods.
I fail to see why for example having a method taking three boolean parameters is necessarily more confusing to use than one taking three int parameters.
Oh, three Ints are just as bad 🙂 in my experience, boolean parameters are typically introduced if a method can be used to do two different things, depending on that boolean flag. That
In Qt, they call this the Boolean Parameter Trap: http://doc.trolltech.com/qq/qq13-apis.html#thebooleanparametertrap
It may be nice to also have a Quick Fix that introduce a method, i.e.,
def forceDoSomething() = doSomething(force = true)
Just saying 🙂