18

From some open source projects, I gathered the following coding style

void someFunction(bool forget);

void ourFunction() {
  someFunction(false /* forget */);
}    

I always have doubt about what false means here. Does it mean "forget", or does the "forget" refer to its corresponding parameter (like in the case above), and "false" is meant to negate it?

What style is used most often and what is the best way (or some of the better ways) to avoid the ambiguity?

gnat
  • 20,543
  • 29
  • 115
  • 306

12 Answers12

33

In the sample code you posted, it looks like forget is a flag argument. (I cannot be certain because the function is purely hypothetical.)

Flag arguments are a code smell. They indicate that a function does more than one thing, and a good function should do only one thing.

To avoid the flag argument, break up the function into two functions that explain the difference in the function name.

Flag argument

serveIceCream(bool lowFat)

No flag argument

serveTraditionalIceCream()
serveLowFatIceCream()

edit: Ideally, you don't need to keep around a function with the flag parameter at all. There are cases along the lines of what Fowler calls a tangled implementation where completely separating the functions creates duplicated code. However, the higher the cyclomatic complexity of the parameterized function, the stronger is the argument for getting rid of it.


This is only a hunch, but a parameter named forget sounds like feature envy. Why would a caller tell another object to forget something? There may be a bigger design issue.
26

In layman's words:

  • false is a literal.
  • you are passing a literal false
  • you are telling someFunction to not forget
  • you are telling someFunction that the parameter forget is false
  • you are telling someFunction to remember

In my opinion it would be better if the function was like this:

void someFunction(bool remember);

the you can call it

void ourFunction() {
  someFunction(true);
} 

or keep the old name but change your wrapper function to

void ourFunctionWithRemember() {
  someFunction(false);
} 

EDIT:

As @Vorac stated, always strive to use positive words. Double negation is confusing.

Tulains Córdova
  • 39,570
  • 13
  • 100
  • 156
14

The parameter may be well-named; it's hard to tell without knowing the name of the function. I assume that the comment was written by the original author of the function, and it was a reminder of what passing false into someFunction means, but for anyone coming along afterward, it's a little unclear at first glance.

Using a positive variable name (suggested in Code Complete) may be the simplest change that would make this snippet easier to read, e.g.

void someFunction(boolean remember);

then ourFunction becomes:

void ourFunction() {
    someFunction(true /* remember */);
}

However, using an enum makes the function call even easier to understand, at the expense of some supporting code:

public enum RememberFoo {
    REMEMBER,
    FORGET
}

...

void someFunction(RememberFoo remember);

...

void ourFunction() {
    someFunction(RememberFoo.REMEMBER);
}

If you're unable to change the signature of someFunction for whatever reason, using a temporary variable makes the code easier to read as well, sort of like simplifying conditionals by introducing a variable for no other reason than to make the code easier to parse by humans.

void someFunction(boolean remember);

...

void ourFunction() {
    boolean remember = false;
    someFunction(remember);
}
10

Rename the variable so a bool value makes sense.

This a million times better than adding a comment to explain arguments to a function because the name is ambiguous.

enderland
  • 12,201
  • 4
  • 53
  • 64
4

Create a local boolean with a more descriptive name, and assign the value to it. That way the meaning will be more clear.

void ourFunction() {
    bool takeAction = false;  /* false means to forget */
    someFunction( takeAction );
}    

If you can't rename the variable, then the comment should be a little more expressive:

void ourFunction() {
    /* false means that the method should forget what was requested */
    someFunction( false );
}    
3

There is a good article that mentions this exact situation as it refers to Qt-Style APIs. There, it's called The Boolean Parameter Trap and it's worth a read.

The gist of it is:

  1. Overloading the function so that the bool isn't needed is better
  2. Using an enum (as Esailija suggests) is best
Steven Evers
  • 28,180
1

This is a bizarre comment.

From the compiler's perspective, someFunction(false /* forget */); is actually someFunction(false); (the comment is stripped). So, all that line does is call someFunction with the first (and only) argument set to false.

/* forget */ is just the name of the parameter. It's probably nothing more than a quick (and dirty) reminder, that doesn't really need to be there. Just use a less ambiguous parameter name, and you won't need the comment at all.

1

One of advices of Clean code is to minimize number of unnecessary comments1 (because they tend to rot), and to name functions and methods properly.

Following that, I would just remove the comment. After all, modern IDEs (like eclipse) pop a box with code when you put a mouse over the function. Seeing the code should clear the ambiguity.


1 Commenting some complex code is ok.

BЈовић
  • 14,049
1

To belabor the obvious, comments can lie. Thus is always better to make your code self-documenting without resorting to comments to explain, because some person (perhaps you) will change true to false and not update the comment.

If you can't change the API, then I resort to 2 options

  • Change the comment so that it always true, regardless of the code. If you only call this once, this is a good solution, since it keeps the documention local.
     someFunction( false /* true=forget, false=remember */);`
  • Use #defines, especially if you call it more than once.
     #define FORGET   true
     #define REMEMBER false
     someFunction(REMEMBER);
1

I like the answer about making the comment always true, but while good I think it misses the fundamental problem with this code -- it is being called with a literal.

You should avoid using literals when calling methods. Local variables, optional parameters, named parameters, enums -- how you best avoid them will depend upon the language and whats available, but do try to avoid them. Literals have values, but they don't have meaning.

jmoreno
  • 11,238
-1

In C#, I used named parameters to make this clearer

someFunction(forget: false);

Or enum:

enum Memory { Remember, Forget };

someFunction(Memory.Forget);

Or overloads:

someFunctionForget();

Or polymorphism`:

var foo = new Elephantine();

foo.someFunction();
Jay Bazuzi
  • 1,604
-2

The naming should always resolve ambiguity for booleans. I always name boolean something like 'isThis' or 'shouldDoThat', for example:

void printTree(Tree tree, bool shouldPrintPretty){ ... }

and so on. But when you are referencing someone else's code, its best to just leave comment when passing values.