I would lean towards
if (object == nil)
but I’ve noticed in some tutorials the use of
if (nil == object)
Is this just a style thing, or is there some justified rationale for using either format?
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.
This is typically done to prevent using an assignment operator instead of a comparison operator. If you accidently typed this for instance:
It may compile but it isn’t what you intended.
By using the second form you will ensure a compile time error if you mistype it, as nil cant be used as the left operand in assignments.
Please note I’m not an objective C programmer, but this question is generic to a lot of languages.