What is the preferred way to write boolean expressions in Java

前端 未结 9 1998
灰色年华
灰色年华 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: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.

提交回复
热议问题