V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
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
Joyreece
V2EX  ›  iDev

变量名起名缩写的习惯

  •  
  •   Joyreece · Sep 15, 2017 · 11406 views
    This topic created in 3152 days ago, the information mentioned may be changed or developed.
    算是个日经话题吧,请问前辈们,你们在起变量名的时候会把 View Controller 所写成 VC (例如:resetPasswordVC = xxx )、Text Field 所写成 TF (例如:articleTitleTF = xxx )的这种形式么?

    我纠结的在于想保持代码的可读性和简洁干净,但是在实例化一些类的时候起名有的时候犹豫不决是写成 VC 这种缩写还是写全了。参考过原声 SDK 一些头文件露出来的方法,看苹果也是有的直接写成 viewController 也有是 vc。

    所以想看看哪种风格更好一些呢?
    37 replies    2020-05-09 11:35:59 +08:00
    lazypu
        1
    lazypu  
       Sep 15, 2017
    我是尽量不用缩写, 可读性在我这里排第一位.
    Immortal
        2
    Immortal  
       Sep 15, 2017
    能缩就缩 但是不能影响可读 ViewController 缩成 VC 就太过分了...这种情况不如不缩
    sudoz
        3
    sudoz  
       Sep 15, 2017   ❤️ 3
    约定俗成的可以写,我不喜欢太长的变量名或方法名
    比如 message -> msg, result -> res,context-> ctx
    TuringGooner
        4
    TuringGooner  
       Sep 15, 2017
    你这缩的都认不出是什么了

    缩写不能影响理解
    laoyur
        5
    laoyur  
       Sep 15, 2017
    VC 党路过,但对 TF 的缩写感觉无法认同,要缩也是在合理范围内缩。
    Vitta
        6
    Vitta  
       Sep 15, 2017   ❤️ 1
    ViewController 一般我是写 VC, 类的话写 ViewController
    textField 个人不写 TF, 每次想写 TF 的时候就老想起来钩子
    xiaoc19
        7
    xiaoc19  
       Sep 15, 2017 via iPhone
    从不缩写,代码写来看的,看起来像读一篇文章的感觉挺好
    flyingghost
        8
    flyingghost  
       Sep 15, 2017   ❤️ 5
    开发圈有哪些约定俗成的常见缩写?

    req = request
    resp = response
    res = resource
    app = application
    auth = authorization/authentication 这点非常不认可,同一个常见缩写居然能代表两个不同的词。
    dev = develop
    env = environment
    db = database
    pkg = package
    svr = server
    i18n = internationalization
    cert = certificate
    msg = message
    addr = address
    tmp/temp = temporary
    var/def = variable/define 其实 var/def 已经是很多语言的关键字了
    calc = calculate/calculation
    src = source
    dest = destination
    min/max = minimum/maximum 应该已经不算了吧。。。
    doc = document
    dir = directory


    foo/bar = 不知道该起什么名字好


    未完待续。
    noe132
        9
    noe132  
       Sep 15, 2017 via Android
    缩写干嘛。。反正不都是按几个键 tab 一下就出来了,为什么要降低可读性
    gclove
        10
    gclove  
       Sep 15, 2017
    必须缩写啊, 缩写可以提高可读性, 看起来更加简洁

    而且, 一般的缩写都是大家约定的, 比如 res, req, resp, db
    Joyreece
        11
    Joyreece  
    OP
       Sep 15, 2017
    看了楼上前辈们的观点,个人决定还是写全了。
    之前又顾虑是因为想尽量保持单行不超过 120 个字符,但是某些情况下 ViewController 和 TextField 的变量名字会越来越长,特蛋疼~
    newtype0092
        12
    newtype0092  
       Sep 15, 2017
    @sudoz 为什么我碰见的都是 ret->result, res->resource ...
    linus3389
        13
    linus3389  
       Sep 15, 2017
    缩啥啊,自动补完这么强大....
    blackfire
        14
    blackfire  
       Sep 15, 2017
    我一般看变量长度,尽量保持可读性,如果前边的比较长一串才能描述完整,一般会缩写 VC,尽量不去缩写 TF。特别长的都会缩写,如果比较短就不缩写
    ——是的,我是混着用的。

    感觉不降低阅读性,没啥歧义,我是赞成缩写的
    Lucups
        15
    Lucups  
       Sep 15, 2017
    缩写是有规则的,据我观察,至少有以下 3 种:

    一种是取开头几个(一般是 3、4 个)字母的,如 binary => bin, application => app,resource => res,楼上有很多例子
    一种是去掉元音字母的,如 source => src, message => msg, text => txt 等等
    还有一些是约定俗成的,如 internationalization => i18n, database => db 等等

    感谢 @flyingghost 提供的例子~

    欢迎补充~
    RorschachZZZ
        16
    RorschachZZZ  
       Sep 15, 2017
    很少缩写。看懂为主,简洁为辅。
    cigarzh
        17
    cigarzh  
       Sep 15, 2017 via Android
    viewCtrl
    fy
        18
    fy  
       Sep 15, 2017
    @sudoz result -> ret 这才是约定俗成吧
    AngelCriss
        19
    AngelCriss  
       Sep 15, 2017 via Android
    @newtype0092 resource 不是 rsc 吗
    AngelCriss
        20
    AngelCriss  
       Sep 15, 2017 via Android
    @fy ret 是 return 啊
    geelaw
        21
    geelaw  
       Sep 15, 2017 via iPhone
    @fy
    @newtype0092

    ret = return (value)
    fy
        22
    fy  
       Sep 15, 2017
    @AngelCriss
    @geelaw

    哦对啊 ret 是 return。

    不过这个变量使用场景和 result 基本相同就是了。
    kx5d62Jn1J9MjoXP
        23
    kx5d62Jn1J9MjoXP  
       Sep 15, 2017 via Android   ❤️ 1
    我越是局部的代码越用缩写,类的成员变量用完整的单词
    alexgor
        24
    alexgor  
       Sep 15, 2017 via Android
    额……驼峰啊
    ExploreWay
        25
    ExploreWay  
       Sep 15, 2017
    我觉得大家说的很好,这个变量命名一定要讲究可读性。上面提到的一些约定熟成的东西,是可以写缩写的,当然你也可以选择全写。(注:本人平时也没有注意这个问题,缩写的比较严重,今后也要多注意这个问题)
    carlclone
        26
    carlclone  
       Sep 15, 2017 via Android
    什么年代了还缩写,自动完成一个 tab 的事情,以前没有还能理解
    Sapp
        27
    Sapp  
       Sep 15, 2017
    如果大家都理解,就缩写,比如 xxxBtn、el、msg、req、res 否则还是写全。
    dsg001
        28
    dsg001  
       Sep 15, 2017   ❤️ 1
    遵循一个原则,变量范围越大变量名越长
    mcfog
        29
    mcfog  
       Sep 15, 2017
    一个是看 scope,另一个是看是否 ambiguous,比如 ret res 这种坚决不缩写

    result response resource return 傻傻分不清楚
    gamexg
        30
    gamexg  
       Sep 16, 2017 via Android
    跟 go 标准库学的,短函数内部变量敢用单字母变量名。
    对外的就继续长变量名。
    beimenjun
        31
    beimenjun  
    PRO
       Sep 16, 2017   ❤️ 1
    既然你发在 iDev 节点里,个人认为,ViewController 缩写成 VC,在 iOS 这个部分是一种很常见的写法,至于你说的 TF,就太偏了。

    既然常见到可以约定俗成的,又不是啥洪水猛兽,为啥不选择缩写呢?
    Meli55a
        32
    Meli55a  
       Sep 16, 2017
    @flyingghost @ssynhtn @beimenjun 认同,我很推崇规范的东西,看着舒服,很不喜欢取名随意的代码,看着难受,已经都有点偏激了
    newtype0092
        33
    newtype0092  
       Sep 18, 2017
    @AngelCriss 我是做客户端的,很多模版工程里都有 src(source)和 res(resource),从没有见过 rsc,这个和 src 放在一起一看就懵逼了吧。。。ret 确实也可以看作 return,不过 return 和 result 其实没啥区别,return 的当然就是 result 了~
    shawndev
        34
    shawndev  
       Sep 28, 2017
    除非类名会长到 xcode 左侧文件导航栏折叠内容否则不缩写,变量名不缩写,可以看一下代码整洁之道。
    iwpz
        35
    iwpz  
       Oct 13, 2017
    ProfileViewController *pvc = [[ProfileViewController alloc] init];
    [nav pushvc:pvc];

    对 vc 的调用不就只有 push pop 那么几行吗,挨着很近,都能看见。
    所以我是这么写的。

    ShopViewController *svc
    MainViewController *mvc
    Obelly
        36
    Obelly  
       Oct 19, 2017
    短函数内(<20 行),爱怎么来怎么来,ijklnm 单字母都没人管,暴露接口,怎么长怎么来……
    话说一定要把类型带到命名里面么?
    syrupofplum
        37
    syrupofplum  
       May 9, 2020
    ret 做 result 缩写使用的时候,感觉很容易和 return 混淆。
    所以我一直把 result 缩写成 rst 。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2396 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 120ms · UTC 11:34 · PVG 19:34 · LAX 04:34 · JFK 07:34
    ♥ Do have faith in what you're doing.