cheapFunction(...) || expensiveFunction(...) // saves us a few cylces
car = car || "bmw" // setting default values, common pattern
funcA(...) && funcB_WhichMightBreakWithoutFuncA(...) // func A implies func B
...
// probably a few other cases I don't remember
Using it to handle control flow (e.g. throwing exceptions, as a makeshift if-then, etc.) is a recipe for disaster.Self-Documenting Code
11–20 of 131 posts
Re: Self-Documenting Code
#12 !(await userService.getUserByEmail(user.email)) || throwError(err.userExists);
I guess if I worked in a codebase that used that pattern consistently I'd get used to it pretty quickly, but if I dropped into a new codebase that I didn't work on often I'd take a little bit longer to figure out what was going on.Re: Self-Documenting Code
#13Re: Self-Documenting Code
#14The writer here misunderstands how short-circuit evaluation is supposed to be used. The idea is that you should use SCE in a few, pretty standard, cases: cheapFunction(...) || expensiveFunction(...) // saves us a few cylces car = car || "bmw" // setting default values, common pattern funcA(...) && funcB_WhichMightBreakWithoutFuncA(...) // func A implies func B ... // probably a few other cases I don't remember Using…
> cheapFunction(...) || expensiveFunction(...)
is not valid unless both functions return bool
> car = car || "bmw"
is not valid at all, because both types would need to be bool
> funcA(...) && funcB_WhichMightBreakWithoutFuncA(...)
not valid unless functions return bool. I think Go smartly realized this syntax is just sugar that causes more problems than it solves.
Re: Self-Documenting Code
#15Re: Self-Documenting Code
#16Honestly, I think all of jsdoc, pydoc, javadoc, doxygen is stuff that most code should not use. The only code that should use these is code for libraries and for functions that are used by hundreds or thousands of other people. And then we also need to notice that these docs in comments are not sufficient for documentation either. When a function is not used by hundreds or thousands of people, just write a conventional comment or perhaps not write a comment at all if the function is quite straightforward. Documentation that explains the big picture is much more important but that is actually somewhat hard to write compared to sprinkling jsdoc, pydoc, javadoc or doxygen worthless shit all over the place.
Re: Self-Documenting Code
#17The writer here misunderstands how short-circuit evaluation is supposed to be used. The idea is that you should use SCE in a few, pretty standard, cases: cheapFunction(...) || expensiveFunction(...) // saves us a few cylces car = car || "bmw" // setting default values, common pattern funcA(...) && funcB_WhichMightBreakWithoutFuncA(...) // func A implies func B ... // probably a few other cases I don't remember Using…
I would go further to say that syntax should never be used. for example with Go: > cheapFunction(...) || expensiveFunction(...) is not valid unless both functions return bool > car = car || "bmw" is not valid at all, because both types would need to be bool > funcA(...) && funcB_WhichMightBreakWithoutFuncA(...) not valid unless functions return bool. I think Go smartly realized this syntax is just sugar that causes m…
We can debate the merits of truthiness and using it this way, but let's have that debate on the merits, not by invoking other languages with completely different design constraints.
Your argument here is similar to what got us "no split infinitives" in English (grammarians wanted English to be more like Latin).
Re: Self-Documenting Code
#18Re: Self-Documenting Code
#19`isValid() || throwError()` is an abuse of abstraction
Re: Self-Documenting Code
#20The writer here misunderstands how short-circuit evaluation is supposed to be used. The idea is that you should use SCE in a few, pretty standard, cases: cheapFunction(...) || expensiveFunction(...) // saves us a few cylces car = car || "bmw" // setting default values, common pattern funcA(...) && funcB_WhichMightBreakWithoutFuncA(...) // func A implies func B ... // probably a few other cases I don't remember Using…
function insertion_sort(a) {
for (let i = 1; i 0 && key
If short circuit evaluation didn't exist, then "key < a[j - 1]" would be evaluated even in the case where j = 0, leading to the array being indexed out of bounds.