Sunday 4 January 2009

self = [super init], WTF ?!?

On the Net you can find a lot of debates if you can (or may) do the following in the init method of an object. (Remember, an instantiation message like [aClass new] will send the init message to your freshly created instance of aClass).

So is this method correct or not ?

- (id) init
{
    self = [super init] ;
   if ( self == nil ) return nil ;
   return self ;
}

From a syntical point of view its correct. But to me as an ex-Smalltalk programmer, its bogus (even from a conceptual view).

Because, think of it, why would you change yourself with something that's returned from initialization of an object of the superclass ?
Self is at the moment of initialization an instance of the whole classtree of the class you instantiate.
You can even introduce (unwanted) side effects with this construction.

No comments:

Post a Comment