Why generate YES and NO via conditional operator vs. using condition directly?

cocoa, objective-c

Solution

It is just poor programming style/understanding. Don't do it.

It is often seen in the work of students who don't properly understand that booleans are values (as they are not numbers and they equate values with numbers) and expressions can be boolean valued.

Note: The boolean/logical operators in (Objective-)C(++) are defined to return integers rather than booleans, so some confusion over booleans is understandable in these languages. However the integers are either 0 or 1 and correspond to false/NO & true/YES respectively.

Problem

In Cocoa's NSRange.h, I noticed the following inline function: ``` NS_INLINE BOOL NSLocationInRange(NSUInteger loc, NSRange range) { return (!(loc < range.location) && (loc - range.location) < range.length) ? YES : NO; } ``` I found it rather perplexing that the author chose to return YES and NO via a conditional operator instead of writing the function like: ``` NS_INLINE BOOL NSLocationInRange(NSUInteger loc, NSRange range) { return (!(loc < range.location) && (loc - range.location) < range.length); } ``` Is there any reason why the former is preferable? I normally would consider this to just be a quirky programming style, but wondered (possibly erroneously) if there was something I'm missing since it's in one of Apple's public .h files...

Original source