I have always written my if statements like this when I want the negative condition to enter me into the if block.
Example 1
if(condition == false){}
However, we just hired a new senior on the team that insists we should refactor to
Example 2
if(!condition){}
and use that moving forward.
I find example #1 to be easier to reason especially when checking a nested property.
e.g. if(person.name.middle.hasValue == false){}
Question What example is better to practice? or is there a better practice than either example?
Edit Scope
please limit answers to Negative conditions only. of course if(condition == true){}
is worst than if(condition){}
Because the == true
is redundent for the true case but not the false case.
condition == true
is redundant, but== false
is the same as!
- if you leave it out, you'll get wrong behaviour.if(!person.name.middle.hasValue)
exactly for what it is. If your find that hard to read, the better solution is to eliminate some of the dots, not tack= false
onto the end of it.!person.hasMiddleName
? - introduce new method/property in Person class to encapsulate child members.!
and== false
are interchangeable), not redundant (which would mean thatcondition == false
andcondition
are exactly the same)