18

Most languages use the true/false keywords for boolean values. I found that even Smalltalk is using true/false. I know Objective-C is just borrowing concepts from Smalltalk, not the language itself, but I'm curious why it's using YES/NO instead of the more widely-used true/false. Is there any historical reason?

Facundo Casco
  • 10,065
  • 8
  • 42
  • 63
eonil
  • 83,476
  • 81
  • 317
  • 516

5 Answers5

35

Objective-C was designed to be (and still is) a strict superset of C. The creators worked very hard to ensure that they did not break compatibility with C in any way. They also tried to make their modifications somewhat obvious so that it would be easy to tell which parts of the code use Objective-C and which parts use plain C. Case in point, the @ used to denote NSStrings rather than just using quotes. This allows plain C strings to coexist with the new ones.

C already had an informal system of TRUE/FALSE macros. I suspect the designers of Objective-C chose the YES/NO macros to avoid conflict and to make it obvious that the code is actually Objective-C. Notice also the usage nil for the 'empty' object rather than just modifying the behavior of good old NULL.

Ken
  • 30,811
  • 34
  • 116
  • 155
Abhay Buch
  • 4,548
  • 1
  • 21
  • 26
  • +1 for an answer that actually makes sense. Most other answers to this question around SO just said "because YES is easier to understand/read then TRUE". – noamtm Jul 29 '12 at 07:16
4

Objective-C is a very verbose language, all methods are very descriptive, and using YES/NO for boolean values instead of true/false makes it more human readable.

You would probably find the following conversation strange, if it happened in real life: A: "Did you see the movie?" B: "True"

If B had answered "yes" (or "no"), it would sound perfectly normal, and code looks more like plain english by using YES/NO instead of true/false.

Morten Fast
  • 6,322
  • 27
  • 36
  • 2
    Hmm... I'm not so sure about the "plain english" explanation. If their intention is to provide clarity, wouldn't it make more sense to follow decades of convention? – FreeAsInBeer Mar 14 '12 at 15:21
  • But Objective-C has used that convention for decades? Oh, you meant other languages... ;) The majority isn't always right. If you were designing a language, wouldn't you prefer that the language was the best it could be, rather than as compatible with other languages as possible? Judging by the readability of Objective-C compared to other languages, my guess is that the designers valued plain English higher than syntactic resemblance with other languages. – Morten Fast Mar 14 '12 at 19:20
  • I see your point that the majority is not always right, but as a counter-example, I offer [Esperanto](http://en.wikipedia.org/wiki/Esperanto), a created language that would make conversation with almost any human being possible, however it has not really caught on yet even though it's over a century old. – FreeAsInBeer Mar 14 '12 at 19:41
3

Apple have always tried to make things easier to use. If you read some system boolean methods and ask yourself what makes more sense to answer a boolean question with, either using YES|NO or TRUE|FALSE, you'll see thank the answer is YES|NO in my opinion.

Otherwise you can always use TRUE|FALSE in your code.

2

The best way to think of this is that it's parallel evolution.

Objective-C's BOOL and YES/NO dates all the way back to early 1980s, and was likely intended to not only mimic other languages but miss C's future development. _Bool, true/false in C were only made part of the standard in 1999.

So are YES and NO historical? Yes. Are they only historical? No. Just as NULL is not the result of 3-3 in a pure sense (despite NULL often being defined as 0, or casually usable if it were), true is not a value for BOOL.

You would not (I think) write this code:

int matches = NULL;
for (int i = 0; i<count; ++i) {
    if (array[i] == value) ++matches;
}

This is less obviously wrong, but it's on the same spectrum:

BOOL foundMatch = false;
for (int i = 0; i<count; ++i) {
    if (array[i] == value) {
        foundMatch = YES;
        break;
    }
}
Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
2

It is strange, but I find code is more readable using the YES/NO macros rather than TRUE/FALSE (which also work).

However, Objective-C is a superset of C99 now, so you should be using the C99 boolean type and true and false wherever possible. I was toying with the idea of defining yes and no to true and false but have resisted it so far.

JeremyP
  • 84,577
  • 15
  • 123
  • 161