views:

163

answers:

4

Something I see ppl define the variable like this:

b2World *_world;
b2Body *_body;
CCSprite *_ball;

instead of

b2World *world;
b2Body *body;
CCSprite *ball;

I familiar with the second one, but not the first one. So, I checked the Wikipedia about naming convention:

Names beginning with double underscore or an underscore and a capital letter are reserved for implementation (compiler, standard library) and should not be used (e.g. __reserved or _Reserved).

So, is that any special meaning which is start with "_"?

The code I saw which using "_" to begin is here:

http://www.raywenderlich.com/457/intro-to-box2d-with-cocos2d-tutorial-bouncing-balls

The wiki page.

+1  A: 

Usually it's used to mark class or struct members. But yeah, these are reserved names, and should not be used.

Smilediver
A: 

Usually they're used for variables that shouldn't be accessed outside the current file/module/namespace/whatever, in languages that don't support restricting access with something like a private keyword

Michael Mrozek
Or static in C.
wykyd
+2  A: 

There's a long-standing convention among some Objective-C developers to prefix instance variables with an underscore. It can be helpful in several ways: one, it makes it easier to spot instance variables in a .m file; two, it relieves developers of having to come up with creative names for method parameters to avoid colliding with instance variable names; and three, as others have noted, it indicates that the instance variables are private, and therefore shouldn't be accessed willy nilly throughout the code.

In fact, I'd argue for avoiding accessing instance variables directly in methods other than accessors (getters and setters), -dealloc, and -init.... Not that you should never, ever use them anywhere else, but you should at least give it some thought before using an instance variable directly in other methods.

jlehr
A: 

Apple reserves names beginning with underscore for its own private ivars and methods. In Objective-C on any Apple platform, it is recommended that you do not prefix your identifiers with an underscore.

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html

JeremyP