Using "!" is not more clear than "== NO" #7
Comments
If I ever have this: if (!([obj bool1] && ![obj bool2])) {
} I would do something like this: BOOL isMetabolicSymbol = ([obj bool1] && ![obj bool2]);
if (!isMetabolicSymbol) {
} Is WAY more readable than == NO. |
NSElvis, you missed a (I too prefer |
@NSElvis I'm a huge proponent of moving the final condition into an intermediate variable, it improves readability and debugging greatly. I don't generally proselytize for using I'm not suggesting the style guide changes teams and requires |
Well said, Jerry. The NYT team humors my refusal to use a preceding ! and I am grateful. I can't imagine how people think it is more readable. |
I'd add something on casting to BOOL as Mike Ash explained: |
I have to agree with the original point. I was quite surprised to read that "!" would allow "greater visual clarity". Instead, I like to be explicit, because it avoids confusion and adds clarity as well as readability and consistency. I mean, we check "if (i > 5)" or "if (somePointer == someOtherPointer)", so why do something different for booleans or nil values? Using a different syntax for these cases is not "consistency across files" as claimed :-) Here's what I like to do: if ([self methodReturningBoolean] == NO) {
}
else if ([self methodReturningBoolean] != NO) {
}
else if ([self methodReturningObject] == nil) {
}
else if ([self methodReturningObject] != nil) {
} I think this is a lot more readable, it is explicit, and there's no mistaking == for !=. Potentially, when I had to use the "!" because of some coworker who wouldn't hear of anything else, I like to make it more readable by adding spaces around it like so: if ( ! [self methodReturningBoolean]) {
}
else if ( ! booleanVariable) {
} |
Add podspec information
This is being discussed in #100 |
Closing as PR #100 has been merged; see #100 (comment) for details. |
While I'm not a zealot about convincing others to move away from using
!
in conditionals, I wouldn't ever give up my use of== NO
. Not only are inverted conditions typically harder to mentally process, using!
in fact often reduces visual clarity. Typically, this is the result of a lack of whitespace with something likeif (![object boolValue]) {
a critically important part of the condition is jumbled up with the delineation elements of the syntax. This gets worse as the complexity of your if condition increases (admittedly this is often a clarity problem in it's own rite).if (!([obj bool1] && ![obj bool2]) {
-- quick, what is the expected result?Truthfully, the primary reason I stopped using
!
(after vehemently arguing with a co-worker that he was silly for suggesting I avoid it) is because I caused myself a world of hurt once. During what seemed like a straight forward refactor, I managed to accidentally remove a!
, we didn't even catch it during code review -- it was just so easy to miss. This happened to be one of those insidious bugs that ultimately took hours to track down, and a fraction of a second to correct.Don't like
== NO
? Fine, don't use it -- but don't punish the defensive coders that would like to, especially not with an empty statement like "increases visual clarity".The text was updated successfully, but these errors were encountered: