by simplotek · 3y ago · 0 comments
That's ok. The worst tradeoff in the "named booleans" pattern proposed in the blog post is that it misses the whole point of chaining predicate evaluations, which is leveraging short-circuiting to prevent further predicates from being evaluated.
Chaining logical comparisons and the way they short-circuit is specially important when a predicate has preconditions and/or is expensive and/or has side-effects.
If your predicates are hard to read you don't fix that mess with "named booleans". You fix them by extracting them to a pure function like
isNotUnknown(_someLongNamedVar) && isNotEmpty(_someLongNamedVar)Says who? IME relying on short circuiting is useful and idiomatic in C and C++ code, e.g.:
if (callback && callback->run()) { ... }
In your opinion would it be better to write: if (callback) {
if (callback->run()) {
..
}
}
The situation is even more awkward if you try to avoid || short-circuiting, since you need to duplicate the body of the conditional.In a case like this, my preference would be to give callback a dummy value and always run it.
I subscribe pretty heavily to the Carmack philosophy of always running the code as similarity as possible and making a decision at the end of a procedure.
In the above example, we check that the pointer is non-null before calling it. The "side effect" we are avoiding is a segfault.
In the code that splits the conditional into two, the callback is still run inside a conditional, just by itself.
I'm curious, what leads you to believe that skipping unnecessary calls to functions with side effects is something to be avoided? Do you believe that unconditionally calling code with side effects specially when you don't have to is something to be desired?
I question the idea that you would ever want the very linchpin of your function hidden away as a second subclause of an if statement. A function that calls callbacks is probably mainly doing that, let it stand proud and clear in the code text.
If your function is calling callbacks left and right “on its way somewhere else”, I think a refactor is in order.
Maybe you will now present some second scenario with something other than callbacks. Maybe in that case you’ll have me convinced it’s an okay exception. For you see, I said only it’s “pretty shaky”, not a mortal sin.
With that said though, I think in 9/10 cases, it’s something you can solve with an early return.
if (!callback)
return;
callback();in mine, yes definitely 100% the second code is more readable than the first
If anything, adding a nesting level for each chained predicate makes code harder to read, and needlessly so.
You've got it all backwards. Short-circuiting is not used to avoid side effects. Short-circuiting is used to avoid needless calls to any predicate, which can have and often do has side-effects, and if it doesn't have today it might have tomorrow.
If you don't call them when you don't have to, your code is in a better shape.
And let's not pretend that unconditionally macode with side effects when you don't have to is a good practice.
The point is not performance. The point is readability, and expressing things clearly and with the appropriate level of detail and leaving out nesting and scope. Safety and performance are important but secondary advantages.