评论是否显示添加/修改了哪些版本代码有用?

dan*_*nio 11 version-control comments

我工作的项目中的一些开发人员习惯于评论他们的代码以显示其添加的产品版本,例如

// added for superEnterpriseyWonder v2.5
string superMappingTag = MakeTag(extras);
if (superMappingTag.empty())
{
     autoMapping = false;
}
// end added for superEnterpriseyWonder v2.5
Run Code Online (Sandbox Code Playgroud)

每当我看到这个,我的血压升高,我必须花5分钟浏览SO来冷却.在我看来,他们不理解版本控制,如果我使用这种做法,源文件中的每一行都会是关于何时添加内容的评论.我正在考虑从我工作的文件中删除所有这些评论,但我想知道这只是我挑剔而且这些评论真的有些价值吗?

Nic*_*GPS 9

如果您正在使用Source Control,那么我建议在每次构建后向Source Control添加构建标签.这样,您可以搜索针对特定构建修改的所有源代码,没有令人讨厌的注释阻塞您的代码.

这来自Bob Martin的一本书"清洁代码":

"正确使用评论是为了弥补我们在代码中表达自己的错误.注意我使用了失败这个词.我的意思是.评论总是失败."

当我看到评论时,我总是会想到这句话,所以我不会惊讶你的热血沸腾.


Joh*_*lan 5

没有任何价值.如果您的版本控制中有一个怪工具,这将实现这一点,他们只是添加噪音.

更糟糕的是,他们会在您的代码中吸引更多评论,使其完全无法读取

// added for superEnterpriseyWonder v2.5
string superMappingTag = MakeTag(extras);
if (superMappingTag.empty())
{
     // bug fix #12345674 shuld have been true
     autoMapping = true;
     // bug fix #12345674 should have been true
     i++; // v2.6 now need to up the counter DO NOT DELETE
}
// end added for superEnterpriseyWonder v2.5
Run Code Online (Sandbox Code Playgroud)

然后有人会删除该方法,但保留代码注释

// added for superEnterpriseyWonder v2.5
     // bug fix #12345674 should have been true
     // v2.6 now need to up the counter DO NOT DELETE
// end added for superEnterpriseyWonder v2.5
Run Code Online (Sandbox Code Playgroud)

只是对糟糕的评论说不