What is the best java syntax: if (isSomething () == false) {or if (! IsSomething ()) {

If you look

if (!x) {
if (x == false) {

X seems to be better, but

if (!isSomething()) {
if (isSomething() == false) {

You can easily watch!

What to do? Is there any recommendation?

+5
source share
4 answers

The hidden third option is to correctly specify your variables and methods.

Instead

if (!isDisabled()) {
    ...
}

using

if (isEnabled()) {
    ...
}

or if you want to check the negation:

boolean disabled = !isEnabled();
if (disabled) {
    ...
}

or add both methods:

boolean isDisabled() {
    return !isEnabled();
}

Edit: I found this question: Is it nice to explicitly compare with boolean constants, for example. if (b == false) in Java?

+12
source

if (!isSomething()) {. , "!" , :

if ( ! isSomething()) { if ( !isSomething()) {

,

if (isSomething() == false && isSomethingElse() == false && ..),

. "!" "not isSomething() isSomethingElse()".

+7

, - , .

-, , if (!isSomething()):)

, if (!x).

+3
if ( !isSomething() ) {

would be the best, in my opinion. Thus, you keep the countdown symbol down, your code is readable and that !really sticks out at the beginning, so just by looking at the code, others can see its intent.

+2
source

All Articles