3 ms·
I have an issue with complex conditions with or without local variable labels for readability. You really shouldn't have them at all. At one time, they used to
by jmyeet 2y ago
I have an issue with complex conditions with or without local variable labels for readability. You really shouldn't have them at all.
At one time, they used to teach that functions should have one entry-point (this is typically a non-issue but can come up with assembly code) and one exit-point. Instead of a complex condition, I much prefer just early returns ie:
// what's going on 1
if (condition1 || condition2) {
return;
}
// what's going on 2
if (condition3 && condition4) {
return;
}
// what's going on 3
if (condition5) {
return;
}
// do the thing
return;
- Aeolos 2y agoI prefer this style for languages that have either a GC or scoped resource management (eg RAII). However, I think the single exit point holds merit for C, where an early return can easily and silently cause a resource leak. (Unless you use compiler-specific extensions, or enforce rust-style resource ownership, which is really hard without compiler support.)