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

2012-01-17 01:55:01 +08:00
 Livid
假设类名是 OLStatsViewController,那么你们会如何为这个 View Controller 的实例命名呢?
5636 次点击
所在节点    iDev
18 条回复
elden
2012-01-17 01:59:11 +08:00
statsViewController
Kai
2012-01-17 02:02:36 +08:00
statsViewController +1

如果需要多个,就 mainViewController, otherViewController 之类。
marvinII
2012-01-17 06:42:38 +08:00
statsVctrl_ : 成员变量
statsVctrl:参数
stats_vctrl:局部变量
g_stats_vctrl: 全局变量

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

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

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

另外,个人认为 Objective-C 代码里最好不要混用 C 风格的命名。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/25732

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX