Re: Is "-init" really needed?
site_archiver@lists.apple.com Delivered-To: cocoa-dev@lists.apple.com Dkim-signature: v=1; a=rsa-sha1; c=relaxed; d=breaqz.com; h=content-type :mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; s= breaqz.com; bh=QbEm6sx6tsrIwbbIcCZzfUGE3XE=; b=nseg1N862uD6j270b RN0QlqghsLmWIRVfhKqxtsMnQYNeBZJFTRrjpgNsq9MGd8pkF668/ktJ/NkJlX8u 1fmN1LieeIVvVZfS9W7NAthFw0Nv0HrqLYeM0O4SWiRkWx2pYr/NSsvsZPt2I9Dm a7UtYHADMcy5cxD0Asd9A1+Dao=
On Aug 9, 2017, at 3:01 AM, Alastair Houghton <alastair@alastairs-place.net> wrote:
On 8 Aug 2017, at 17:38, Doug Hill <cocoadev@breaqz.com> wrote:
As others have mentioned, I too have never seen any evidence or statements from Apple that discourages +new or -init.
I suspect it was ObjC programmers themselves rather than Apple/NeXT that discouraged it. As for why, well I can imagine a few reasons:
- Performance - it incurs an extra message send (which would have been an issue back in the day)
+new requires no extra message. It's just a shorthand for [[SomeClassname alloc] init]
- Clarity - [[… alloc] init] shows clearly that it’s a two step operation (some classes support being *re*-initialized, so you can call initialisers more than once; other classes don’t actually need initialising)
Some say that it's far more confusing and hard to read the alloc/init syntax.
- If +new was the way to go, you’d need variants of +new for each variant of -init (or you have to use [[… alloc] init] anyway)
There has never been an issue with this. +new saves you some typing for one syntax but has no impact on anything else.
- The fact that convenience constructors were often written naming the object, e.g. [NSString stringWithFormat:…], [NSArray array]. +new would duplicate that, but isn’t as nice to read or look at. OK, +new doesn’t autorelease, but still.
With ARC, autorelease behavior is essentially hidden from the developer and doesn't really matter any more. Again, +new is unrelated to all the other class methods. Doug Hill _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/site_archiver%40lists.appl... This email sent to site_archiver@lists.apple.com
participants (1)
-
Doug Hill