Why check self != nil in -init when messaging nil has no effect?

Viewed 396

Assuming our -init method only invokes messages on self, why is it common to check if self != nil if messaging nil has no effect?

Let's say we have an initializer as follows:

- (id)init
{
    self = [super init];
    if (self) {
        [self doThis];
        [self setFoo:@"Bar"];
    }

    return self;
}

Instead of checking self, we could write:

- (id)init
{
    self = [super init];
    [self doThis];
    [self setFoo:@"Bar"];

    return self;
}

Now if for some reason [super init] returns nil, there would be no difference in the outcome of the method as far as I know. Why then do we constantly perform this check?

2 Answers
Related