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: Wade Tregaskis <email@hidden>
- Date: Fri, 24 Feb 2012 15:34:31 -0800
> 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.
>
> Of course if(self) is not subject to that kind of typo, but if you're going to insist on the verbose version, you might as well use if(nil == self).
Scott makes an excellent point. I just want to emphasise it by saying how useful that has been to me over the years - countless bugs killed at birth.
I will admit it took me a while to get used to it, but the sooner you stop thinking about it the sooner it's natural. You may run into people who dislike it on religious grounds, however. For example, Google's internal style rules forbid it. Try to make sure you're paid by bug count, if you find yourself in that situation. ;)
_______________________________________________
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