我应该在提交消息或内联注释中写下推理吗?

发布于 2025-01-09 04:40:26 字数 155 浏览 1 评论 0原文

我经常听说提交消息应该描述我进行这些更改的原因。我想我同意这一点。然而,我也经常听说内联注释(即源代码中的注释)应该说明代码为什么以这种特定方式工作,而不是它的作用。

那么,什么时候应该将推理放入提交消息中,什么时候应该将其写在内联注释中呢?是否有一些可以应用的经验法则或最佳实践?

I've often heard that the commit message should describe why I'm making these changes. And I think I agree with that. However, I've also often heard that inline comments (i.e. comments in the source code) should tell why the code works in this particular way instead of what it does.

So, when should I put the reasoning in the commit message, and when should it be written in an inline comment? Is there some rule-of-thumb or best practice that can be applied?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凑诗 2025-01-16 04:40:26

通常,人们会使用内联注释来表达理解代码所必需的内容。例如,如果存在棘手的算法或微妙的行为,则应将其记录在注释中,以便读者在查看单个修订版时可以查看并理解代码。

您将使用提交消息来解释进行更改的理由,包括为什么选择一种替代方案而不是另一种方案。例如,如果您选择了一种算法而不是另一种算法(例如,出于性能原因),则该算法属于提交消息。您还应该记录诸如引发变革的问题或想法等内容,以帮助向未来的读者解释为什么您的变革是有价值的。

最终的区别在于,在前一种情况下,您希望它在阅读代码时可见,而在后一种情况下,您可以期望感兴趣的各方查看历史记录。

Typically, one uses inline comments for matters which are necessary to understand the code. For example, if there is a tricky algorithm or subtle behavior, that should be documented in a comment, so that the reader can look at and understand the code when looking at a single revision.

You would use the commit message to explain the rationale for making a change, including why one alternative was chosen over another. For example, if you've chosen one algorithm over another, say, for performance reasons, that belongs in the commit message. You should also document things like the problem or idea which motivated the change to help explain to future readers why your change was valuable.

The difference is ultimately that in the former case, you want it to be visible while reading the code, and in the latter case, you can expect the interested party to look through the history.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文