2

I used to use prefix underscore for instance variable naming, to distinguish from local variables.

I happend to see the "Google Objective-C Style Guide", and found that it suggests to use trailing underscores(refer it HERE), but without any detail explanation about why.

I know it is a coding style issue, but I do wonder is there any advantages of using trailing underscores?

Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • Oh...not suitable for SO? Please leave a comment. :( – Kjuly Jan 12 '12 at 08:48
  • More of a subjective discussion question than one with a "right answer" – jrturton Jan 12 '12 at 08:56
  • Thanks @jrturton, but I do need a good answer to explain some reasons about `iVar_`, so I can show this to my co-worker and discuss with him. :p – Kjuly Jan 12 '12 at 09:02
  • @Jano, I know about prefix underscore, and that's why I used to use it either. Thanks anyway! :) – Kjuly Jan 12 '12 at 09:04
  • 3
    hm... why is it closed as "not constructive"? Why do we follow Google's Objective-C style but not Apple's Objective-C style? – nonopolarity May 25 '12 at 03:38
  • @動靜能量 just like @jrturton said, it's __more of a subjective discussion question__.. I remember Google's Objective-C style is based on Apple's. But whether you use it or not, that's fine, just a personal taste of coding style issue. ;) By the way, the `iVar_` is safer than `_iVar` just like @LinghuaZhang said, __The prefix underscore usually is used in system/SDK libraries. So using prefix underscore may cause overridden of variable in super class and a bug like this is not so easy to found.__. I prefer the choice of `iVar_`. :p – Kjuly May 25 '12 at 03:54

5 Answers5

3

The prefix underscore usually is used in system / SDK libraries. So using prefix underscore may cause overridden of variable in super class and a bug like this is not so easy to found. Take a look at any class provided by system, like NSView, and you will find that.

Linghua Zhang
  • 996
  • 7
  • 4
  • Yeah, I heard about that, but I noticed that XCode4 updates the application templates to include underscores before instance variables. – Kjuly Jan 12 '12 at 08:51
3

Related: Question about @synthesize (see the blockquotes at the bottom of the answer)
The advantage is: _var is a convention (C99, Cocoa guidelines) for private, but it is so common (even on Apple templates) that Apple uses double underscore __var. Google solves it by using trailing underscore instead.

I updated the other answer with a couple more reasons...

Leading underscore is also discouraged in C++ (see What are the rules about using an underscore in a C++ identifier?) and Core Data properties (try adding a leading underscore in the model and you'll get "Name must begin with a letter").

Trailing underscore seems the sensible choice, but if you like something else, collisions are unlikely to happen, and if they do, you'll get a warning from the compiler.

Community
  • 1
  • 1
Jano
  • 62,815
  • 21
  • 164
  • 192
  • ! That's the key point I think! – Kjuly Jan 12 '12 at 09:05
  • _Apple uses single leading underscore for ivars so that their variable names won't collide with ours. When you name your ivars, use anything but a single leading underscore._ as @NSResponder said, so Google just use trailing underscores to solve this conflict? – Kjuly Jan 12 '12 at 09:20
  • 1
    They didn't say, but there are multiple reasons to think that. – Jano Jan 12 '12 at 09:46
1

Apple uses single leading underscore for ivars so that their variable names won't collide with ours. When you name your ivars, use anything but a single leading underscore.

NSResponder
  • 16,861
  • 7
  • 32
  • 46
  • Google just use trailing underscores(instead of single leading underscore) to solve this conflict? – Kjuly Jan 12 '12 at 09:21
0

There is no advantage as such using trailing underscores. We follow the coding style so that it eases the code readability. Underscore, in this case helps us differentiate between iVars and local variables. As far as i know, _iVar is more prominent than iVar_

Shanti K
  • 2,873
  • 1
  • 16
  • 31
  • Mum..I want to follow the coding style with it either, but my co-worker prefer `_iVar`. I need a good reason to persuade him. :p – Kjuly Jan 12 '12 at 08:54
  • why do u want to persuade him to use iVar_. There is nothing wrong with _iVar – Shanti K Jan 12 '12 at 08:56
  • 1
    Agree. Me see no advantage, either. – ZhangChn Jan 12 '12 at 09:00
  • @ShantiK, just in order to make sure we use the same coding style. This happened a few days ago, and finally we use `_iVar`. But recently, I find many projects use `iVar_`. :S – Kjuly Jan 12 '12 at 09:11
  • @ZhangChn maybe jano's answer will show the advantage. :) – Kjuly Jan 12 '12 at 09:12
0

Almost all programming languages based on english, where we write and read from left to right.
Using leading underscores makes easier finding iVars, and recognition that a variable is iVar.

Tomasz Wojtkowiak
  • 4,910
  • 1
  • 28
  • 35
  • Yep, `_iVar` is more convenient to find the ivars out. However, as you can see, _Google Objective-C Style Guide_ suggests `iVar_`.. – Kjuly Jan 12 '12 at 08:59
  • Google document doesn't explain why they suggest using trailing underscores.
    Additionaly, I don't agree with suggestion of abandoning ot the hungarian notation in the naming of local variables.
    – Tomasz Wojtkowiak Jan 12 '12 at 09:11