我想知道在解释伪代码算法时,在代码中使用注释或事后解释来解释它是常见的做法还是更好的风格。这是我的 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/