Comment
Try to remember what you ate last night. Easy right? What about Thursday night one week ago? What about a month? If you don't have a pitch perfect Instagram account, you won't be able to easily answer these questions. Programming is much harder than remembering your last meal. So how does a programmer remember, what have they done with a complex piece of code 5 years ago?
The ingenious solution is called “Comment”.
Commenting involves placing Human Readable Descriptions inside of computer programs detailing what the Code is doing. [Source]
Let's decrypt the statement. I am sure you have seen sidenotes on books. Those side notes provide example and explanation of complex concepts. Comments are exactly that. Providing side notes, reference, explanation that will help us understand the code in the future.
How to Comment?
In C, we can comment the code in two ways. One is commenting a block of text that covers multiple line, and the other is commenting just one line.
1. Single Line Comment:
By using “//” a single line comment can be made.
#include <stdio.h>
int main() {
printf("It's unbelievably chilly today!\n"); // I am learning to comment!
return 0;
}
By pressing ctrl+F5
, you get the following result.
If you observe, you can see the compiler totally ignored the following line
// I am learning to comment!
One important aspect is single line comment can be added in a line that has code in it, but it after comment no new code can be added.
2. Multi Line Comment:
In this method, we can take as many line as we want to explain a complex mechanism.
/* I always wondered why every programming language starts with “Hello World”
printing demo. After some thought, I think I know why.
1. It enables to show the learner an instant result. Dopamine gets released in
our brain and that enables us to feel happiness and a sense of accomplishment.
2. Print a sentence is one of the easiest form of programming for any given
languages.
That is why, here is a printing program to calibrate the Eureka moment. */
#include <stdio.h>
int main() {
printf("The ranting above is unbearable\n");
return 0;
}
Again, hitting ctrl+F5
,
Compiler ignored a whole paragraph(rant) which doesn't follow C Syntax at all.
Why does Commenting Matter?
Commenting is not any less significant than documentation at all. Good comments are like time investment that can save myriad amount of time in the future. It can contain custom algorithm, reference link, maybe some warning even!(Some codes are so ancient and obscure that touching them is sure to cause breakage)