[java] Boolean checking in the 'if' condition

Which one is better Java coding style?

boolean status = true;
if (!status) {
    //do sth
} else {
    //do sth
}

or:

if (status == false) {
    //do sth
} else {
    //do sth
}

This question is related to java coding-style

The answer is


I would suggest that you do:

if (status) {
    //positive work
} else {
    // negative work
}

The == tests, while obviously redundant, also run the risk of a single = typo which would result in an assignment.


First style is better. Though you should use better variable name


If you look at the alternatives on this page, of course the first option looks better and the second one is just more verbose. But if you are looking through a large class that someone else wrote, that verbosity can make the difference between realizing right away what the conditional is testing or not.

One of the reasons I moved away from Perl is because it relies so heavily on punctuation, which is much slower to interpret while reading.

I know I'm outvoted here, but I will almost always side with more explicit code so others can read it more accurately. Then again, I would never use a boolean variable called "status" either. Maybe isSuccess or just success, but "status" being true or false does not mean anything to the casual reader intuitively. As you can tell, I'm very into code readability because I read so much code others have written.


This is more readable and good practice too.

if(!status){
//do sth
}else{
//do sth
}

The first one, or if (status) { /*second clause*/ } else { /* first clause */ }

EDIT

If the second form is really desired, then if (false == status) <etc>, while uglier, is probably safer (wrt typos).


The former. The latter merely adds verbosity.


The first one. But just another point, the following would also make your code more readable:

if (!status) {
    // do false logic
} else {
    // do true logic
}

Note that there are extra spaces between if and the (, and also before the else statement.

EDIT

As noted by @Mudassir, if there is NO other shared code in the method using the logic, then the better style would be:

if (!status) {
    // do false logic
}

// do true logic

My personal feeling when it comes to reading

if(!status) : if not status

if(status == false) : if status is false

if you are not used to !status reading. I see no harm doing as the second way.

if you use "active" instead of status I thing if(!active) is more readable


It really also depends on how you name your variable.

When people are asking "which is better practice" - this implicitly implies that both are correct, so it's just a matter of which is easier to read and maintain.

If you name your variable "status" (which is the case in your example code), I would much prefer to see

if(status == false) // if status is false

On the other hand, if you had named your variable isXXX (e.g. isReadableCode), then the former is more readable. consider:

if(!isReadable) { // if not readable
  System.out.println("I'm having a headache reading your code");
}