Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
2.4k views
in Blog Post by 48 54 93
edited by

In this post, we're gonna learn how to write a comment in

  • Python,
  • C#,
  • Java.

Before we getting started, let's first know more about Comments in Programming Languages.

What's Comment?

  • Comments are used for explaining code.
  • Compilers ignore the comment entries.
  • Comments should always be short, straight to the point, and add informative value.

Comments Type

There are two types of comments:

  • Single-line comments.
  • Multi-line comments.

Comments in C#

Single-line comment

Start with//

Multi-line comment

Start with/* and end with */


Comments in Python

Single-line comment

Start with #

Multi-line comment

Technically Python doesn't have explicit support for multi-line comments.

Generally, you’d use a single line comment in multiple-lines.

Docstrings

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.

Single-line Docstrings

  • 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."""

Multi-line Docstrings

A Docstring can be written as multi-line comment using three single quotes or three double quotes.


Comments in Java

Single-line comments

  • 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.

Multi-line comments

 

JavaDoc

  • Use java doc when you are writing formal APIs for your comments


Conclusion

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.


If you don’t ask, the answer is always NO!
...