Re: [Obj-C] if (self) vs. if (self != nil)
Re: [Obj-C] if (self) vs. if (self != nil)
- Subject: Re: [Obj-C] if (self) vs. if (self != nil)
- From: Ben Kennedy <email@hidden>
- Date: Fri, 24 Feb 2012 13:47:09 -0800
On 24 Feb 2012, at 9:52 am, Scott Ribe wrote:
> Now there is one style that is worth defending, which is when comparing a variable to a constant, put the constant first. Consider the following:
>
> if(self == nil)...
> if(self = nil)...
> if(nil == self)...
> if(nil = self)...
>
> The 1st & 3rd are the correct conditions, the 2nd & 4th are typos. But the 2nd compiles and gives incorrect behavior, while the 4th fails to compile.
Fortunately the compiler (nowadays) warns in these situations, and suggests that one either fix the operator or enclose the expression in extra parentheses to clarify the intent. Personally I enjoy the latter.
b
--
Ben Kennedy, chief magician
Zygoat Creative Technical Services
http://www.zygoat.ca
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden