Concept of Comments in Computer Programming

29 Aug, 2018
Comments are text notes added to the program to provide explanatory information about the source code. They are used in a programming language to document the program and remind programmers of what tricky things they just did with the code and also helps the later generation for understanding and maintenance of code. The compiler considers these as non-executable statements. Different programming language uses a different way of including the comments in the source code.
/* this is a comment in C. This comment syntax is guaranteed towork on every compiler */  // This is also a comment in C// but it might present portability challenges
<!-- This is a comment in HTML-->
Comments can be used for the various purpose like :
Planning and reviewing : In comments, we can write the pseudocode which we planned before writing the source code. Pseudocode is a mixture of natural language and high-level programming language. This helps in reviewing the source code more easily because pseudocode is more understandable than the program.Example –// GCD BY EUCLID'S ALGORITHM/* Euclid(m, n){      while m does not divide n      r = n mod m      n = m      m = r      end      return m}*/In the above example, I have written the pseudo code of gcd in comments. This will help during the implementation of the code.Code description : Code description is used by the programmer to make others understand his/her intent. It contains the summary of the code.{    /* A Multiline Comment --           Define a variable of           string type and assign           value to it*/    string msg = "GeeksforGeeks";}Algorithmic description : Comments are used for explanation of the methodology. Such explanations may include diagrams and formal mathematical proofs. This may constitute the explanation of the code, rather than a clarification of its intent. For example, a programmer may add a comment to explain why an insertion sort was chosen instead of a quicksort, as the former is, in theory, slower than the latter.list = [ f(b), f(b), f(c), f(d), f(a), ... ];  // Need a stable sort. Besides,// the performance really does not matter.insertion_sort(list);Resource inclusion : Logos, diagrams, and flowcharts consisting of ASCII art constructions can be inserted into source code formatted as a comment. Further, copyright notices can be embedded within source code as comments.Metadata : Comments also contain metadata of the program. This metadata helps in software maintenance. The metadata includes the name of the creator of the original version, current maintainer of the program, data when the first version was created, the name of the people who have edited the program files so far etc.Debugging : Brute force method is a common method of debugging. In this approach, print statements are inserted throughout the program to print the intermediate values with the hope that some of the printed values will help to identify the errors. After doing debugging we comment those print statements. Hence comment is also used for debugging.int fun(int m){    int count = 0;    while (m > 10) {          // printf("m is less than 10, m=%d", m);        count++;    }    return m;}Automatic documentation generation : Programming tools sometimes store documentation and metadata in comments. These may include insert positions for automatic header file inclusion, commands to set the file’s syntax highlighting mode, or the file’s revision number. These functional control comments are also commonly referred to as annotations. Keeping documentation within source code comments is considered as one way to simplify the documentation process, as well as increase the chances that the documentation will be kept up to date with changes in the code.Stress relief : Commenting on development tools, competitors, employers, working conditions, or the quality of the code itself are the ways to relieve stress. The occurrence of this phenomenon can be easily seen from online resources that track profanity in source code.
Planning and reviewing : In comments, we can write the pseudocode which we planned before writing the source code. Pseudocode is a mixture of natural language and high-level programming language. This helps in reviewing the source code more easily because pseudocode is more understandable than the program.Example –// GCD BY EUCLID'S ALGORITHM/* Euclid(m, n){      while m does not divide n      r = n mod m      n = m      m = r      end      return m}*/In the above example, I have written the pseudo code of gcd in comments. This will help during the implementation of the code.
// GCD BY EUCLID'S ALGORITHM/* Euclid(m, n){      while m does not divide n      r = n mod m      n = m      m = r      end      return m}*/
In the above example, I have written the pseudo code of gcd in comments. This will help during the implementation of the code.
Code description : Code description is used by the programmer to make others understand his/her intent. It contains the summary of the code.{    /* A Multiline Comment --           Define a variable of           string type and assign           value to it*/    string msg = "GeeksforGeeks";}
{    /* A Multiline Comment --           Define a variable of           string type and assign           value to it*/    string msg = "GeeksforGeeks";}
Algorithmic description : Comments are used for explanation of the methodology. Such explanations may include diagrams and formal mathematical proofs. This may constitute the explanation of the code, rather than a clarification of its intent. For example, a programmer may add a comment to explain why an insertion sort was chosen instead of a quicksort, as the former is, in theory, slower than the latter.list = [ f(b), f(b), f(c), f(d), f(a), ... ];  // Need a stable sort. Besides,// the performance really does not matter.insertion_sort(list);
list = [ f(b), f(b), f(c), f(d), f(a), ... ];  // Need a stable sort. Besides,// the performance really does not matter.insertion_sort(list);
Resource inclusion : Logos, diagrams, and flowcharts consisting of ASCII art constructions can be inserted into source code formatted as a comment. Further, copyright notices can be embedded within source code as comments.
Metadata : Comments also contain metadata of the program. This metadata helps in software maintenance. The metadata includes the name of the creator of the original version, current maintainer of the program, data when the first version was created, the name of the people who have edited the program files so far etc.
Debugging : Brute force method is a common method of debugging. In this approach, print statements are inserted throughout the program to print the intermediate values with the hope that some of the printed values will help to identify the errors. After doing debugging we comment those print statements. Hence comment is also used for debugging.int fun(int m){    int count = 0;    while (m > 10) {          // printf("m is less than 10, m=%d", m);        count++;    }    return m;}
int fun(int m){    int count = 0;    while (m > 10) {          // printf("m is less than 10, m=%d", m);        count++;    }    return m;}
Automatic documentation generation : Programming tools sometimes store documentation and metadata in comments. These may include insert positions for automatic header file inclusion, commands to set the file’s syntax highlighting mode, or the file’s revision number. These functional control comments are also commonly referred to as annotations. Keeping documentation within source code comments is considered as one way to simplify the documentation process, as well as increase the chances that the documentation will be kept up to date with changes in the code.
Stress relief : Commenting on development tools, competitors, employers, working conditions, or the quality of the code itself are the ways to relieve stress. The occurrence of this phenomenon can be easily seen from online resources that track profanity in source code.
Learn more about Comments in C/C++, Java, HTML, C#
CBSE - Class 11
Picked
Programming Basics
school-programming
School Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Constructors in Java
Exceptions in Java
Python Exception Handling
Python Try Except
Ternary Operator in Python
How JVM Works - JVM Architecture?
Python program to add two numbers
Variables in Java
Data types in Java
Difference between Abstract Class and Interface in Java