Martin Fowler describes the Flag Argument code smell as a "kind of function argument that tells the function to carry out a different operation depending on its value".
Answers to Is it wrong to use a boolean argument to determine behavior? seem to conflate all boolean arguments with flag arguments. I think that question doesn't fully explore the problem of boolean arguments.
However, I have a feeling that not all boolean arguments are equal.
A use-case for boolean arguments
Imagine a popup that shows a message that is partly dependant on whether a user is logged in or not. We would define a method
show(boolean isLoggedIn)
The method would be called in this way:
popup.show(session.isLoggedIn())
It seems to me that in this example readability and cohesion are not impacted negatively. Furthermore, I don't think that this is a flag argument. It doesn't tell the method how exactly to behave, but conveys the information necessary for the method to make that decision.
Alternatives
I can't think of a way to improve this method using the solutions proposed in the discussion above.
Enum would look something like this:
popup.show(session.isUserLoggedIn()
? Popup.ContentType.LoggedIn
: Popup.ContentType.LoggedOut);
Splitting the method would look like this:
if (session.isUserLoggedIn()) {
popup.showLoggedInMessage();
} else {
popup.showLoggedOutMessage();
}
This seems longer and also leads to code duplication if they have common parameters:
if (session.isUserLoggedIn) {
popup.showLoggedInMessage(commonIntro);
} else {
popup.showLoggedOutMessage(commonIntro);
}
In my opinion, using a boolean argument is better than both of these approaches.
So, are all boolean arguments a code smell, or can they be a valid technique?