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

后端 未结 30 2273
礼貌的吻别
礼貌的吻别 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:37

    I prefer to use:

    if (IsGood)
    {
        DoSomething();
    }
    

    and

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

    as I find this more readable - the ! is just too easy to miss (in both reading and typing); also "if not IsGood then..." just doesn't sound right when I hear it, as opposed to "if IsGood is false then...", which sounds better.

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

    I would prefer shorter variant. But sometimes == false helps to make your code even shorter:

    For real-life scenario in projects using C# 2.0 I see only one good reason to do this: bool? type. Three-state bool? is useful and it is easy to check one of its possible values this way.

    Actually you can't use (!IsGood) if IsGood is bool?. But writing (IsGood.HasValue && IsGood.Value) is worse than (IsGood == true).

    Play with this sample to get idea:

    bool? value = true; // try false and null too
    
    if (value == true)
    {
        Console.WriteLine("value is true");
    }
    else if (value == false)
    {
        Console.WriteLine("value is false");
    }
    else
    {
        Console.WriteLine("value is null");
    }
    

    There is one more case I've just discovered where if (!IsGood) { ... } is not the same as if (IsGood == false) { ... }. But this one is not realistic ;) Operator overloading may kind of help here :) (and operator true/false that AFAIK is discouraged in C# 2.0 because it is intended purpose is to provide bool?-like behavior for user-defined type and now you can get it with standard type!)

    using System;
    
    namespace BoolHack
    {
        class Program
        {
            public struct CrazyBool
            {
                private readonly bool value;
    
                public CrazyBool(bool value)
                {
                    this.value = value;
                }
    
                // Just to make nice init possible ;)
                public static implicit operator CrazyBool(bool value)
                {
                    return new CrazyBool(value);
                }
    
                public static bool operator==(CrazyBool crazyBool, bool value)
                {
                    return crazyBool.value == value;
                }
    
                public static bool operator!=(CrazyBool crazyBool, bool value)
                {
                    return crazyBool.value != value;
                }
    
                #region Twisted logic!
    
                public static bool operator true(CrazyBool crazyBool)
                {
                    return !crazyBool.value;
                }
    
                public static bool operator false(CrazyBool crazyBool)
                {
                    return crazyBool.value;
                }
    
                #endregion Twisted logic!
            }
    
            static void Main()
            {
                CrazyBool IsGood = false;
    
                if (IsGood)
                {
                    if (IsGood == false)
                    {
                        Console.WriteLine("Now you should understand why those type is called CrazyBool!");
                    }
                }
            }
        }
    }
    

    So... please, use operator overloading with caution :(

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

    You forgot:

    if(IsGood == FileNotFound)

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

    Occasionally it has uses in terms of readability. Sometimes a named variable or function call can end up being a double-negative which can be confusing, and making the expected test explicit like this can aid readability.

    A good example of this might be strcmp() C/C++ which returns 0 if strings are equal, otherwise < or > 0, depending on where the difference is. So you will often see:

    if(strcmp(string1, string2)==0) { /*do something*/ }
    

    Generally however I'd agree with you that

    if(!isCached)
    {
        Cache(thing);
    }
    

    is clearer to read.

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

    It seems to me (though I have no proof to back this up) that people who start out in C#/java type languages prefer the "if (CheckSomething())" method, while people who start in other languages (C++: specifically Win32 C++) tend to use the other method out of habit: in Win32 "if (CheckSomething())" won't work if CheckSomething returns a BOOL (instead of a bool); and in many cases, API functions explicitly return a 0/1 int/INT rather than a true/false value (which is what a BOOL is).

    I've always used the more verbose method, again, out of habit. They're syntactically the same; I don't buy the "verbosity irritates me" nonsense, because the programmer is not the one that needs to be impressed by the code (the computer does). And, in the real world, the skill level of any given person looking at the code I've written will vary, and I don't have the time or inclination to explain the peculiarities of statement evaluation to someone who may not understand little unimportant bits like that.

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

    We tend to do the following here:

    if(IsGood)
    

    or

    if(IsGood == false)
    

    The reason for this is because we've got some legacy code written by a guy that is no longer here (in Delphi) that looks like:

    if not IsNotGuam then
    

    This has caused us much pain in the past, so it was decided that we would always try to check for the positive; if that wasn't possible, then compare the negative to false.

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