Should I use `!IsGood` or `IsGood == false`?

后端 未结 30 2272
礼貌的吻别
礼貌的吻别 2020-12-02 19:49

I keep seeing code that does checks like this

if (IsGood == false)
{
   DoSomething();
}

or this

if (IsGood == true)
{
   D         


        
相关标签:
30条回答
  • 2020-12-02 20:51

    I prefer the !IsGood approach, and I think most people coming from a c-style language background will prefer it as well. I'm only guessing here, but I think that most people that write IsGood == False come from a more verbose language background like Visual Basic.

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

    Cybis, when coding in C++ you can also use the not keyword. It's part of the standard since long time ago, so this code is perfectly valid:

    if (not foo ())
       bar ();
    

    Edit: BTW, I forgot to mention that the standard also defines other boolean keywords such as and (&&), bitand (&), or (||), bitor (|), xor (^)... They are called operator synonyms.

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

    According to Code Complete a book Jeff got his name from and holds in high regards the following is the way you should treat booleans.

    if (IsGood)
    if (!IsGood)
    

    I use to go with actually comparing the booleans, but I figured why add an extra step to the process and treat booleans as second rate types. In my view a comparison returns a boolean and a boolean type is already a boolean so why no just use the boolean.

    Really what the debate comes down to is using good names for your booleans. Like you did above I always phrase my boolean objects in the for of a question. Such as

    • IsGood
    • HasValue
    • etc.
    0 讨论(0)
  • 2020-12-02 20:54

    For some reason I've always liked

    if (IsGood)
    

    more than

    if (!IsBad)
    

    and that's why I kind of like Ruby's unless (but it's a little too easy to abuse):

    unless (IsBad)
    

    and even more if used like this:

    raise InvalidColor unless AllowedColors.include?(color)
    
    0 讨论(0)
  • 2020-12-02 20:56

    From the answers so far, this seems to be the consensus:

    1. The short form is best in most cases. (IsGood and !IsGood)
    2. Boolean variables should be written as a positive. (IsGood instead of IsBad)
    3. Since most compilers will output the same code either way, there is no performance difference, except in the case of interpreted languages.
    4. This issue has no clear winner could probably be seen as a battle in the religious war of coding style.
    0 讨论(0)
  • 2020-12-02 20:56

    It's possible (although unlikely, at least I hope) that in C code TRUE and FALSE are #defined to things other than 1 and 0. For example, a programmer might have decided to use 0 as "true" and -1 as "false" in a particular API. The same is true of legacy C++ code, since "true" and "false" were not always C++ keywords, particularly back in the day before there was an ANSI standard.

    It's also worth pointing out that some languages--particularly script-y ones like Perl, JavaScript, and PHP--can have funny interpretations of what values count as true and what values count as false. It's possible (although, again, unlikely on hopes) that "foo == false" means something subtly different from "!foo". This question is tagged "language agnostic", and a language can define the == operator to not work in ways compatible with the ! operator.

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