Lably
Skip to content

Comment (computer programming)

Text in computer source code that is generally ignored by a compiler/interpreter

  • 27 min read
  • 7 chapters
  • 1 lab on Lably

Learn it on Lably

The flexibility supported by comments allows for a wide degree of content style variability. To promote uniformity, style conventions are commonly part of a programming style guide. But, best practices are disputed and contradictory.

Common attributes

Support for code comments is defined by each programming language. The features differ by language, but there are several common attributes that apply throughout.

Most languages support multi-line block (a.k.a. stream) and/or single line comments. A block comment is delimited with text that marks the start and end of comment text. It can span multiple lines or occupy any part of a line. Some languages allow block comments to be recursively nested inside one another, but others do not. A line comment ends at the end of the text line. In modern languages, a line comment starts with a delimiter but some older languages designate a column at which subsequent text is considered comment. Many languages support both block and line comments – using different delimiters for each. For example, C, C++ and their many derivatives support block comments delimited by /* and */ and line comments delimited by //. Other languages support only one type of comment.

Comments can also be classified as either prologue or inline based on their position and content relative to program code. A prologue comment is a comment (or group of related comments) located near the top of an associated programming topic, such as before a symbol declaration or at the top of a file. An inline comment is a comment that is located on the same line as and to the right of program code to which it refers. Both prologue and inline comments can be represented as either line or block comments. For example:

<span id="mwYA"></span><span class="cm" id="mwYQ">/*</span> <span class="cm" id="mwYg"> * prologue block comment</span> <span class="cm" id="mwYw"> */</span> <span class="kt" id="mwZA">bool</span><span class="w" id="mwZQ"> </span><span class="nf" id="mwZg">foo</span><span class="p" id="mwZw">()</span><span class="w" id="mwaA"> </span><span class="p" id="mwaQ">{</span> <span class="w" id="mwag"> </span><span class="k" id="mwaw">return</span><span class="w" id="mwbA"> </span><span class="nb" id="mwbQ">true</span><span class="p" id="mwbg">;</span><span class="w" id="mwbw"> </span><span class="cm" id="mwcA">/* inline block comment */</span> <span class="p" id="mwcQ">}</span> <span class="c1" id="mwcg">//</span> <span class="c1" id="mwcw">// prologue line comment</span> <span class="c1" id="mwdA">//</span> <span class="kt" id="mwdQ">bool</span><span class="w" id="mwdg"> </span><span class="nf" id="mwdw">bar</span><span class="p" id="mweA">()</span><span class="w" id="mweQ"> </span><span class="p" id="mweg">{</span> <span class="w" id="mwew"> </span><span class="k" id="mwfA">return</span><span class="w" id="mwfQ"> </span><span class="nb" id="mwfg">false</span><span class="p" id="mwfw">;</span><span class="w" id="mwgA"> </span><span class="c1" id="mwgQ">// inline line comment</span> <span class="p" id="mwgg">}</span>

Sign in to keep reading

6 more chapters are free with a Lably account.

More in Computing and communication