FROM : Adhamh Findlay
DATE : Sun Feb 10 19:41:40 2008
I'm no expert, but here is my take.
This argument is not able what is correct, but about style. Shipley
is saying you're wasting 7 characters with the self = [super init]
assignment because [super init] does the assignment for you. Shipley
is correct that you don't need to do the assignment, but I don't
believe doing the assignment is ever going to cause problems, its just
extra.
But what's more important, writing correct code or writing code that
is clear and correct? Just because you don't have to do something, if
you do it does that make the code incorrect? I think clear and
correct is better and doing something extra doesn't make the code
wrong if its more clear.
Is it more clear to write this:
[super init];
if(self != nil)
or
self = [super init];
if(self != nil)
I think it is the second because its explicit.
My guess is that the majority of Cocoa programmers go the assignment
route because that's what they've been taught. When you are teaching
your class you should tell your students that they don't have to do
the assignment, but that by convention most Cocoa programmers do.
That way they are informed about how things actual work.
Adhamh
On Feb 10, 2008, at 11:35 AM, Scott Andrew wrote:
> One point of research (going through my books.)
>
> The Cocoa with Objective-C book shows not assigning self.. Arg.. So
> where do we get in the habit of assigning self? Is it bad code
> practice? I know i have done it for the past 4 years out of habit
> and learning from example code and other materials.
>
> Scott
>
> On Feb 10, 2008, at 9:18 AM, Scott Andrew wrote:
>
>> Ok. Myself, like most Cocoa engineers have learned to write
>> initializers like so:
>>
>> -(id) init
>> {
>> self = [super init];
>>
>> if (self != nil)
>> {
>> .. do my init
>> }
>>
>> return self;
>> }
>>
>> So my question is:
>>
>> As i get ready to teach non-cocoa programmers in my company about
>> cocoa programming, and having read Will Shipley's blog, Is the
>> above the way to teach writing an initializer? I see Will, and a
>> few others, say no [super init] will initialize self in its code
>> there is no reason to assign self. Yet all code and docs I see have
>> similar code.
>>
>> Arguments for and against? I know old debate.... ;-)
>>
DATE : Sun Feb 10 19:41:40 2008
I'm no expert, but here is my take.
This argument is not able what is correct, but about style. Shipley
is saying you're wasting 7 characters with the self = [super init]
assignment because [super init] does the assignment for you. Shipley
is correct that you don't need to do the assignment, but I don't
believe doing the assignment is ever going to cause problems, its just
extra.
But what's more important, writing correct code or writing code that
is clear and correct? Just because you don't have to do something, if
you do it does that make the code incorrect? I think clear and
correct is better and doing something extra doesn't make the code
wrong if its more clear.
Is it more clear to write this:
[super init];
if(self != nil)
or
self = [super init];
if(self != nil)
I think it is the second because its explicit.
My guess is that the majority of Cocoa programmers go the assignment
route because that's what they've been taught. When you are teaching
your class you should tell your students that they don't have to do
the assignment, but that by convention most Cocoa programmers do.
That way they are informed about how things actual work.
Adhamh
On Feb 10, 2008, at 11:35 AM, Scott Andrew wrote:
> One point of research (going through my books.)
>
> The Cocoa with Objective-C book shows not assigning self.. Arg.. So
> where do we get in the habit of assigning self? Is it bad code
> practice? I know i have done it for the past 4 years out of habit
> and learning from example code and other materials.
>
> Scott
>
> On Feb 10, 2008, at 9:18 AM, Scott Andrew wrote:
>
>> Ok. Myself, like most Cocoa engineers have learned to write
>> initializers like so:
>>
>> -(id) init
>> {
>> self = [super init];
>>
>> if (self != nil)
>> {
>> .. do my init
>> }
>>
>> return self;
>> }
>>
>> So my question is:
>>
>> As i get ready to teach non-cocoa programmers in my company about
>> cocoa programming, and having read Will Shipley's blog, Is the
>> above the way to teach writing an initializer? I see Will, and a
>> few others, say no [super init] will initialize self in its code
>> there is no reason to assign self. Yet all code and docs I see have
>> similar code.
>>
>> Arguments for and against? I know old debate.... ;-)
>>
| Related mails | Author | Date |
|---|---|---|
| Scott Andrew | Feb 10, 18:18 | |
| Scott Andrew | Feb 10, 18:35 | |
| Adhamh Findlay | Feb 10, 19:41 | |
| John Newlin | Feb 10, 20:13 | |
| Hans van der Meer | Feb 10, 20:20 | |
| Quincey Morris | Feb 10, 20:29 | |
| Jens Alfke | Feb 10, 20:45 | |
| Wade Tregaskis | Feb 10, 20:50 | |
| Ricky Sharp | Feb 10, 21:01 | |
| Jens Alfke | Feb 10, 22:44 | |
| Hans van der Meer | Feb 10, 23:26 | |
| Paul Bruneau | Feb 10, 23:29 | |
| Uli Kusterer | Feb 10, 23:40 | |
| Paul Bruneau | Feb 11, 00:18 | |
| Scott Andrew | Feb 11, 00:56 | |
| Quincey Morris | Feb 11, 02:32 | |
| mmalc crawford | Feb 11, 02:49 | |
| Jens Alfke | Feb 11, 03:05 | |
| Bill Bumgarner | Feb 11, 03:28 | |
| Quincey Morris | Feb 11, 04:35 | |
| Jean-Daniel Dupas | Feb 11, 10:27 | |
| Jens Alfke | Feb 11, 17:08 | |
| Ben Trumbull | Feb 11, 23:58 |






Cocoa mail archive

