quinntaylor / CHDataStructures

An Objective-C library of data structures, such as deque, heap, linked list, queue, stack, and tree.
Other
387 stars 78 forks source link

Designated-Convenience initializer scheme #9

Closed ghost closed 3 years ago

ghost commented 9 years ago

Reading the source code, I've noticed the following (CHListStack.m):

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

- (id) initWithArray:(NSArray*)anArray {
    if ([self init] == nil) return nil;
    for (id anObject in anArray) {
        [list prependObject:anObject];
    }
    return self;
}

According to this code the designated initializer is (-init) while (-initWithArray:) is the convenience one. So, shouldn't it be on the contrary? (-init) will be convenience initializer that will supply a default parameter to (-initWithArray:) that will be a designated initializer?

Apple Documentation states "The initializer of a class that takes the full complement of initialization parameters is usually the designated initializer". The reason I've created this issue is that the current designated-convenience initializers scheme may be confusing to some developers.

So, that code may be converted into this one:

// Convenience initializer
- (id) init {
    return [self initWithArray:nil];
}

// Designated initializer
- (id) initWithArray:(NSArray*)anArray {
    if ((self = [super init]) == nil) return nil;

    list = [[CHSinglyLinkedList alloc] init];
    for (id anObject in anArray) {
        [list prependObject:anObject];
    }

    return self;
}
quinntaylor commented 3 years ago

Thanks for reporting this. I've fixed it in main now.