碧海潮生's profile碧海潮生的小屋PhotosBlogLists Tools Help

Blog


    May 25

    设计笔记——命名规则

    “C是一门朴素的语言,你使用的命名也应该这样。”
    —— 《Linux kernel coding style》

    顾名思义,“命名规则”指的是为标识符起名字时遵循的规则。
    标识符主要包括变量名、函数名和宏名。


    1 常见命名规则

    比较著名的命名规则首推匈牙利命名法,
    这种命名方法是由Microsoft程序员查尔斯·西蒙尼(Charles Simonyi) 提出的。
    其主要思想是“在变量和函数名中加入前缀以增进人们对程序的理解”。
    匈牙利命名法关键是:标识符的名字以一个或者多个小写字母开头作为前缀;
    前缀之后的是首字母大写的一个单词或多个单词组合,该单词要指明变量的用途。
    例如:lpszStr, 表示指向一个以'\0'结尾的字符串(sz)的长指针(lp)变量。

    骆驼(Camel)命名法近年来越来越流行,
    在许多新的函数库和Java这样的平台下使用得当相多。
    骆驼命名法,正如它的名称所表示的那样,指的是混合使用大小写字母来构成标识符的名字。
    其中第一个单词首字母小写,余下的单词首字母大写。
    例如:printEmployeePaychecks(),函数名中每一个逻辑断点都有一个大写字母来标记。

    帕斯卡(Pascal)命名法与骆驼命名法类似。
    只不过骆驼命名法是第一个单词首字母小写,而帕斯卡命名法则是第一个单词首字母大写。
    例如:DisplayInfo()和UserName都是采用了帕斯卡命名法。

    在C#中,以帕斯卡命名法和骆驼命名法居多。
    事实上,很多程序设计者在实际命名时会将骆驼命名法和帕斯卡结合使用,
    例如变量名采用骆驼命名法,而函数采用帕斯卡命名法。

    另一种流行的命名规则称为下划线命名法。
    下划线法是随着C语言的出现流行起来的,在UNIX/LIUNX这样的环境,以及GNU代码中使用非常普遍。

    本章所述的命名规则主要基于下划线命名法发展而来。


    2 函数的命名

    函数名使用下划线分割小写字母的方式命名:

    设备名_操作名()

    操作名一般采用:谓语(此时设备名作为宾语或者标明操作所属的模块)或者
    谓语+宾语/表语(此时设备名作为主语或者标明操作所属的模块) 等形式,如:

    tic_init()
    adc_is_busy()
    uart_tx_char()

    中断函数的命名直接使用 设备名_isr() 的形式命名,如:
    timer2_isr()


    3 变量的命名

    变量的命名也采用下划线分割小写字母的方式命名。
    命名应当准确,不引起歧义,且长度适中。如:
    int length;
    uint32 test_offset;

    单字符的名字也是常用的,如i, j, k等,它们通常可用作函数内的局部变量。

    tmp常用做临时变量名。

    局部静态变量,应加s_词冠(表示static),如:
    static int s_lastw;

    全局变量(尤其是供外部访问的全局变量),应加g_词冠(表示global),如:
    void (* g_capture_hook)(void);


    4 常量及宏的命名

    采用下划线分割大写字母的方式命名,一般应以设备名作为前缀,
    防止模块间命名的重复。如:

    #define TIMER0_MODE_RELOAD        2
    #define TIMER2_COUNT_RETRIEVE(val)    ((uint16)(65536 - (val)))

    当然,看作接口的宏可以按照函数的命名方法命名,例如:

    #define timer2_clear()        (TF2 = 0)
    #define timer0_is_expired()    (TF0)


    5 常用缩写词

    原词        缩写

    addition    add
    answer        ans
    array        arr
    average        avg
    buffer        buf或buff
    capture        cap或capt
    check        chk
    count        cnt
    column        col
    control        ctrl
    decode        dec
    define        def
    delete        del
    destination    dst或dest
    display        disp
    division    div
    encode        enc
    environment    env
    error        err
    float        flt
    frequency    freq
    header        hdr
    index        idx
    image        img
    increment    inc
    initalize    init
    iteration    itr
    length        len
    memory        mem
    middle        mid
    make        mk
    message        msg
    multiplication    mul
    number        num
    operand        opnd
    optimization    opt
    operator    optr
    packet        pkt
    positon        pos
    previous    pre或prev
    payload type    pt
    pointer        ptr
    return code    rc
    record        rcd
    receive        recv
    result        res
    return        ret
    source        src
    stack        stk
    string        str
    subtraction    sub
    table        tab
    temporary    tmp或temp
    total        tot
    time stamp    ts
    value        val



    6 结语

    没有一种命名规则可以让所有的程序员赞同。而这多种命名规则也确实各有利弊。

    没有必要花太多的精力试图发明最好的命名规则,
    而是应当制定一种令大多数项目成员满意的命名规则并切实执行。
    标识符命名的一致性自然会体现出代码的优雅。

    当然,如果你的程序使用了第三方的代码,而这些模块经验证确实是正确无误的。
    那么也没有必要一味追求命名的一致性,而去修改这些已经定型的模块中的函数和变量名。


    参考文献:

    [1] 高质量程序设计指南——C/C++语言,林锐
    [2] Linux kernel coding style,Linus Torvalds
    [3] GNU Coding Standards,Richard Stallman

    Comments (6)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Oct. 21
    Oct. 21
    Sept. 3
    No namewrote:
    Welcome to enter (wow gold) and (wow power leveling) trading site, (wow gold) are cheap, (wow power leveling) credibility Very good! Quickly into the next single! Key words directly to the website click on transactions! -25591224352962
    Aug. 5
    July 21
    Apr. 11

    Trackbacks (1)

    The trackback URL for this entry is:
    http://jx-kingwei.spaces.live.com/blog/cns!F7A152EB74B9576E!1495.trak
    Weblogs that reference this entry