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);
– Brian Sep 20 '13 at 17:05true
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 */)
– Mark Lakata Sep 20 '13 at 21:27someFunction(/*remember=*/false)
where remember is the name of the parameter – Michael Deardeuff Sep 21 '13 at 02:56reverse
(orascending
) parameter to specify the order chosen. Then the code can simply use aless-than
function or agreater-than
function depending on the flag, without changing other lines of code. In this situations using flags makes code more DRY, so I believe there are situations where they can be used. – Bakuriu Sep 21 '13 at 08:17sortAscending
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... – Clockwork-Muse Sep 21 '13 at 11:56lambda
for a simple reversal of order may not be so readable. For simple use-cases use thereverse
parameter. When you want a completely different order pass in akey
function, and you can also combine the two, allowing sorting in both directions using the samekey
function. This is what python does and, in my experience, it works like a charm. – Bakuriu Sep 21 '13 at 12:56