On Jun 1, 2015, at 4:52 PM, Britt Durbrow 
<bdurb...@rattlesnakehillsoftworks.com> wrote:
> 
> I don’t use underscores to prefix ivars. I think it’s ugly, and unnecessary 
> -- it doesn’t help with namespacing (if a subclass and a superclass both 
> declare _someVariable with the underscore they will collide just as badly as 
> if they declare someVariable without one)

Which is not at all, actually:

#import <Foundation/Foundation.h>

@interface Foo : NSObject

- (void)fooLogAnIvar;

@end

@interface Bar : Foo

- (void)barLogAnIvar;

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Bar *bar = [Bar new];
        
        [bar fooLogAnIvar];
        [bar barLogAnIvar];
    }
    return 0;
}

@implementation Foo {
    NSString *_anIvar;
}

- (instancetype)init {
    self = [super init];
    
    if (self == nil) {
        return nil;
    }
    
    _anIvar = @"Foo";
    
    return self;
}

- (void)fooLogAnIvar {
    NSLog(@"Foo: _anIvar is %@", _anIvar);
}

@end

@implementation Bar {
    NSString *_anIvar;
}

- (instancetype)init {
    self = [super init];
    
    if (self == nil) {
        return nil;
    }
    
    _anIvar = @"Bar";
    
    return self;
}

- (void)barLogAnIvar {
    NSLog(@"Bar: _anIvar is %@", _anIvar);
}

@end

2015-06-01 17:12:16.328 test[17203:2499855] Foo: _anIvar is Foo
2015-06-01 17:12:16.329 test[17203:2499855] Bar: _anIvar is Bar

> and ivars vs accessors are obvious by context: [self obviouslyAnAccessor] or 
> self.obviouslyAnAccessor vs obviouslyAnIvar (or very rarely, 
> someObject->obviouslyAnIvar).


Non-underscored ivars vs. local variables, however, are not obvious at all, 
especially if the method is large.

Charles

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@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/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to