by dietr1ch · 3y ago · 0 comments
if (someLongNamedVarIsNotUnknown && someLongNamedMapCountIsZero)
return false;
else
return true;
This is not the same, but I'd suspect there might be a bug in the code above if (someLongNamedVarIsUnknown) {
return false; // Can't do much
}
if (someLongNamedMapCountIsZero) {
return false; // Nothing to do
}
// Do stuff
return true
The named booleans suggested here help adding clarity, but more often than not they'll end up being single-use (const) variables that just paraphrase their definitions. In these cases I'd rather avoid introducing a variable, and rely in comments if I really need to explain something.Interesting. With the same axioms I come to the opposite conclusion. That comments are to be avoided except when strictly necessary, and giving names to concepts via intermediate const variables is one of the best weapons in that fight. Ie. make your code read as close to what the comment would be (but simply via naming) makes it very clear when the names no longer match the behavior, in contrast to comments which have a tendency to bit rot.
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.
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
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.
const value = if (a) {
return true
} else {
return switch (b) {
case 1: return false
default: return undefined
}
} value := (if a then True
else (
case b is
when 1 => False,
when others => Undefined
));https://learn.microsoft.com/en-us/dotnet/csharp/language-ref...
const value = [&]() {
if (a) {
return true;
}
return switch (b) {
case 1: return false;
default: return undefinied;
}
}(); let value = if a { true } else { match b { 1 => false, _ => undefined } };
And notice that because now we're a function our relationship to the world changed, how does your immediately invoked lambda do this? let value = if a { true } else { match b { 1 => false, 2 => return foo, _ => undefined }};
With expression syntax returning from the function we're in is fine† but in your lambda you can't do that, you need control flow logic outside the lambda to handle it.† You might wonder about the inferred type of this expression since it might return. The type of the return expression in Rust is ! aka Never, which is an empty type. In the same way that N + 0 == N in integer arithmetic, any type plus an empty type is the original type again in type arithmetic, so the type of the expression isn't altered.
Still the syntax is a bit confusing. In ML languages (f# and also rust are somehow descendants of ML) you can just use if .. else if .. else expressions. They work like ternaries! But easier readable. Nested ternaries are useful, but get really hard to read very fast. (x = condition1 ? A : condition2 ? B : C;)
LLVM and GCC can at least.
const value = (()=>{
…
})()