• 不要让简单的方式复杂化

    4. 代码习惯 - 图1

    Here are a few possibilities for simplifying:

    • Don’t use a list when an atomic vector will do.

    • Don’t use a data frame when a matrix will do.

    • Don’t try to use an atomic vector when a list is needed.

    • Don’t try to use a matrix when a data frame is needed.

    • 正确对待warning
    1. Figure out what the warning is saying.
    2. Figure out why the warning is triggered.
    3. Figure out the effect on the results of the computation (via deduction or experimentation).
    4. Given the result of step 3, decide whether or not the results will be erroneous.
    • 几类错误

    4. 代码习惯 - 图2

    • 避免全局赋值<<- 风险的一种策略

    将函数用local 定义在一个局部环境中,将相关计算的变量结果直接保存在局部,通过环境获取该变量:

    4. 代码习惯 - 图3

    4. 代码习惯 - 图4