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);
}
someFunction(forget: true);
true
tofalse
and not update the comment. If you can't change the API, then the best way to comment this issomeFunction( false /* true=forget, false=remember */)
sortAscending
andsortDescending
, or similar). Now, inside, they may both call the same private method, which might have this kind of parameter. Actually, if the language supported it, probably what I'd pass in would be a lambda function that contained the sorting direction...