Saturday 12 November 2011

Code Should Be Liberally Commented

Code Should Be Liberally Commented

Again, some programmers find commenting one’s code to be a real drag. But even if you never intend to have
someone else look at your code, it’s a very good idea to liberally comment it. Even code that you have written

that seems very clear to you now can become ugly and impossible to read after a few months. This is not to
say that commenting can never be bad; too many comments can actually obscure the meaning of the code. But it can be a good idea to place a few lines of comment in each function and before each bit of code that is doing something important or something unclear. Here is an example of what might be considered wellcommented
code:
/*
* Compute an integer factorial value using recursion.
* Input : an integer number.
* Output : another integer
* Side effects : may blow up stack if input value is *Huge*
*/
int factorial( int number )
{
if ( number <= 1 )
return 1; /* The factorial of one is one; QED */
else
return n * factorial( n - 1 )/
/* The magic! This is possible because the factorial of a
number is the number itself times the factorial of the
number minus one. Neat! */
}

No comments:

Post a Comment