Underscore naming conventions


#1

One thing that has changed since I read the 2nd edition of this book is the user of underscore naming for variables.

It used to be that you would just synthesize the name of the property and that was that. Now the Apple template adds in the underscore name as well.

Reading the Apple ObjC style guide they seem to recommend against underscore naming as it is something they reserve for private methods and such.
Reading the Google ObjC style guide they recommend against underscore prefixes, but encourage underscore suffixes.
Reading the internet in general there seems to be no real consensus to any of it. Some people say they use underscores as protection against calling ivars directly, but isn’t that the point of a property anyway? That it creates the getters and setters for us?

It’s all reminiscent of the, blood soaked, dot notation wars of the early Objective-C 2.0 era.

I was wondering what the mods here thought of it.


#2

Regarding using underscore prefixes in the book: we only use them in the SpeakLine exercise, and mostly because we want you to use the Assistant Editor to create outlets/connections, and fighting the underscore prefix would have made things a bit more complicated. Otherwise the book tries to use no prefixes.

Apple does strongly caution against underscore naming for method names, but as far as I know they make no recommendation for instance variable names. The template authors like underscore prefixes, evidently. A number of the Cocoa headers indicate that they favor underscore prefixes in their own classes.

We use prefixes/suffixes to make it very clear when we’re referencing an instance variable directly and circumventing any setter/getter. Xcode’s syntax highlighting makes this somewhat less necessary. For me it comes down to a question of personal style. How do you like your instance variables to look? A lot of Cocoa developers use no prefix (and so did I, for a long time). I’m not a fan of underscores… to me they make code look noisy and cold. In my own projects now I use a lowercase ‘m’ prefix (mVariableName). I did want my instance variables to stand out more than just the syntax highlighting provided, however.

Adam


#3

I have a slightly different question on this topic. It appears that Xcode adds the “_” to the instance variables when it creates the @synthesize statements in the implementation file; the ones you created in the header file were *_voices and *speechSynth, and it appears you knew they would be required to have the "" for some reason.

I read the NSSpeechSynthesizer Class Reference information and could find no reference for the required use of the “". So, is NSSpeechSynthesizer one of the classes that Apple itself chose to require the "” in the naming of instances? If so, how can we find the requirements for this with other classes, especially since Xcode 4.4 no longer requires the creation of @synthesize statements in the implementation?

BTW, the BNR books are the most thorough I’ve found on Objective C. Thanks for your excellent treatment of the subject.


#4

There is a good reason for this naming convention: viewtopic.php?f=214&t=4969&p=12750#p12654


#5

[quote=“AdamPreble”]Regarding using underscore prefixes in the book: we only use them in the SpeakLine exercise, and mostly because we want you to use the Assistant Editor to create outlets/connections, and fighting the underscore prefix would have made things a bit more complicated. Otherwise the book tries to use no prefixes.

Apple does strongly caution against underscore naming for method names, but as far as I know they make no recommendation for instance variable names. The template authors like underscore prefixes, evidently. A number of the Cocoa headers indicate that they favor underscore prefixes in their own classes.

We use prefixes/suffixes to make it very clear when we’re referencing an instance variable directly and circumventing any setter/getter. Xcode’s syntax highlighting makes this somewhat less necessary. For me it comes down to a question of personal style. How do you like your instance variables to look? A lot of Cocoa developers use no prefix (and so did I, for a long time). I’m not a fan of underscores… to me they make code look noisy and cold. In my own projects now I use a lowercase ‘m’ prefix (mVariableName). I did want my instance variables to stand out more than just the syntax highlighting provided, however.

Adam[/quote]

Doing the SpeakLine exercise and the Challenge (using Xcode 4.5.1) I was a little confused by this. For example, in the Challenge I used the Assistant Editor and these were created for the text and label fields by Xcode in the interface:

@property (weak) IBOutlet NSTextField *textField;
@property (weak) IBOutlet NSTextField *labelField;

In the AppDelegate implementation I had to refer to them as _textField and _labelField for it to work, for example:

  • (IBAction)countCharacters:(id)sender {
    NSString *string = [_textField stringValue];
    NSUInteger labelLength = [string length];
    NSString *labelValue = [NSString stringWithFormat:@"’%@’ has %lu characters", string, labelLength];
    [_labelField setStringValue:labelValue];
    }

If I understand correctly now these variables with the underscore prefix are automatically synthesized by Xcode “under the covers” somewhere. My comment is that the book is confusing in that I have not found an explanation there of using the underscore. I figured it out “script monkey” style by just imitating the code I had.