I see some code in Nimbus look like this:
if (nil == someObject)
but I usually type:
if (someObject == nil)
Are there any differences in these statements?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Technically no. The former, Nimbus, is using what is endearingly called “Yoda Conditions”.
The name of the game being foolproofing here. See, the problem is that this:
is totally valid, only one character away from
== nil, and really easy to miss. However, if you attempt to do this:your compiler will freak out, preventing the issue.
Personally, I hate Yoda Conditionals, as I think they’re hard to read. It does mean being extra careful with my code, but hey, I’m the better for it, right? It all comes down to style here, so whatever makes you more comfortable, go for.
Oh, and if you’re using Xcode, this is nearly a moot point. If you check out this question, you’ll see that Xcode now warns you if you attempt to do an assignment within an
ifwithout extra parens. That is,making the issue much harder to miss.