site_archiver(a)lists.apple.com
Delivered-To: cocoa-dev(a)lists.apple.com
Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=ax+xPf+ZcgI6lhaoRvHRe/KIjLhG9yxzCzw0f0UTuJI=; b=htiN9k8XO0HlEDwgnNwf/Uyo2+aihSJYlhFl0Ax01dOTG2F+hdoM9ok5EZaiMCrzdf zhkQ0BQgNh7yWNS7VbGz5tuuEHHUvj3fN4SUZ4DKFhmXKaxWr1JRDwA8vjiV6QE1llmJ FJfv8amvTo59m1citO47uEt18Od+I0X8Q7udNI2kJNSyOY/u99q53h+MI3b3kyGOy/9q FgkRJ6jUBApVSmhwMuYHq3PHLzg5hMxsZV+Fqe+4n4dJMxentDQeW93+eFD7AaGfHoLx /IzZzwwiIqn7I0SMYKJjIZ0Hu3cy2jg2WJtAfvaM1YEtZoXDPM0F2LtKuLp107UwAw8o pq/w==
> On Aug 8, 2017, at 9:09, Jens Alfke <jens(a)mooseyard.com> wrote:
>
>
>> On Aug 7, 2017, at 5:02 PM, David Hoerl <dhoerl(a)mac.com> 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(a)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.app…
This email sent to site_archiver(a)lists.apple.com
site_archiver(a)lists.apple.com
Delivered-To: cocoa-dev(a)lists.apple.com
> On Aug 7, 2017, at 5:02 PM, David Hoerl <dhoerl(a)mac.com> 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 gparker(a)apple.com <mailto:gparker@apple.com> Runtime
Wrangler
_______________________________________________
Cocoa-dev mailing list (Cocoa-dev(a)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.app…
This email sent to site_archiver(a)lists.apple.com
site_archiver(a)lists.apple.com
Delivered-To: cocoa-dev(a)lists.apple.com
Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc:x-sasl-enc; s=fm1; bh=NrawCi0/GZ6xl2l471 JT4yAUktxJkdcmBF0s3VRiqd4=; b=bAESL5XkIDc4nWZUpKEUgfMkdf6EnCTLZL IWP4RQ6IPHf5iAmeG5A03Tq1hcGj4Iha6IlDv7NtOUJnsRjTZr2uDrOyD2cO89uW sY0B6gMU/DRpV/gBa2oh4I3v0g83MPqCF6dA4iDfbAr5XXZtRYWqwdd1bshinOZ7 cEieir6nYhpNYjBnpC72Kvk704SFqTNkTlcQhBUK5/USy5e0wakZXSvPZCRdowbW mqLtEmPxI+mfIP4Y9gaCrgiLM7FbYFqskqcQOBJ+rlhsNILEnjF0cxUKbRUAVeIJ HaF7mB+riAQVkMDeoz9Fpr31j0lFFEkoYtMWdwShUh3l8mP/hA+Q==
> On Aug 7, 2017, at 5:02 PM, David Hoerl <dhoerl(a)mac.com> 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(a)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.app…
This email sent to site_archiver(a)lists.apple.com
site_archiver(a)lists.apple.com
Delivered-To: cocoa-dev(a)lists.apple.com
Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mac.com; s=04042017; t=1502150554; bh=RIUZFz3HBW1LbK2p2ZASTfUt3XjkQn2i1KKTSzNO/RA=; h=To:From:Subject:Message-id:Date:MIME-version:Content-type; b=DcjdTZ7ESOtw/xq2rHjbOYmj/qp0aAdIBW8JfVZ8RxyWMSHDVCdt8RrJiHuQYauZA GM1FDIFlXqr8TiFKdysEK6OVaiJinkZjf+mPr1NGw5Wl8mV6fDyHLPrPaob1UVv5W/ NlBDKb+OG1v7bq/GOlzvmz3EwwFW0Mfa3LOhySvrvSoOBJYx5m3bbOyLB/7Prh2mzC tR+N7t09Z9FbDnsjON6FlqRYjMcm0e7s//CU+Ew1PcmauxzhasHV0PS9oRYFCqPQl4 hy94dQPMVU76u4jjXY7njDAa/osAsYGHvSs62EAWNxh8P9vYMYSoiE4k1ss38HVVf0 Q53EeU8PB/O0Q==
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.2.1
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!
Right?
[Still trying to get pulse down...]
D
_______________________________________________
Cocoa-dev mailing list (Cocoa-dev(a)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.app…
This email sent to site_archiver(a)lists.apple.com
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.