17 January 2024

C++ 核心指南目录

“State intent in comments”

理由

代码说明做了什么,而不是打算要做什么。通常代码意图能比代码实现说明地更清晰准确。

例子

void stable_sort(Sortable& c)
    // sort c in the order determined by <, keep equal elements (as
    // defined by ==) in their original relative order
{
    // ... quite a few lines of non-trivial code ...
}

注意

如果代码和注释冲突,那么两者可能都出错了。