Confusing If Statement?

前端 未结 13 879
-上瘾入骨i
-上瘾入骨i 2020-12-20 13:03

I always use If statement (In C#) as (1. Alternative);

if (IsSuccessed == true)
{
   //
}

I know that there is no need to write \"== true\"

相关标签:
13条回答
  • 2020-12-20 13:03

    I used to write "== true" because I thought it was clearer and more explicit, but decided to change. Now it always seems much clearer without, you'll just get used to it.

    0 讨论(0)
  • 2020-12-20 13:03

    I'll go for the second. It's easier for me at least. In the first alternative I always wonder why the comparison is made. Check the type of the left hand side just to be sure that no developer on acid overloaded the == operator making comparison between his class and bool an option.
    The first also leads to bugs the second won't.
    if(a) might need to be change to if(a||b) or if(a&&b) in the first version it might end up as if(a == true || b) and if(a == true && b) in the former b is redundant and the latter equals if(a==b)

    0 讨论(0)
  • 2020-12-20 13:04

    What i see most is: (what I do)

    if (IsSuccessed)
    {
       //
    }
    

    and as alternative for in C++, for C# it's not needed (see comment):

    if (true == IsSuccessed)
    {
       //
    }
    

    The alternative is to prevent yourself for making the mistake to assign instead of compare. (= vs ==)

    0 讨论(0)
  • 2020-12-20 13:09

    I'd personally go for the second option. It reads more naturally and shows that a programmer is actually aware of a built-in bool type, which is a first-class citizen.

    0 讨论(0)
  • 2020-12-20 13:10

    Totally style dependant. Seriously. Go with whatever you like for your own stuff, whatever is the enfoced style at your work.

    0 讨论(0)
  • 2020-12-20 13:11

    More typing means more chances for bugs. Option 2 all the way...

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