247

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.

Ray
  • 2,480
  • 2
    This is a question of where decisions belong. Move the decisions in a central place instead of having them littered all over. This will keep the complexity lower than having a factor two of code paths everytime you have an if. –  May 10 '12 at 12:35
  • 37
    Martin Fowler has an article about this: http://martinfowler.com/bliki/FlagArgument.html – Christoffer Hammarström May 10 '12 at 14:17
  • 5
    Also see http://stackoverflow.com/questions/1240739/boolean-parameters-do-they-smell and http://stackoverflow.com/questions/6107221/at-what-point-does-passing-a-flag-into-a-method-become-a-code-smell – Christoffer Hammarström May 10 '12 at 14:18
  • @ChristofferHammarström Nice link. I'll include that in my answer as it explains in much details the same idea of my explanation. – Alex May 10 '12 at 16:32
  • 1
    I don't always agree with what Nick has to say, but in this case I agree 100%: Don't use boolean parameters. – Marjan Venema Aug 14 '13 at 20:10
  • 1
    I think this question consists of two parts: 1. using a boolean variable to a method and 2. using a variable (boolean or not) to determine behavior. The first part shows a bad design decision, but it can be avoided by e.g. using an enum. The second part is the more interesting question: is it good design to switch behavior according to some specific variable? – Maarten Bodewes Nov 15 '18 at 13:13
  • My two cents on this question: 1) bool is a generic parameter, representing something that can only be in two states. So enum ShopStatus {Open, Closed} or enum ClientStatus {Connected, Disconnected} are logically booleans, but they are specialized cases. Thus, a function f(..other_args.., ShopStatus shop_status) is fine, but a function f(..other_args.., bool shop_status) is a code smell, for a similar reason as to why you should avoid passing void pointers or base classes to the function, when it can only deal with a specialized type. – mercury0114 Apr 22 '23 at 10:19
  • In the future you might want to introduce more states to your system, ShopStatus {Open, Closed, LunchBreak}, extending an enum type would be no problem, but boolean is limited to two states only.
  • – mercury0114 Apr 22 '23 at 10:21