Which is clearer form: if(!value) or if(flag == value)?

后端 未结 19 2622
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 19:07

I understand this is a subjective question, so I apologize if it needs to be closed, but I feel like it comes up often enough for me to wonder if there is a general preferen

相关标签:
19条回答
  • 2020-12-23 19:57

    Dissenting opinion (kind of)

    From a compilation standpoint, you're going to get the same IL, so it really only matters from a readability standpoint.

    From that standpoint, the if(value == false) is more obvious to a casual reader, and there is less chance of missing the ! before the bool.

    Honestly, I use both approaches, and most times, I make it depend on my variable name. If it still sounds ok to say "not" in place of the "bang", I'm likely to use the bang notation

    e.g.

    if(!gotValue) {}
    //if (I've) not gotValue
    
    //but
    
    if(checkValue == false){}
    //If (I've) not checkValue doesn't quite work here grammatically.
    
    0 讨论(0)
  • 2020-12-23 20:00

    if (!value) is easier/faster to follow. Subjective as you said. As long as you are consistent, this is the main thing.

    EDIT

    One other point to add - omitting the true/false keywords should also (hopefully) force the coder to use better named variables. Bool variables should always indicate meaning or state purpose, such as:

    if (MyWallet.IsEmpty)

    There is no reason with the above to use == false or == true as it's redundant. The above is human readable immediately.

    Far better than having to decipher:

    if (MyWallet.EmptyStatus == true) or something ridiculous like this.

    0 讨论(0)
  • 2020-12-23 20:00

    Whatever one you prefer. Pick one and stick to it.

    0 讨论(0)
  • 2020-12-23 20:01

    I am sorry to say, the second just looks stupid to me.

    I'd add an extra level, if someone prefers it:

    if( (value==false) == true )
    

    :)

    0 讨论(0)
  • 2020-12-23 20:02

    I would normally prefer if (!value) too, when I know for sure that value is a boolean. But many times it can be a string, or a number.

    The number zero would evaluate to false in conditionals in a lot of languages (not all, though); however, the string "0" would evaluate to true. This is a problem particularly in JavaScript, particularly if you receive JSON strings from the server, particularly if the server is written in PHP (because most PHP developers are careless enough to just take values from the DB and call json_encode on them, not knowing that the DB yields strings and not having a clue that all those zeros and ones that they use as boolean fields will be encoded as strings on the other end, thus all treated as true in conditionals).

    Rant over. My suggestion: be explicit, especially if your language is the “very dynamic” type (i.e. JavaScript, PHP, Perl).

    0 讨论(0)
  • 2020-12-23 20:03
    if (!value)
    

    This is always clearer in my opinion.

    if (value == false)
    

    I hate to say this, because it sounds kind of mean, but this normally shows that the person writing the code doesn't really understand the use of boolean values. You don't need to re-validate what a boolean is in an if statement. It's redundant.

    (Personally, I would be annoyed at the person too if they named the variable value instead of something more meaningful. I have a feeling what you posted is just psuedo code, I would definitely ding that on a review.)

    Edit (in response to a comment below):

    It may look trivial, but often it is a sign of much bigger things. Truthfully, most people who do use var == true etc. don't understand. It's just a fact. I'm not saying their stupid or they shouldn't be programmers just that there is probably something that they need to review and learn. The problem is that when logic gets much more complex, not understanding concepts like this can lead to much much bigger problems down the road. Some people say "it's a style." That's fine. The real question in this case is, "How is it beneficial for me to do it this way? What do I or other people get from it?" If you can't solidly answer that question, then you need to ask yourself "Why is this a good idea?"

    0 讨论(0)
提交回复
热议问题