We also cover two common types of comments and how to write them in 15 different programming languages.
What are comments in programming?
Comments in layman terms are pieces of text that will not be visible to end-users and are for the reference of people who are writing code. A bit more formally, A comment is a description of a program about how it works in a simple readable format. These are usually used at places where some additional clarity needs to be provided to the developer who is reading through the code. Thus, thereby this helps in improving code readability and reduces time and communication that would be required for transferring knowledge.
Compiler/Interpreter simply ignores the comments in your code thereby not impacting the end output of your program. So in a nutshell comments are like a simple readable explanation for certain pieces of code.
Common Types of Comment
Most programming languages support 2 types of comments.
Single Line Comment Multi-Line Comment / Block Comments
As the name suggests single-line comments are useful when one needs to add small single-line text eg. a particular condition, a constant value that needs a small description. Whereas multi-line work better when we have a larger amount of information to be added like a description about a function and how one can use it, etc. There are some other types of comments like Document comments but that is out of the scope of this article.
Why should you add comments?
The major advantage of adding comments is the enhanced readability and better understanding of the program. Apart from this, some other advantages include –
Comments make it super easy for your peers and even other developers to understand the logic without reading through long documentations. Reduced communication between developers for small doubts Comments are ignored by the compiler/interpreters. For Open source software, these are a must since you won’t be there to explain every function to millions of developers who want to use that particular program.
Comments in Multiple Languages
Awesome, now that we learned about comments and why they are useful. Let’s explore how we can add comments in different programming languages.
Comments in C/C++
Single Line Comment Multi-Line Comment
Comments in Python
Single Line Comment Multi-Line Comment
Comments in Java
Single Line Comment Multi-Line Comment
Comments in Ruby
Single Line Comment Multi-Line Comment
Comments in Golang
Single Line Comment Multi-Line Comment
Comments in Haskell
Single Line Comment Multi-Line Comment
Comments in Rust
Single Line Comment Multi-Line Comment
Comments in HTML
Single Line Comment Multi-Line Comment
Comments in CSS
Single Line Comment Multi-Line Comment
Comments in JavaScript
Single Line Comment Multi-Line Comment
Comments in R
Single Line Comment Multi-Line Comment R doesn’t support multi-line comments.
Comments in Erlang
Single Line Comment Multi-Line Comment R doesn’t support multi-line comments.
Comments in PHP
Single Line Comment Multi-Line Comment
Comments in Perl
Single Line Comment Multi-Line Comment
Comments in Kotlin
Single Line Comment Multi-Line Comment
Conclusion
The tutorial covered basics about comments and 2 common types – Single and Multi-line. I also tried to cover why are comments useful and one should write them when needed. I hope you learned something new! Keep exploring. Keep learning! 👨💻