In this post, we're gonna learn how to write a comment in
Before we getting started, let's first know more about Comments in Programming Languages.
- Comments are used for explaining code.
- Compilers ignore the comment entries.
- Comments should always be short, straight to the point, and add informative value.
There are two types of comments:
- Single-line comments.
- Multi-line comments.
Comments in C#
Start with/* and end with */
Comments in Python
Start with #
Technically Python doesn't have explicit support for multi-line comments.
Generally, you’d use a single line comment in multiple-lines.
Lastly, if, instead of a comment, what you really want to achieve is function or class documentation, the most recommended way to do that is by using docstring.
Docstring is an abbreviation for documentation string that describe your source code.
A docstring is either a single line or a multi-line comment.
- Python allows you to use three single-quotes as well as three double-quotes.
- Make sure that the closing quotes are in the same line.
- '''This is an example of single comment that use three single quotes.'''
- """This is an example of single comment that use three double quotes."""
A Docstring can be written as multi-line comment using three single quotes or three double quotes.
Comments in Java
- The compiler ignores everything from /* to */.
- /** documentation */ This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment.
- The compiler ignores everything from // to the end of the line.
- Use java doc when you are writing formal APIs for your comments
In this post, we have learned that writing comments in your code is not that complicated and it helps all of us to understand the code including yourself when you want to revise or change your code.