Re: Is "-init" really needed?

2017-08-07 Thread Jens Alfke

> On Aug 7, 2017, at 5:23 PM, Carl Hoefs  wrote:
> 
> Is the use of +new discouraged also?

No, I use it all the time. It’s simply shorthand for [[XXX alloc] init].

These days with ARC, I tend to use +new instead of factory class methods — e.g. 
[NSMutableArray new] instead of [NSMutableArray array] — on the assumption that 
this avoids adding the new object to the autorelease pool. However, it’s 
possible the compiler & runtime are smart enough to optimize the autorelease 
out of the second call. (Greg?)

—Jens
___

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/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-07 Thread Carl Hoefs

> On Aug 7, 2017, at 5:17 PM, じょいすじょん  
> wrote:
> 
>> 
>> On Aug 8, 2017, at 9:09, Jens Alfke  wrote:
>> 
>> 
>>> On Aug 7, 2017, at 5:02 PM, David Hoerl  wrote:
>>> 
>>> But then I though - heck, if Foo has NSObject as its super class, gee, 
>>> maybe -init isn't really need. I mean, if all of Foo's ivars and properties 
>>> are initialized, its a shortcut, right.
>> 
>> -[NSObject init] happens to be a no-op empty method. So if a direct subclass 
>> of NSObject has no -init method of its own, you could get by with just 
>> calling +alloc. However, I think this would be a really bad idea. If at some 
>> point you needed to add an -init method to class Foo, like to initialize an 
>> ivar, you’d have to go and fix all this code that wasn’t calling -init, or 
>> else you’d suddenly have a number of bugs in your code. Even worse, if 
>> someone else added the -init method and didn’t know about this quirk of how 
>> callers initialized Foo, they might have  no idea why their method didn’t 
>> get called. Yuck.
>> 
>> —Jens
>> ___
>> 
> It definitely should never pass in a code review for exactly these reasons 
> and should be fixed by either adding the init call or changing the alloc call 
> to a new call (since new is a synonym for alloc init).
> If you saw it pre-existing in code that was being checked in, require it to 
> be fixed. Refusal to type a few characters is absolutely a shortcut to 
> trouble later (Y2K).
> ___
> 

Is the use of +new discouraged also?

-Carl


___

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/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-07 Thread じょいすじょん

> On Aug 8, 2017, at 9:09, Jens Alfke  wrote:
> 
> 
>> On Aug 7, 2017, at 5:02 PM, David Hoerl  wrote:
>> 
>> But then I though - heck, if Foo has NSObject as its super class, gee, maybe 
>> -init isn't really need. I mean, if all of Foo's ivars and properties are 
>> initialized, its a shortcut, right.
> 
> -[NSObject init] happens to be a no-op empty method. So if a direct subclass 
> of NSObject has no -init method of its own, you could get by with just 
> calling +alloc. However, I think this would be a really bad idea. If at some 
> point you needed to add an -init method to class Foo, like to initialize an 
> ivar, you’d have to go and fix all this code that wasn’t calling -init, or 
> else you’d suddenly have a number of bugs in your code. Even worse, if 
> someone else added the -init method and didn’t know about this quirk of how 
> callers initialized Foo, they might have  no idea why their method didn’t get 
> called. Yuck.
> 
> —Jens
> ___
> 
It definitely should never pass in a code review for exactly these reasons and 
should be fixed by either adding the init call or changing the alloc call to a 
new call (since new is a synonym for alloc init).
If you saw it pre-existing in code that was being checked in, require it to be 
fixed. Refusal to type a few characters is absolutely a shortcut to trouble 
later (Y2K).
___

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/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-07 Thread Greg Parker

> On Aug 7, 2017, at 5:02 PM, David Hoerl  wrote:
> 
> I recently saw some code where an object was alloced but there was no init:
> 
>  Foo *foo = [Foo alloc];
>  foo.bar = ...
> 
> My blood pressure soared! My pulse quickened! I started breathing rapidly!
> 
> But then I though - heck, if Foo has NSObject as its super class, gee, maybe 
> -init isn't really need. I mean, if all of Foo's ivars and properties are 
> initialized, its a shortcut, right.

Pro:
* -[NSObject init] is in fact guaranteed to do nothing.

Con:
* As you noticed, independent of its correctness, the code above *looks* wrong. 
That alone is bad. It ought either to call -init or to have a big scary comment 
describing why it is safe not to.
* If Foo is ever changed to have a superclass other than NSObject, the code 
above is likely to be wrong.


-- 
Greg Parker gpar...@apple.com  Runtime 
Wrangler


___

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/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is "-init" really needed?

2017-08-07 Thread Jens Alfke

> On Aug 7, 2017, at 5:02 PM, David Hoerl  wrote:
> 
> But then I though - heck, if Foo has NSObject as its super class, gee, maybe 
> -init isn't really need. I mean, if all of Foo's ivars and properties are 
> initialized, its a shortcut, right.

-[NSObject init] happens to be a no-op empty method. So if a direct subclass of 
NSObject has no -init method of its own, you could get by with just calling 
+alloc. However, I think this would be a really bad idea. If at some point you 
needed to add an -init method to class Foo, like to initialize an ivar, you’d 
have to go and fix all this code that wasn’t calling -init, or else you’d 
suddenly have a number of bugs in your code. Even worse, if someone else added 
the -init method and didn’t know about this quirk of how callers initialized 
Foo, they might have  no idea why their method didn’t get called. Yuck.

—Jens
___

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/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Is "-init" really needed?

2017-08-07 Thread David Hoerl

I recently saw some code where an object was alloced but there was no init:

  Foo *foo = [Foo alloc];
  foo.bar = ...

My blood pressure soared! My pulse quickened! I started breathing rapidly!

But then I though - heck, if Foo has NSObject as its super class, gee, 
maybe -init isn't really need. I mean, if all of Foo's ivars and 
properties are initialized, its a shortcut, right.


Right?

[Still trying to get pulse down...]

D
___

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/archive%40mail-archive.com

This email sent to arch...@mail-archive.com