开发者

Why does google suggest to use trailing underscores when naming iVars in Objective-C?

In Google's Objective-C Style guide which is follow by many people , Google says

Class member variables have trailing underscores

Why? Is there any good 开发者_StackOverflowreason for doing this? I found apple usually name an ivar when beginning underscore.


I prefer trailing underscores because if I have:

int test_;

I can type 't' and it will appear in the code completion immediately.

With

int _test;

I have to type '_t' to get to the T's in code completion.

Whether that's true or not, not sure, but that's what I've convinced myself of.


Heavy users of Core Data will have also noticed that Core Data attributes cannot begin with non-alpha characters. If you want to name ivars consistently across your app and various projects, this is another reason to append, rather than prepend, your ivars with an underscore.

If you're not using Core Data a lot, or you don't have OCD tendencies towards consistency in naming conventions, then whatever works best for you is probably the right answer.


This could be a carry-over of from their C++ style guide. In C++, leading underscores are reserved.

Their naming convention for objective-c ivars is consistent with c++ private member variables.


It's simply their preferred style. The underscore, whether prefixed or suffixed, represents that it is a private ivar and not a property or public ivar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜