• 《编写可读代码的艺术》第6章 写出言简意赅的注释


    1. 让注释保持紧凑

    1 //反面示例
    2 // The int is the CategoryType.
    3 // The first float in the inner pair is the 'score',
    4 // the second is the 'weight'.
    5 typedef hash_map<int, pair<float, float> > ScoreMap;
    6 
    7 //正面示例
    8 // CategoryType -> (score, weight)
    9 typedef hash_map<int, pair<float, float> > ScoreMap;

    2. 避免使用不明确的代词

    1 // Insert the data into the cache, but check if it's too big first.
    2 
    3 //1. 把it改成data
    4 // Insert the data into the cache, but check if the data is too big first.
    5 
    6 //2. 重组以使it更明确
    7 // If the data is small enough, insert it into the cache.

    3. 润色粗糙的句子

    1 // Depending on whether we've already crawled this URL before, give it a different priority.
    2 
    3 //更简单直接,且包含“优先级”相关的信息
    4 // Give higher priority to URLs we've never crawled before.

    4. 用精心挑选的例子进行说明

    1 // Example: Strip("abba/a/ba", "ab") returns "/a/"
    2 String Strip(String src, String chars) { ... }

    5. ”具名参数函数“的注释

    # Python
    # Call the function using named parameters
    Connect(timeout = 10, use_encryption = False)
    
    // C++
    // Call the function with commented parameters
    Connect(/* timeout_ms = */ 10, /* use_encryption = */ false);

    6. 采用信息量高的词

    1 // This class acts as a caching layer to the database.
    2 
    3 // Canonicalize the street address (remove extra spaces, "Avenue" -> "Ave.", etc.)
  • 相关阅读:
    谜题48:我所得到的都是静态的
    谜题47:啊呀!我的猫变成狗了
    谜题46:令人混淆的构造器案例
    谜题45:令人疲惫不堪的测验
    谜题44:切掉类
    Python--csv文件处理
    Python装饰器
    Python单例模式
    <<Senium2自动化测试>>读书笔记二
    python之内置函数
  • 原文地址:https://www.cnblogs.com/yyqng/p/14227320.html
Copyright © 2020-2023  润新知