• 变量命名 函数命名 方法 Naming cheatsheet


    Naming things is hard. This sheet attempts to make it easier.
    Although these suggestions can be applied to any programming language, I will use JavaScript to illustrate them in practice.

    English language

    • Use English language when naming your variables and functions.

    Naming convention

    • Pick one naming convention and follow it. It may be camelCase, PascalCase, snake_case, or anything else, as long as it remains consistent. Many programming languages have their own traditions regarding naming conventions; check the documentation for your language or study some popular repositories on Github!

    S-I-D

    • Short. A name must not take long to type and, therefore, remember;
    • Intuitive. A name must read naturally, as close to the common speech as possible;
    • Descriptive. A name must reflect what it does/possesses in the most efficient way.

    Avoid contractions

    • Do not use contractions. They contribute to nothing but decreased readability of the code. Finding a short, descriptive name may be hard, but contraction is not an excuse for not doing so.
    class MenuItem {
      /* Method name duplicates the context (which is "MenuItem") */
      handleMenuItemClick = (event) => { ... }
      /* Reads nicely as `MenuItem.handleClick()` */
      handleClick = (event) => { ... }
    }
    

    Reflect the expected result

    • A name should reflect the expected result.
    /* Bad */
    const isEnabled = itemCount > 3
    return <Button disabled={!isEnabled} />
    
    /* Good */
    const isDisabled = itemCount <= 3
    return <Button disabled={isDisabled} />
    

    Naming functions

    • A/HC/LC pattern
    • There is a useful pattern to follow when naming functions

      prefix? + action (A) + high context (HC) + low context? (LC)

    • Take a look at how this pattern may be applied in the table below.
    • Actions
      • get,set,reset,fetch,remove,delete,compose,handle
    • Context
      • A domain that a function operates on.
    • Prefixes
      • Prefix enhances the meaning of a variable. It is rarely used in function names.
      • is,has,should,min/max,prev/next

    Singular and Plurals

    • Like a prefix, variable names can be made singular or plural depending on whether they hold a single value or multiple values.

    原地址
    naming-cheatsheet

  • 相关阅读:
    5 Python3 函数进阶&迭代器与生成器
    2 python第三章文件操作
    4 python内置函数
    python内置函数 eval()、exec()以及complie()函数
    0 字符与字节的区别
    python enumerate() 函数
    1 python 文件处理
    python 之编写登陆接口
    python 之九九乘法表
    第一模块第二章-数据类型整理
  • 原文地址:https://www.cnblogs.com/jiangyibo/p/15903901.html
Copyright © 2020-2023  润新知