4 ms·
Because it gives you the opportunity to descriptively name the expression, sparing others from parsing it mentally.
by bberrry 7y ago
Because it gives you the opportunity to descriptively name the expression, sparing others from parsing it mentally.
- Liron 7y agoDo you also think `if ((x > 3) == true)` is more descriptive than `if (x > 3)`?
- sombremesa 7y agoCan't the returning function be named appropriately?
- bberrry 7y agoI mean, it's the same thing as extracting the boolean expression into a function and calling that in the if statement. It's also easy to imagine a quite long expression that would benefit from breaking into two or three descriptive names. It's a good tool for your own sake when coding, to not get the logic tangled up.
- pdkl95 7y agoStoring the result of a Boolean expression isn't the strange part. Storing it in a well-named variable can make the code a lot easier to read; the name (hopefully) communicates intent, and it can separate a conditional action into separate steps. The strange part is testing Boolean value... to return the same value as Boolean constants. Why wasn't the Boolean result simply returned directly? var condn = ...; return condn; The if statement isn't necessary. Even if you wanted to explicitly return only "true" or "false" - using the if statement to convert truthy and falsy results into true booleans - using double not operators removes q potentially mispredicted branch. var condn = ...; return !!condn;
- bberrry 7y agoAh, sorry, I completely failed to realize that was the question being asked. :)