Question

I want to know whether it is common practice or better stylistically to, when explaining a pseudocode algorithm, explain it using comments in the code, or with an explanation afterwards. This is for my A-level Computing Project, when explaining the algorithms I have used in the Design section. For example, compare the following:

Example 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

Example 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.

I'm aware that the code is reasonably self-documenting, but imagine that the code is sufficiently complex to need explanation.

I'm also aware this is questionable for SO but am not aware of a better place to ask this question, so would appreciate a point in the right direction if there is one.

Was it helpful?

Solution

It's better to have summaries if the code is relatively self-explanatory, and better to have comments in-line if it is unlikely to be possible to remember all the details by the time you hit the end. (Though in academic publishing, usually the code is just code and all explanations are outside.)

And if the comments say nothing that the code doesn't already say, it's better to leave them out! Giving the user twice as much to read while saying the same thing is not helpful.

(In your example, all the comments are of that sort.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top