New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The boolean "good" casting could be better #97
Comments
The example uses a Number, but the Boolean casting section is intended to be generic for any type. What if I agree checking values of Numbers should be done differently, but checking for truthiness is straightforward with |
Actually you completely missed the point. First of all if age === 0 is a On Fri, Nov 8, 2013 at 4:16 AM, Ross Allen notifications@github.com wrote:
|
The Boolean section is a possibly-too-abbreviated check for existence. What do you think of:
I can see how the double bang could be misinterpreted or possibly lead to the wrong value. |
I agree with @bobef on " Only conditions are true or false. And further the point is about readability and expressiveness and maintainability." Maybe the context is important, still |
+1 to avoid !!. |
Pruning this one. Feel free to reopen if you feel strongly about it. |
Superb. Can you tell if casting is being used here? Or does this == operator have its vulnerabilities as well? console.log(23 == "23") Hard to tell if casting happens to the left side in order to compare it with the right. Would a true value occur? |
@ut3saturn I’m not sure what you’re asking, exactly - all == operations do type conversions. As for which side is coerced, the rules are consistent but hard to understand, which is why it’s better to make the coercion explicit and use ===. |
I was wondering what airbnb style guides thinks about the Is there a rule pref for/against:
To ensure the that the check for |
@sstern6 |
I wouldn't call this "good".
If you think about it there is no way to cast number to truth. All numbers are equally true, they are just numbers. The logical negation operator converts from numbers to bools but this is just a dumb legacy from C where there are no real bools and convenience but has nothing to do with good practice. And further - double negation a good practice - are you serious? This damages readability so much, it could really confuse young people making them wonder if this is some kind of new operator and even experienced developers will need to think about what it does. And if you are debugging and you are in some kind of confusion already this will only add to the confusion. Then you need to be aware of all language specifics like what negation does to strings, empty strings, arrays, empty arrays, objects, empty objects, bools, numbers, specific numbers. If it was strongly typed language like C it is better because you know the type of "age", but in JS, and particularly in debugging/undocumented/confused situation you don't know the type of age and the code will produce different results.
The right thing to do would be.
The text was updated successfully, but these errors were encountered: