if (boolean == false) vs. if (!boolean)
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.
ateiob
Updated on April 11, 2020Comments
-
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 almost 12 yearsThis could be a truly enlightening answer, if I only understood where you see a
Boolean
. All I see there is aboolean
. Please explain. -
ateiob almost 12 yearsExcellent. 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 aConcurrentMap
? Oh wait,ConcurrentMap
is an interface! Let me check this, I never came acrossConcurrentMap
before. -
Vishy almost 12 yearsInteresting, 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 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 over 7 yearsI 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 almost 5 yearsthis is 100% preference