Comment

    0
    7
    « Back to Glossary Index

    What are Comments in Programming?

    In programming, a comment is a section of text embedded in source code that the compiler or interpreter ignores.

    Comments exist solely for the benefit of human readers; they describe intent, explain complex logic, mark sections requiring improvement, or remind developers about future work.

    Unlike executable statements, comments do not affect program behavior. They are annotations to make code easier to read, maintain, and debug.

    How Comments Work

    Comments are parsed but not executed. A code translator recognizes comment delimiters and skips the enclosed text.

    Because comments are ignored, developers can disable code temporarily by converting it into a comment; this technique helps isolate bugs or test alternate logic.

    Many integrated development environments provide shortcuts to toggle comments on and off.

    Most languages support single-line and/or multi-line comments:

    • Single-line comments begin with a delimiter (e.g., // in C/C++, # in Python) and extend to the end of the line. They are used for short explanations or to comment out a single line of code.
    • Multi-line comments are delimited by start and end markers, such as /* and */ in C/C++ or “ in HTML. They can span multiple lines and are suited for longer descriptions or temporarily disabling blocks of code.
    • Inline comments can appear inside a line of code; they often clarify a specific expression or quickly change a value for testing.

    Some languages support prologue comments—blocks placed before a declaration—and inline comments beside code. Both can be written using either line or block comment syntax.

    Types of Comments and Syntax

    Different programming languages use different symbols to denote comments. The table below summarizes common syntax:

    Language Single-line comment Multi-line comment
    Python # comment """ multi-line comments """
    JavaScript // comment /* multi-line comments */
    C / C++ / Java // comment /* multi-line comments */
    HTML ` `
    SQL -- comment /* multi-line comments */
    Bash # comment : << 'COMMENT' ... COMMENT

    While the syntax differs across languages, the purpose of comments remains consistent: to provide information to humans without affecting execution.

    Best Practices for Comments in Programming

    Although comments improve readability, they should be used judiciously:

    • Explain “Why,” not “How”: Comments should describe the purpose or reasoning behind code rather than restate obvious operations.
    • Avoid Redundancy: Repeating the code in comments can clutter files and mislead readers when code changes.
    • Write Clear, Concise Comments: Use proper grammar and avoid ambiguity.
    • Keep Comments Up to Date: Outdated comments are worse than none; update or remove them as code evolves.
    • Don’t Compensate for Bad Code: Clean, self-explanatory code reduces the need for excessive comments.

    Summary

    Comments are non-executing annotations in source code that improve readability, clarify intent, and assist debugging. They come in various forms—single-line, multi-line, inline, and prologue—across different programming languages.

    By following best practices and using comments thoughtfully to explain “why” rather than “how,” programmers create clearer, more maintainable code while preserving their programs’ behavior.

    « Back to Glossary Index