Is it wrong to use a boolean parameter to determine behavior?

Posted by Ray on Programmers See other posts from Programmers or by Ray
Published on 2012-05-09T22:33:30Z Indexed on 2012/11/07 11:20 UTC
Read the original article Hit count: 198

I have seen a practice from time to time that "feels" wrong, but I can't quite articulate what is wrong about it. Or maybe it's just my prejudice. Here goes:

A developer defines a method with a boolean as one of its parameters, and that method calls another, and so on, and eventually that boolean is used, solely to determine whether or not to take a certain action. This might be used, for example, to allow the action only if the user has certain rights, or perhaps if we are (or aren't) in test mode or batch mode or live mode, or perhaps only when the system is in a certain state.

Well there is always another way to do it, whether by querying when it is time to take the action (rather than passing the parameter), or by having multiple versions of the method, or multiple implementations of the class, etc. My question isn't so much how to improve this, but rather whether or not it really is wrong (as I suspect), and if it is, what is wrong about it.

© Programmers or respective owner

Related posts about design

Related posts about coding-standards