Is it bad to explicitly compare it with Boolean constants, for example. if (b == false) in Java?

Is it bad to write:

if (b == false) //... while (b != true) //... 

It is always better to write:

 if (!b) //... while (!b) //... 

Presumably, there is no difference in performance (or is there?), But how do you weigh explanatory, brevity, clarity, readability, etc. between them?

Update

To limit subjectivity, I also appreciate any quotes from authoritative coding style rules that are always preferable or which should be used when.




Note: the variable name b used as an example, ala foo and bar .

+66
java coding-style boolean
Apr 18 '10 at 4:15
source share
14 answers

This is not necessarily bad, it is just superfluous. In addition, the actual variable name is very different. I would prefer, for example, if (userIsAllowedToLogin) above if (b) or even worse if (flag) .

As for performance, the compiler optimizes it in some way.

Update : as for authoritative sources, I cannot find something explicitly in the Sun Coding Conventions , but at least Checkstyle has a SimplifyBooleanExpression module that would warn about this.

+58
Apr 18 '10 at 4:18
source share

You cannot use the first style. I saw how people used:

  • if ( b == true )
  • if ( b == false )

It’s hard for me personally to read, but it's passable. However, the big problem I encounter in this style is that it leads to incredibly conflicting examples that you showed:

  • if ( b != true )
  • if ( b != false )

It requires more effort on the part of the reader to determine the intentions of the authors. Personally, I believe that an explicit comparison with true or false is redundant and therefore more difficult to read, but that I am.

+46
Apr 18 '10 at 4:23
source share

It depends a lot on the taste.

Personally, I found that if (!a) { much less readable (EDIT: for me) than if (a == false) { and therefore is more prone to errors when saving code later, and I converted it to the last form.

Basically, I don’t like the choice of characters for logical operations instead of words (C compared to Pascal), because for me a = 10 and not b = 20 is easier to read than a == 10 && !(b==20) , but the way it is in Java.

Anyone who puts the "== false" approach aside "!" obviously never stared at the code for too long and missed this exclamation point. Yes, you can get a blind code.

+31
Apr 18 2018-10-18T00:
source share

The main reason why you should not use the first style is that both of them are valid:

 if (b = false) //... while (b = true) //... 

That is, if you accidentally leave one character, you will create a task instead of a comparison. The assignment expression evaluates to the value that was assigned, so the first statement above assigns false - b and evaluates to false . The second sets true b , so it always evaluates to true , no matter what you do with b inside the loop.

+23
Apr 18 2018-10-18T00:
source share

I never saw the first, except for the code written by beginners; it is always the last, and I do not think that anyone is really embarrassed by this. On the other hand, I think

 int x; ... if(x) //... 

against

 if(x != 0) //... 

much more controversial, in which case I prefer the second

+11
Apr 18 '10 at 4:18
source share

IMHO, I think if you just make the bool variable names added with "Is" it will be self-evident and more meaningful, and then you can remove the explicit comparison with true or false

Example:

 isEdited // use IsEdited in case of property names isAuthorized // use IsAuthorized in case of property names 

etc.

+7
Apr 18 2018-10-18T00:
source share

I prefer the first because it is clearer. A machine can read equally well, but I'm trying to write code for other people to read, not just a machine.

+5
Apr 18 '10 at 4:18
source share

I prefer the long approach, but I compare using == instead of != 99% of the time.

I know this question is about Java, but I often switch between languages, and in C# , for example, comparing with (for isntance) == false can help when working with nullable bool types. Therefore, I got this habit of comparing with true or false , but using the == operator.

I'm doing it:

if(isSomething == false) or if(isSomething == true)

but I hate them:

if(isSomething != false) or if(isSomething != true)

for obvious reasons of readability!

As long as you keep your code readable, that doesn't matter.

+3
May 21 '13 at 03:19
source share

Personally, I would reorganize the code, so I do not use a negative test. eg.

 if (b == false) { // false } else { // true } 

or

 boolean b = false; while(b == false) { if (condition) b = true; } 

IMHO, In 90% of cases, the code can be reorganized, so a negative test is not required.

+2
Apr 18 2018-10-18T00:
source share

In my opinion, this is just annoying. Not that I could make a noise though.

+1
Apr 18 '10 at 4:18
source share

The normal rule is to never check for a boolean value. Some argue that additional verbosity adds clarity. The added code may help some people, but each reader will need to read more code.

This morning I lost half an hour to find a mistake. Code was

  if ( !strcmp(runway_in_use,"CLOSED") == IPAS_FALSE) printf(" ACTIVE FALSE \n"); else printf(" ACTIVE TRUE \n"); 

If it were encoded with the usual convention, I would see much faster that it was wrong:

  if (strcmp(runway_in_use, "CLOSED")) printf(" ACTIVE FALSE \n"); else printf(" ACTIVE TRUE \n"); 
+1
07 Feb '13 at 14:35
source share

This is my first answer to StackOverflow, so be nice ... Recently, during refactoring, I noticed that 2 blocks of code have almost the same code, but one of them was used

 for (Alert alert : alerts) { Long currentId = alert.getUserId(); if (vipList.contains(currentId)) { customersToNotify.add(alert); if (customersToNotify.size() == maxAlerts) { break; } } } 

and the other

 for (Alert alert : alerts) { Long currentId = alert.getUserId(); if (!vipList.contains(currentId)) { customersToNotify.add(alert); if (customersToNotify.size() == maxAlerts) { break; } } } 

so in this case it makes sense to create a method that worked for both conditions like this, using boolean == condition to flip the value

 private void appendCustomersToNotify(List<Alert> alerts List<Alert> customersToNotify, List<Long> vipList, boolean vip){ for (Alert alert : alerts) { Long currentId = alertItem.getUserId(); if (vip == vipList.contains(currentId)) { customersToNotify.add(alertItem); if (customersToNotify.size() == maxAlerts) { break; } } } } 
+1
Apr 27 '18 at 11:54
source share

I would say that this is bad.

 while (!b) { // do something } 

reads much better than

 while (b != true) { // do something } 
0
May 30 '10 at 20:27
source share

One of the reasons why the first (b == false) is underestimated is because newbies often do not understand that the second alternative (! B) is possible at all. Therefore, the use of the first form may indicate a misconception with Boolean expressions and logical variables. Thus, the use of the second form became a kind of sjiboleth: when someone writes this, he probably understands what is happening.

I believe that this made the difference deemed more important than it really is.

0
07 Oct '15 at 9:22
source share



All Articles