V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
DjvuLee
V2EX  ›  编程

好的变量缩写

  •  
  •   DjvuLee · 2015-09-08 22:36:01 +08:00 · 6209 次点击
    这是一个创建于 3145 天前的主题,其中的信息可能已经有所发展或是发生改变。

    自己觉得 3~4 个字符表示的缩写给程序变量命名比较好,即避免了单个字符的完全没有意义,也避免了冗余,我自己个人编程中觉得下面这些是不错的,不知道大家通常有哪些不错的缩写?

    idx: index
    cnt: count
    src: srouce
    dst: destination
    usr: user
    ptr: pointer
    msg: message
    buf: buffer
    len: length
    dat: data
    lib: library
    inc: increase
    dec: decrement
    rsp: response
    req: request

    addr: address
    conn: connection

    32 条回复    2015-09-09 11:43:51 +08:00
    sox
        1
    sox  
       2015-09-08 23:02:41 +08:00
    scu: shouldComponentUpdate
    lavadore
        2
    lavadore  
       2015-09-08 23:13:21 +08:00   ❤️ 1
    个人觉得通常情况没必要缩写,你给的例子里也不是很长。有很多反而会引起混淆,比如
    dat: data, date
    inc: increase, increment, include
    dec: decrease, decrement, december
    ariestiger
        3
    ariestiger  
       2015-09-08 23:23:49 +08:00
    你确定把单词写完整了会产生冗余? 告诉你吧, 随着时间的流逝, 这些类名, 变量名, 方法名中包含的信息量是越来越少的, 到时候你只会嫌当初命名时没取个长一点完整一点更有意义一点的名字。
    msg7086
        4
    msg7086  
       2015-09-08 23:25:53 +08:00
    dat: data

    这是节约了多少字符?

    另外在有 IDE / 良好的编辑器 的时候,这么玩毫无意义。
    DjvuLee
        5
    DjvuLee  
    OP
       2015-09-09 00:17:52 +08:00
    @ariestiger 自己确实不太喜欢完整的单词,尤其是像 Java 那种冗余的 API 命名就是不太喜欢。
    DjvuLee
        6
    DjvuLee  
    OP
       2015-09-09 00:18:25 +08:00
    @msg7086 哈哈,其实主要是自己觉得 3 个字符比较统一
    lavadore
        7
    lavadore  
       2015-09-09 00:22:02 +08:00
    @DjvuLee 自己一个人写随便你怎么规定,但是团队还是全称比较好。

    就像你写字的时候如果每个字都少几个笔画有什么意义么?你写的时候是可以少浪费点墨,但是看的人需要自己脑补那些缺少的部分,很不爽的
    seki
        8
    seki  
       2015-09-09 00:34:17 +08:00
    在自动补全的 ide 如此多的情况下,缩写其实没什么意义,除非你写的语言找不到这样的一个 ide ,例如 office vba
    长长的变量的一个好处是不需要加注释也能看懂;过度地缩写,结果还得老老实实写注释,击键次数会更多
    DjvuLee
        9
    DjvuLee  
    OP
       2015-09-09 00:35:09 +08:00
    @lavadore 少写的意义就是简洁,可能和自己的风格有关系。团队的时候肯定是写全点好。不过 c 语言的项目很多还是缩写比较多的。
    FrankFang128
        10
    FrankFang128  
       2015-09-09 01:11:53 +08:00
    缩写哪有好的
    lavadore
        11
    lavadore  
       2015-09-09 01:23:42 +08:00
    @DjvuLee 简洁更重要的是代码格式和架构,缩写变量名这种可以说是舍本逐末. C 语言那是历史遗留下来的旧习惯
    shiny
        12
    shiny  
       2015-09-09 01:25:32 +08:00
    个人不认同楼主观点
    akira
        13
    akira  
       2015-09-09 01:31:35 +08:00
    变量名 /函数名等等所有的命名,尽量写完整,缩写这玩意太具有个人风格,就算是自己独立维护的代码,过上一段时间,你回头看也会发现没那么容易看了。如果是团队合作,就更不用说了。

    好代码最重要是要容易阅读!
    Valyrian
        14
    Valyrian  
       2015-09-09 02:17:41 +08:00 via iPhone   ❤️ 1
    我记得在哪看过, data 是最没用的变量名,任何东西都是 data ,所以永远不要用 data 当变量名
    ljbha007
        15
    ljbha007  
       2015-09-09 02:50:59 +08:00
    我反倒是喜欢很长的名字 反正都是 ide 提示
    loading
        16
    loading  
       2015-09-09 07:07:14 +08:00 via iPhone
    楼主你这是写汇编出生的?
    yangg
        17
    yangg  
       2015-09-09 08:29:09 +08:00 via iPhone
    最大的问题是 IDe 会说 typo
    frittle
        18
    frittle  
       2015-09-09 08:42:22 +08:00
    标准通用的大家一看就知道的还无妨,可是 cnt 、 dat 之类的,尽量避免。
    finian
        19
    finian  
       2015-09-09 09:10:59 +08:00
    尽量不缩写, Readability matters
    NearTan
        20
    NearTan  
       2015-09-09 09:14:59 +08:00
    曾经有多少人认为 Linux 系统下的 usr 目录就是 user
    ehs2013
        21
    ehs2013  
       2015-09-09 09:20:28 +08:00
    @NearTan In the original Unix implementations, /usr was where the home directories of the users were placed (that is to say, /usr/someone was then the directory now known as /home/someone ). In current Unices, /usr is where user-land programs and data (as opposed to 'system land' programs and data ) are. The name hasn't changed, but it's meaning has narrowed and lengthened from "everything user related" to "user usable programs and data". As such, some people may now refer to this directory as meaning 'User System Resources' and not 'user' as was originally intended.
    zylll520
        22
    zylll520  
       2015-09-09 09:21:50 +08:00
    个人习惯没有超过 5 个就不用缩写
    tabris17
        23
    tabris17  
       2015-09-09 09:32:48 +08:00   ❤️ 1
    src: srouce
    dst: destination
    ptr: pointer
    msg: message
    buf: buffer
    len: length
    lib: library
    addr: address
    conn: connection

    这几个长的可以有,而且很常用,一目了然,其他几个本来就很短,没必要再缩写了
    Andiry
        24
    Andiry  
       2015-09-09 10:20:39 +08:00
    When Ken Thompson was asked if he redid UNIX, what would he do differently, he said that he'd spell creat with an e.
    wuxu92
        25
    wuxu92  
       2015-09-09 10:39:24 +08:00
    user 竟然用 usr 。。。 data 缩写为 dat 也没有意义, response 缩写为 resp 的比较多
    Raito
        26
    Raito  
       2015-09-09 10:42:13 +08:00
    srouce? 打错了吧
    kappa
        27
    kappa  
       2015-09-09 10:43:00 +08:00
    inc: increase
    dec: decrement

    这。。。
    lxrmido
        28
    lxrmido  
       2015-09-09 11:08:24 +08:00
    pwd/passwd/password
    desc/description
    fs/filesystem
    res/resource
    db/database
    jhaohai
        29
    jhaohai  
       2015-09-09 11:11:43 +08:00
    最令人发指的是把变量名缩的只剩一个字符
    把 request 缩成 r
    把 header 缩成 h
    把 packet 缩成 p
    pangtianyu
        30
    pangtianyu  
       2015-09-09 11:31:31 +08:00   ❤️ 1
    我认为最好的变量就是 a 到 z 一共 26 个字母 不够就 aa ab 一直下去
    jswh
        31
    jswh  
       2015-09-09 11:33:02 +08:00
    代码自注释
    learnshare
        32
    learnshare  
       2015-09-09 11:43:51 +08:00
    尽量不缩写,靠 IDE/编辑器的自动提示来提高效率
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5302 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 1904ms · UTC 07:53 · PVG 15:53 · LAX 00:53 · JFK 03:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.