if (boolean == false) vs. if (!boolean)

115,300

Solution 1

Apart from "readability", no. They're functionally equivalent.

("Readability" is in quotes because I hate == false and find ! much more readable. But others don't.)

Solution 2

Mostly READABILITY. When reading others code, it is much more intuitive to read as NOT CONTAINS KEY !values.containsKey(NoteColumns.CREATED_DATE) instead of reading CONTAINS KEY IS FALSE (values.containsKey(NoteColumns.CREATED_DATE) == false).

Solution 3

This is a style choice. It does not impact the performance of the code in the least, it just makes it more verbose for the reader.

Solution 4

- Here its more about the coding style than being the functionality....

- The 1st option is very clear, but then the 2nd one is quite elegant... no offense, its just my view..

Solution 5

No. I don't see any advantage. Second one is more straitforward.

btw: Second style is found in every corners of JDK source.

Share:
115,300
ateiob
Author by

ateiob

Updated on April 11, 2020

Comments

  • ateiob
    ateiob about 4 years

    Possible Duplicate:
    Is it bad to explicitly compare against boolean constants e.g. if (b == false) in Java?

    In this NotePadProvider sample code, I noticed that the author chose the form:

        if (values.containsKey(NoteColumns.CREATED_DATE) == false) {
            values.put(NoteColumns.CREATED_DATE, now);
        }
    

    Over:

        if (!values.containsKey(NoteColumns.CREATED_DATE)) {
            values.put(NoteColumns.CREATED_DATE, now);
        }
    

    Is there any advantage in the first form over the more logical one?

  • ateiob
    ateiob almost 12 years
    This could be a truly enlightening answer, if I only understood where you see a Boolean. All I see there is a boolean. Please explain.
  • ateiob
    ateiob almost 12 years
    Excellent. I love this. Since ConcurrentMap isn't derived from ContentValues and there isn't a direct connection between the two, do you have any suggestions to convert the passed ContentValues parameter to a ConcurrentMap? Oh wait, ConcurrentMap is an interface! Let me check this, I never came across ConcurrentMap before.
  • Vishy
    Vishy almost 12 years
    Interesting, it doesn't even extend Map and I am not sure its thread safe. You could write a putIfAbsent(ContentValues, NoteColumns.CREATED_DATE, now) method. ;)
  • Sam
    Sam almost 12 years
    "In this concrete case it really doesn't matter." As you rightly point out, there is no Boolean shown above. My concern was for future code which might return to you a Boolean type and might be null. If you believe this will, "never happen," then this answer isn't very useful.
  • user2481095
    user2481095 over 7 years
    I think it even goes beyond the readability argument. ! (the bang operator) was created for this purpose. You should use it because it helps you write these statements in a succinct manner. You CAN write code using a more limited range of operators but your code will suck from a readability standpoint and won't be following conventions everyone uses.
  • McHat
    McHat almost 5 years
    this is 100% preference