What is the preferred way to write boolean expressions in Java

前端 未结 9 1997
灰色年华
灰色年华 2021-01-18 05:03

I have always written my boolean expressions like this:

if (!isValid) {
  // code
}

But my new employer insists on the following style:

相关标签:
9条回答
  • 2021-01-18 05:24

    I just want to say I learned C twenty years ago in school and have moving onto Perl and Java and now C# which all have the same syntax and...

    I think (!myvar) is the most popular

    I think (myvar==false) is just fine too

    in 20 years i have NEVER EVEN SEEN

    (false==myvar) 
    

    I think your boss is smoking something-- I'm sorry but I'd take this as a sign your boss is some kind of control freak or numbskull.

    0 讨论(0)
  • 2021-01-18 05:25

    IMO the first one is much more readable while the second one more verbose.

    I would surely go for the 1st one

    0 讨论(0)
  • 2021-01-18 05:32

    It was discussed for C# several hours ago.

    The false == isValid construct is a leftover from C-world, where compiler would allow you to do assignments in if statement. I believe Java compilers will warn you in such case.

    Overall, second option is too verbose.

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