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

时间:2014-03-13 21:12:39

标签: algorithm comments project pseudocode

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

示例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.

我知道代码是合理的自我记录,但想象代码足够复杂,无需解释。

我也知道这对于SO来说是有问题的,但我不知道有更好的地方可以提出这个问题,所以如果有的话,我会理解正确的方向。

1 个答案:

答案 0 :(得分:1)

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

如果评论中没有说明代码没有说明,那么最好将它们排除在外!在说同样的事情时给予用户两倍的阅读量是没有用的。

(在您的示例中,所有注释都是那种。)