1

I see the following used occasionally and while I understand the meaning it looks oddly phrased.

if (nil == s)

It is used to check if an Objective-C variable was declared as nil. For me it would be more natural to write s == nill as s is what we are examining.

Just curios.

  • 2
    Duh! Just read that it is done this way to trap a common error of programer of assigning nil to s by typing s = nil. – irishetcher May 10 '14 at 15:49
  • isn't the most natural way if(!s) ? – Fermat's Little Student May 10 '14 at 15:50
  • 3
    You'll sometimes hear it called a "Yoda condition", for obvious reasons. Others may disagree, but I think they damage readability. They only work when one of the operands is a literal, and if you remember to write them backwards, you might as well just remember to write `==` instead of `=`. Any good modern compiler will warn you about this type of construction, rendering this bad cure fairly useless. – Crowman May 10 '14 at 15:56
  • Because it's like saying "if blue is the sky" or "if tall is the man". [SO Answer](http://stackoverflow.com/a/2430307/451475) (must have rep to view) – zaph May 10 '14 at 16:00
  • There is also the same question on programmers.stackexchange.com. Search for "Yoda". – Sulthan May 10 '14 at 16:07
  • 1
    @Will That's not natural at all! `if(!s)` relies on the principle that pointer is an integer and integer behaves like a boolean (or viceversa). C people understand that because it's common, it's not natural. Try to explain it to a beginner. – Sulthan May 10 '14 at 16:17
  • It's like the line from "Once Upon A Time In The West," "How can you trust a man who wears both a belt and suspenders? The man can't even trust his own pants." Same thing here, doing this silly nonsense when you don't trust your own compiler. Or yourself. – Extra Savoir-Faire May 10 '14 at 16:33
  • @Sulthan I would argue that using `if (!s)` is more natural. Read it in English - "if not s". Simple. That means "if s isn't set". Pretty clear to anyone in my opinion. – rmaddy May 10 '14 at 16:33
  • The problem with `if (!s)` is that `s` is not a boolean. Better: `if (s == nil)` since `s` is an object. – zaph May 10 '14 at 19:47

1 Answers1

2

This is used to make it harder to accidentally write if (s = nil) which will assign nil to s instead of performing a comparison. Accidentally writing if (nil = s) will not compile.

Sebastian Kirsche
  • 861
  • 2
  • 19
  • 36
  • 1
    In general this will now cause a compiler warning or error. Best to make the code human readable. It will be written once and read many times. – zaph May 10 '14 at 16:06
  • There are also other reasons - for example, in Java `"text".equals(stringVar)` works differently than `stringVar.equals("text")` if `stringVar` is `null`. Usually they only make the code less readable because the bugs they try to prevent are very easy to find and fix. However, they are still heavily used in some languages (e.g. PHP). – Sulthan May 10 '14 at 16:14
  • Thanks for all the feedback. Very informative. – irishetcher May 12 '14 at 13:44