iOS 开发实用技术导航
NSHipster 中文版
› http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
› http://www.cocos2d-iphone.org/
CocoaPods
› http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
› http://code.google.com/mobile/analytics/
WWDC
› https://developer.apple.com/wwdc/
Design Guides and Resources
› https://developer.apple.com/design/
Transcripts of WWDC sessions
› http://asciiwwdc.com
Cocoa with Love
› http://cocoawithlove.com/
Cocoa Dev Central
› http://cocoadevcentral.com/
NSHipster
› http://nshipster.com/
Style Guides
› Google Objective-C Style Guide
› NYTimes Objective-C Style Guide
Useful Tools and Services
› Charles Web Debugging Proxy
› Smore
Livid
408.15D
543.89D
V2EX  ›  iDev

你们是如何为 View Controller 的变量命名的呢?

  •  
  •   Livid ·
    PRO
    · Jan 17, 2012 · 7020 views
    This topic created in 5424 days ago, the information mentioned may be changed or developed.
    假设类名是 OLStatsViewController,那么你们会如何为这个 View Controller 的实例命名呢?
    18 replies  •  1970-01-01 08:00:00 +08:00
    elden
    elden
    Jan 17, 2012 via iPhone
    1
    statsViewController
    Kai
    Kai
    Jan 17, 2012
    2
    statsViewController +1

    如果需要多个,就 mainViewController, otherViewController 之类。
    marvinII
    marvinII
    Jan 17, 2012 via iPhone
    3
    statsVctrl_ : 成员变量
    statsVctrl:参数
    stats_vctrl:局部变量
    g_stats_vctrl: 全局变量

    1. 我倾向于短点的
    2. 成员变量加_尾巴,是c++ programming style里提倡的,不会和库里的父类成员起冲突,也不影响自动补全的速度;objc也适用这点
    3. 参数用标准的骆驼命名
    4. 局部变量k&r风格,这样就避免和参数混淆
    5. 全局加g_前缀,g for global
    levithinking
    levithinking
    Jan 17, 2012
    4
    如果代码是写给自己看的,就简洁点
    如果是写给团队看的话,还是详细点比较好,不要觉得长名字是累赘,不贪图一时方便。
    levey
    levey
    Jan 17, 2012
    5
    OLStatsViewController *svc = [[OLStatsViewController alloc] init];
    levey
    levey
    Jan 17, 2012
    6
    OLStatsViewController *_statsViewController;
    tokki
    tokki
    Jan 17, 2012
    7
    *vc
    guoxx_
    guoxx_
    Jan 17, 2012
    8
    @tokki *vc+1
    拉投资的时候让vc看代码,说写代码的时候都想着vc这事
    godwarlock
    godwarlock
    Jan 17, 2012
    9
    OLStatsViewController *oLStatsVC;
    adow
    adow
    Jan 17, 2012
    10
    statsViewController;
    wtl
    wtl
    Jan 17, 2012
    11
    vc。。。。。还有ra、pn。。。。。。
    我深受其害

    某次与一同事讨论此事 我说这样不利于阅读 代码一长 变量一多 根本就不知所以了
    人家只一句:那是你短期记忆能力不好
    我只能石化

    想必精通字母缩写流的均是大牛 也要照顾下吾等凡人啊
    ultragtx
    ultragtx
    Jan 17, 2012
    12
    xxxxViewController 或者 xxxxvc
    godwarlock
    godwarlock
    Jan 17, 2012
    13
    @wtl 其实关系不大,我这个也是工作中其他人从C++迁移过来的习惯,如果是我的话,我个人还是喜欢写全,反正有补全,写一次后面就都方便了
    surf
    surf
    Jan 17, 2012
    14
    一般都是直接小写首字母的方式
    风格接近自动生成的代码就好了
    acwind
    acwind
    Jan 17, 2012
    15
    如果只在某函数内部使用一次,我基本都上是:
    OLStatsViewController *controller = [[OLStatsViewController alloc] init];
    [self presentModalViewController:controller animated:YES];
    sharkli
    sharkli
    Jan 18, 2012
    16
    OLStatsViewController* statsVC = nil;
    tokki
    tokki
    Jan 18, 2012
    17
    @guoxx_ @acwind 我写vc一般都是用一次-。-
    如果用很多的话 我会写完整的
    soulhacker
    soulhacker
    Jan 18, 2012
    18
    Cocoa 源码的哲学就是不怕名字长,为了可读尽量不用缩写,反正 Xcode 自动完成功能就为这个存在的。当然也不是绝对的,不过我自己还是喜欢 statsViewController 或 oLStatsViewController 这样的。

    另外,个人认为 Objective-C 代码里最好不要混用 C 风格的命名。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   974 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 23:05 · PVG 07:05 · LAX 15:05 · JFK 18:05
    ♥ Do have faith in what you're doing.