x-pert
07-06-2007, 07:27 PM
Please read lesson 01 (http://www.elakiri.com/forum/showthread.php?t=35788) and lesson 02 (http://www.elakiri.com/forum/showthread.php?t=35876) to get a clear idea befor reading this....
Thought to start this lesson with some words of ma mate L Flon. :lol:
He once said.... “There is no programming language, no matter how structured, that will prevent Programmers from writing bad programs.”
Keeping that in mind... Lets start this.
OK... First thing first. How to insert a comment to the codings and what is the purpose of inserting comments to your codes.
You might think that programmers will always write codings. No guys. That’s a myth. :D
Far more time is spent maintaining, upgrading and debugging existing code.
Actually according to my data sources the amount of time which we spend in maintaining is skyrocketing.
From 1980 to 2000 the average lines of code went from 23,000 to 2,200,000. The average age of an application went up from 4.75 years to 11.3 years.
So just the same as a human.... Source codes also should be maintained within its life time.
When comes to maintaining codes, there are 2 big big problems which we have to face.
1) Code is difficult to correct, coz the authors even can’t understand it.
2) Modifications or upgrades are difficult to make coz the programmer should spend a considerable amount of time figuring out what the program does.
To make your coding more readable and more understandable, you have to include comments appropriately. Usually it is recommended to have 40% of your lines of code as comments. :P
Comments in C language starts with a slash asterisk (/*) and ends with an asterisk slash (*/).
/* ************************************************** *****
* hello -- program to print "Hello World". On the screen.*
* *
* *
* Author: NRYDDS a.k.a x-pert. *
* *
* Purpose: Demonstration of a simple program. *
* *
* Usage: *
* Other comments: exclusively from http://www.elakiri.com *
* *
* Runs the program and the message appears. *
************************************************** ***** */
#include <stdio.h>
int main()
{
/* Tell the world hello */
printf("Hello World\n");
return (0);
}
In the above example, I have Bold the start and the end of the comment.
All the characters which are between those two points belong to the comment.
These types of comments are called ‘comment boxes’.
Less important comments are not boxed.
Such as /* Tell the world hello */ in the above example.
Some commenting styles as in: Oualline, S. Practical C programming, O’Reilly Publications
/************************************************** ******
************************************************** ******
******** WARNING: This is an example of a *******
******** warning message that grabs the *******
******** attention of the programmer. *******
************************************************** ******
************************************************** ******/
/*------------> Another, less important warning <--------*/
/*>>>>>>>>>>>> Major section header <<<<<<<<<<<<<<<< */
/************************************************** ******
* We use boxed comments in this book to denote the *
* beginning of a section or program. *
************************************************** ******/
/*------------------------------------------------------*\
* This is another way of drawing boxes. *
\*------------------------------------------------------*/
/*
* This is the beginning of a section.
* ^^^^ ^^ ^^^ ^^^^^^^^^ ^^ ^ ^^^^^^^
*
* In the paragraph that follows, we explain what
* the section does and how it works.
*/
/*
* A medium-level comment explaining the next
* dozen (or so) lines of code. Even though we don't have
* the bold typeface, we can **emphasize** words.
*/
/* A simple comment explaining the next line */
Thought to start this lesson with some words of ma mate L Flon. :lol:
He once said.... “There is no programming language, no matter how structured, that will prevent Programmers from writing bad programs.”
Keeping that in mind... Lets start this.
OK... First thing first. How to insert a comment to the codings and what is the purpose of inserting comments to your codes.
You might think that programmers will always write codings. No guys. That’s a myth. :D
Far more time is spent maintaining, upgrading and debugging existing code.
Actually according to my data sources the amount of time which we spend in maintaining is skyrocketing.
From 1980 to 2000 the average lines of code went from 23,000 to 2,200,000. The average age of an application went up from 4.75 years to 11.3 years.
So just the same as a human.... Source codes also should be maintained within its life time.
When comes to maintaining codes, there are 2 big big problems which we have to face.
1) Code is difficult to correct, coz the authors even can’t understand it.
2) Modifications or upgrades are difficult to make coz the programmer should spend a considerable amount of time figuring out what the program does.
To make your coding more readable and more understandable, you have to include comments appropriately. Usually it is recommended to have 40% of your lines of code as comments. :P
Comments in C language starts with a slash asterisk (/*) and ends with an asterisk slash (*/).
/* ************************************************** *****
* hello -- program to print "Hello World". On the screen.*
* *
* *
* Author: NRYDDS a.k.a x-pert. *
* *
* Purpose: Demonstration of a simple program. *
* *
* Usage: *
* Other comments: exclusively from http://www.elakiri.com *
* *
* Runs the program and the message appears. *
************************************************** ***** */
#include <stdio.h>
int main()
{
/* Tell the world hello */
printf("Hello World\n");
return (0);
}
In the above example, I have Bold the start and the end of the comment.
All the characters which are between those two points belong to the comment.
These types of comments are called ‘comment boxes’.
Less important comments are not boxed.
Such as /* Tell the world hello */ in the above example.
Some commenting styles as in: Oualline, S. Practical C programming, O’Reilly Publications
/************************************************** ******
************************************************** ******
******** WARNING: This is an example of a *******
******** warning message that grabs the *******
******** attention of the programmer. *******
************************************************** ******
************************************************** ******/
/*------------> Another, less important warning <--------*/
/*>>>>>>>>>>>> Major section header <<<<<<<<<<<<<<<< */
/************************************************** ******
* We use boxed comments in this book to denote the *
* beginning of a section or program. *
************************************************** ******/
/*------------------------------------------------------*\
* This is another way of drawing boxes. *
\*------------------------------------------------------*/
/*
* This is the beginning of a section.
* ^^^^ ^^ ^^^ ^^^^^^^^^ ^^ ^ ^^^^^^^
*
* In the paragraph that follows, we explain what
* the section does and how it works.
*/
/*
* A medium-level comment explaining the next
* dozen (or so) lines of code. Even though we don't have
* the bold typeface, we can **emphasize** words.
*/
/* A simple comment explaining the next line */