• Get application security done the right way! Detect, Protect, Monitor, Accelerate, and more…
  • Let’s find out what are comments and why they are important in programming.

    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?

    “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler

    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

    // This is a single line comment in C/C++

    Multi-Line Comment

    /* This is slightly long
    multi line comment in C/C++ */

    Comments in Python

    Single Line Comment

    # This is a single line comment in Python

    Multi-Line Comment

    # This is slightly long
    # multi line comment in Python

    Comments in Java

    Single Line Comment

    // This is a single line comment in Java

    Multi-Line Comment

    /* This is slightly long
    multi line comment in Java */

    Comments in Ruby

    Single Line Comment

    # This is a single line comment in Ruby

    Multi-Line Comment

    =begin 
    This is slightly long
    multi line comment in Ruby
    =end

    Comments in Golang

    Single Line Comment

    // This is a single line comment in Golang

    Multi-Line Comment

    /* This is slightly long
    multi line comment in Golang */

    Comments in Haskell

    Single Line Comment

    – – This is a single line comment in Haskell

    Multi-Line Comment

    {- This is slightly long
    multi line comment in Haskell -}

    Comments in Rust

    Single Line Comment

    // This is a single line comment in Rust

    Multi-Line Comment

    /* This is slightly long
    multi line comment in Rust */

    Comments in HTML

    Single Line Comment

    <!--  This is a single line comment in HTML -->

    Multi-Line Comment

    <!-- This is slightly long
    multi line comment in HTML -->

    Comments in CSS

    Single Line Comment

    /* This is a single line comment in CSS */

    Multi-Line Comment

    /* This is slightly long
    multi line comment in CSS */

    Comments in JavaScript

    Single Line Comment

    // This is a single line comment in Javascript

    Multi-Line Comment

    /* This is slightly long
    multi line comment in Javascript */

    Comments in R

    Single Line Comment

    # This is a single line comment in R programming language

    Multi-Line Comment

    R doesn’t support multi-line comments.

    Comments in Erlang

    Single Line Comment

    % This is a single line comment in Erlang

    Multi-Line Comment

    R doesn’t support multi-line comments.

    Comments in PHP

    Single Line Comment

    // This is a single line comment in PHP

    Multi-Line Comment

    /* This is slightly long
    multi line comment in PHP */

    Comments in Perl

    Single Line Comment

    # This is a single line comment in Perl

    Multi-Line Comment

    =begin 
    This is slightly long
    multi line comment in Perl
    =end

    Comments in Kotlin

    Single Line Comment

    // This is a single line comment in Kotlin

    Multi-Line Comment

    /* This is slightly long
    multi line comment in Kotlin */

    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! 👨‍💻