algorithm - 解释伪代码算法 - 中间或之后

标签 algorithm comments project pseudocode

我想知道在解释伪代码算法时,在代码中使用注释或事后解释来解释它是常见的做法还是更好的风格。这是我的 A-level 计算项目,在解释我在设计部分使用的算法时。例如,比较以下内容:

示例 1

Declare Operand1 as Integer, Set = 5
Declare Operand2 as Integer, Set = 68
Declare Result as Integer
//Result variable is calculated by adding values of Operand1 and Operand2.
Set Result = Operand1 + Operand2
//The Result is printed on the Console.
Print Result

示例 2

Declare Operand1 as Integer, Set = 5
Declare Operand2 as Integer, Set = 68
Declare Result as Integer
Set Result = Operand1 + Operand2
Print Result

This algorithm creates two initialised integer variables, Operand1 and Operand2,
and a third uninitialised variable Result. The value of Result is calculated by
adding together the values of Operand1 and Operand2, and this Result is then
printed onto the Console.

我知道代码是合理的 self 记录,但想象一下代码足够复杂以至于需要解释。

我也知道这对 SO 来说是有问题的,但我不知道问这个问题的更好的地方,所以如果有一个正确的方向,我将不胜感激。

最佳答案

如果代码相对不言自明,最好有摘要;如果不太可能在你读完时记住所有细节,最好有注释。 (虽然在学术出版中,通常代码只是代码,所有解释都在外面。)

如果注释没有说明代码没有说明的内容,最好将它们排除在外!在说同样的话的同时给用户两倍的阅读量是没有帮助的。

(在你的例子中,所有的评论都是那种。)

关于algorithm - 解释伪代码算法 - 中间或之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22391022/

相关文章:

android - 防止 Android Studio/Intellij Idea 在启动时构建项目

java - JMH结果和算法复杂度的解读

c++ - 为什么将 operator< 作为成员函数重载是不好的做法?

python - 如何在 python 注释中包含超长 URL

javascript - jsdoc3 注释对象

java - 共享 Eclipse Java 项目

c# - 一种在不存储 key 的情况下将 key 存储在字典中的方法?

c++ - 具有求和界限的重复排列

php - 如何在 PHP 中的字符串中进行注释

android - 当我们从 Fragment 类调用 facebook 或 twitter 时如何处理后退按钮?