Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Using "!" is not more clear than "== NO" #7

Closed
jerryhjones opened this issue Aug 2, 2013 · 8 comments
Closed

Using "!" is not more clear than "== NO" #7

jerryhjones opened this issue Aug 2, 2013 · 8 comments
Labels

Comments

@jerryhjones
Copy link

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 like if (![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".

@3lvis
Copy link
Contributor

3lvis commented Aug 2, 2013

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.

@regexident
Copy link

NSElvis, you missed a ! in your "cleaned-up" snippet, which kind of contradicts your point.

(I too prefer ! over == NO, though)

@jerryhjones
Copy link
Author

@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 == NO, but it does offer a small benefit in the form of defensive coding. The argument that ! is more readable is rather tenuous, especially when you consider how common it is to compare a value in a condition.

I'm not suggesting the style guide changes teams and requires == NO; I'd just like to see them keep the option on the table for the more paranoid folks.

@paulbruneau
Copy link
Contributor

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.

@murdocdv
Copy link
Contributor

murdocdv commented Aug 3, 2013

I'd add something on casting to BOOL as Mike Ash explained:
http://www.mikeash.com/pyblog/friday-qa-2012-12-14-objective-c-pitfalls.html

@thibault-ml
Copy link

I have to agree with the original point. I was quite surprised to read that "!" would allow "greater visual clarity".
The character is such a small one that it is easy to miss, on reading, or on code review, especially between a "(" and a "["

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) {
}

ayanonagon added a commit to venmo/objective-c-style-guide that referenced this issue Jan 20, 2015
@mattbischoff
Copy link
Contributor

This is being discussed in #100

@cdzombak
Copy link
Contributor

Closing as PR #100 has been merged; see #100 (comment) for details.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants