Subject: Re: Is "-init" really needed?



On 10 Aug 2017, at 00:28, Doug Hill <[email protected]> wrote:
>
>> - 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]

Er, yes it does. It sends a +new message to the class object, which, in turn,
sends -alloc and -init messages. So that’s three messages rather than two.
It’s no big deal on current machines, but it would have been back on the old
68K boxes. It isn’t just syntax.

>> - 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.

:-D Some people say all kinds of things. Anyway, you’ve misunderstood my list
as *my* arguments. I don’t necessarily agree with all of them - I was just
pondering why it might have been discouraged.

>> - 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.

Except it isn’t just syntax, and having +new but then e.g. +stringWithFormat
while not having +newWithFormat: makes little sense.

>> - The fact that convenience constructors were often written naming the
>> object, e.g. [NSString stringWithFormat:…], [NSArray array]. +new would
>> duplica...

te 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.

Agreed. But I wasn’t talking about a time in which ARC even existed. I was
trying to suggest reasons why it might have been discouraged in the past.

Personally I *would* still discourage +new in favour of class-specific
convenience constructors because I think it’s less expressive and also less
consistent (e.g. +array is better, in my opinion, than +new, not least because
+arrayWithObjects: and others exist).

Kind regards,

Alastair.

--
http://alastairs-place.net

_______________________________________________

Cocoa-dev mailing list ([email protected])

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/maillists%40codeha.us

This email sent to [email protected]



Programming list archiving by: Enterprise Git Hosting