Commenting Flashcards
What is the importance of commenting within a method?
Commenting within a method is important to improve code readability and to help other programmers understand the purpose and functionality of the code.
In what parts of a method should you consider adding comments?
You should consider adding comments to the variable declarations, branching structures (if-, switch-statements), and loops (while-, do-, for-statements).
Why is it important to comment on variable declarations?
It is important to comment on variable declarations to provide information about the type of variable and its purpose in the code.
Why is it important to comment on branching structures and loops?
: It is important to comment on branching structures and loops to explain the logic and flow of the code and to highlight any specific conditions or scenarios that are being checked.
What is the benefit of commenting on a program structure?
Commenting on a program structure helps to improve the code’s readability and maintainability by allowing other programmers to quickly understand the purpose and functionality of the code without having to read through every line of the code.
Examples of Variable declarations
[] int p //polynomial
int i,j //counters
How do you comment branching
State the purpose of the whole branching structure, explain what happens in each of the cases
Example of comments in branches
//add polynomials only if they are of the same degree
If degree(p) = degree(q){
then // add polynomials
{…}
else //error case
{…}
}
How do you comment loops
State the purpose of the whole loop structure, explaining how the computation takes, place, discuss termination of the loop
Example of commenting in loops
/* evaluate the polynomial p
Implements Horner’s Method
Terminates as k is not touched in the loop body and k is initialised to a positive value
*/
b = p[n]
for (k = degree(p); k >= 0; k = k - 1) {
…
}
What is something to avoid
Over-commenting