Comments in Different Programming Languages

How to Write Comments in Different Programming Languages?

Hello Programmers,

With the help of this post, we will learn ways to write comments in different programming languages.

Introduction

Comments in coding help us to describe the intent of the code. By reading comments, we know what the piece of code/logic is doing.

We are going to learn:

  1. What is a comment?
  2. Benefits of comments
  3. How we can comment on our code?

What is a Comment?

Comments in programming are text annotations that are applied to the software to provide the source code with explanatory details.

A comment is a piece of text that won’t be seen by the end-user.

11298 903475

Benefits of Comments

Comments are a very useful and important part of programming/coding activity.

While writing a program, people think a lot before deleting a piece of code. They always want to refer to that piece of logic again and again until they finalize the code.

So the best way is to comment on that piece of code/logic for time being.

The benefits of having comments in your code are:

  • Comments are very useful for a third person to understand your code.
  • Well commented code is easier to read and follow.
  • Commented code is never executed.
Write Comments in Different Programming Languages
Source: Freepik

How to Comment Your Code?

Adding comments in code is very simple. There are different ways to add comments in different programming languages.

Example:

If we are coding in the C# programming language, in order to add a single-line comment, we need to add a comment using the “//” symbol.

int a = 10;
int b = 20;
int c = a + b; //addition of two integers

In order to add a multi-line comment, we need to add a comment using the “/*  */” symbol.

/* Code snippet displaying addition
of two numbers */
int a = 10;
int b = 20;
int c = a + b;

Documentation Comments in C#

Comments can be used as documentation in C#. To add documentation comments using Visual Studio, we just need to press “///” and it will generate the XML comments as shown below:

Comments in Different Programming Languages

To know more about documentation comments 👉 Read Here

Comments in Different Programming Languages

I have listed how to write comments in different programming languages in tabular format so that it’s easy to read and find while you are looking for a specific language.

Comments in JavaScript

JavaScript Comments
Single line comments // Comment here
Multi-line comments /* Multi-line comments
in JavaScript */

Comments in C#

C# Comments
Single line comments // Comment here
Multi-line comments /* Multi-line comments
in C# */

Comments in GO

Go Comments
Single line comments // Comment here
Multi-line comments /* Multi-line comments
in Go */

Comments in Python

Python Comments
Single line comments # Comment here
Multi-line comments # Multi-line comments
# in Python

Comments in Ruby

Ruby Comments
Single line comments # Comment here
Multi-line comments =begin
Multi-Line comment
in Ruby
=end

Comments in C

C Comments
Single line comments // Comment here
Multi-line comments /* Multi-line comments
in C */

Comments in C++

C++ Comments
Single line comments // Comment here
Multi-line comments /* Multi-line comments
in C++ */

Comments in PHP

PHP Comments
Single line comments // Comment here
OR
# Comment here
Multi-line comments /* Multi-line comments
in PHP */

Comments in VB.NET

VB.NET Comments
Single line comments ‘ Comment here
Multi-line comments ‘ Multi-line comments
‘ in VB.NET

Comments in Java

Java Comments
Single line comments // Comment here
Multi-line comments /* Multi-line comments
in Java */

Comments in Kotlin

Kotlin Comments
Single line comments // Comment here
Multi-line comments /* Multi-line comments
in Kotlin */

Comments in Swift

Swift Comments
Single line comments // Comment here
Multi-line comments /* Multi-line comments
in Swift */

Comments in TypeScript

TypeScript Comments
Single line comments // Comment here
Multi-line comments /* Multi-line comments
in Typescript*/

Comments in R

R Comments
Single line comments # Comment here
Multi-line comments Doesn’t support multi-line
comments

Comments in Perl

Perl Comments
Single line comments # Comment here
Multi-line comments =begin
Multi-Line comment
in Perl
=end

Comments in Rust

Rust Comments
Single line comments // Comment here
Multi-line comments /* Multi-line comments
in Rust */

Comments in ErLang

ErLang Comments
Single line comments % Comment here
Multi-line comments Doesn’t support multi-line
comments

Comments in Elixir

Elixir Comments
Single line comments # Comment here
Multi-line comments Doesn’t support multi-line
comments

Comments in Haskell

Haskell Comments
Single line comments – – Comment here
Multi-line comments {- Multiline comment
in Haskell
-}

Comments in Scala

Scala Comments
Single line comments // Comment here
Multi-line comments /* Multiline comment
in Scala
*/

Code Commenting Best Practices

Code commenting is good but refrain from over commenting. These code commenting best practices might be helpful for you:

  • Keep your comments relevant and to the point.
  • Use TODO comments often.
  • Verify comments during the code-review process.
  • Make use to remove commented out code.

⭐Course Recommendation

Complete Access to 35+ Courses

To land your coding dream job, you need to be skilled in as many areas of coding as you can. You need to know various programming languages, data structures, design patterns, front-end/back-end libraries, databases, etc.

master coding skills

 

Summary

So to summarize, we have learned to write comments in different programming languages.

This post will act as a reference point whenever you want to add comments in your code using any of the above-listed programming languages. So, you can bookmark our “comments in different programming languages” post.

Spread a word with others devs { 🙂 }

Share with your friends:

5 thoughts on “How to Write Comments in Different Programming Languages?”

  1. Is it advisable to write comments for every line of code?

    Because I am working for a client and he needs an explanation for every function or method written in a code.

Leave a Comment

Your email address will not be published. Required fields are marked *