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
- Data Processing in JavaClass 9 · Computer Applications
- Python FundamentalsClass 11 · Computer Science
- Basic Structure of a Class in JavaClass 9 · Computer Applications
- Introduction to PythonClass 11 · Computer Science
- Office ToolsClass 9 · Computer Applications
- Python Basics and Control StatementsClass 11 · Informatics Practices
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.
Examples of use
Describe intent
Comments can explain the author's intent – why the code is as it is. Some contend that describing what the code does is superfluous. The need to explain the what is a sign that it is too complex and should be re-worked.
- "Don't document bad code – rewrite it."
- "Good comments don't repeat the code or explain it. They clarify its intent. Comments should explain, at a higher level of abstraction than the code, what you're trying to do."
Highlight unusual practice
Comments may explain why a choice was made to write code that is counter to convention or best practice. For example:
<span id="mwmg"></span><span class="c1" id="mwmw">' Second variable dim because of server errors produced when reuse form data.</span> <span class="c1" id="mwnA">' No documentation available on server behavior issue, so just coding around it.</span> <span class="n" id="mwnQ">vtx</span><span class="w" id="mwng"> </span><span class="o" id="mwnw">=</span><span class="w" id="mwoA"> </span><span class="n" id="mwoQ">server</span><span class="p" id="mwog">.</span><span class="n" id="mwow">mappath</span><span class="p" id="mwpA">(</span><span class="s2" id="mwpQ">"local settings"</span><span class="p" id="mwpg">)</span>
The example below explains why an insertion sort was chosen instead of a quicksort, as the former is, in theory, slower than the latter.
<span id="mwrA"></span><span class="w" id="mwrQ"> </span><span class="n" id="mwrg">list</span><span class="w" id="mwrw"> </span><span class="o" id="mwsA">=</span><span class="w" id="mwsQ"> </span><span class="o" id="mwsg">[</span><span class="n" id="mwsw">f</span><span class="w" id="mwtA"> </span><span class="p" id="mwtQ">(</span><span class="n" id="mwtg">b</span><span class="p" id="mwtw">),</span><span class="w" id="mwuA"> </span><span class="n" id="mwuQ">f</span><span class="w" id="mwug"> </span><span class="p" id="mwuw">(</span><span class="n" id="mwvA">b</span><span class="p" id="mwvQ">),</span><span class="w" id="mwvg"> </span><span class="n" id="mwvw">f</span><span class="w" id="mwwA"> </span><span class="p" id="mwwQ">(</span><span class="n" id="mwwg">c</span><span class="p" id="mwww">),</span><span class="w" id="mwxA"> </span><span class="n" id="mwxQ">f</span><span class="w" id="mwxg"> </span><span class="p" id="mwxw">(</span><span class="n" id="mwyA">d</span><span class="p" id="mwyQ">),</span><span class="w" id="mwyg"> </span><span class="n" id="mwyw">f</span><span class="w" id="mwzA"> </span><span class="p" id="mwzQ">(</span><span class="n" id="mwzg">a</span><span class="p" id="mwzw">),</span><span class="w" id="mw0A"> </span><span class="p" id="mw0Q">...</span><span class="o" id="mw0g">]</span><span class="p" id="mw0w">;</span> <span class="w" id="mw1A"> </span><span class="c1" id="mw1Q">// Need a stable sort. Besides, the performance really does not matter.</span> <span class="w" id="mw1g"> </span><span class="n" id="mw1w">insertion_sort</span><span class="w" id="mw2A"> </span><span class="p" id="mw2Q">(</span><span class="n" id="mw2g">list</span><span class="p" id="mw2w">);</span>
Describe algorithm
Comments can describe an algorithm as pseudocode. This could be done before writing the code as a first draft. If left in the code, it can simplify code review by allowing comparison of the resulting code with the intended logic. For example:
<span id="mw4g"></span><span class="cm" id="mw4w">/* loop backwards through all elements returned by the server </span> <span class="cm" id="mw5A">(they should be processed chronologically)*/</span> <span class="k" id="mw5Q">for</span><span class="w" id="mw5g"> </span><span class="p" id="mw5w">(</span><span class="n" id="mw6A">i</span><span class="w" id="mw6Q"> </span><span class="o" id="mw6g">=</span><span class="w" id="mw6w"> </span><span class="p" id="mw7A">(</span><span class="n" id="mw7Q">numElementsReturned</span><span class="w" id="mw7g"> </span><span class="o" id="mw7w">-</span><span class="w" id="mw8A"> </span><span class="mi" id="mw8Q">0</span><span class="p" id="mw8g">);</span><span class="w" id="mw8w"> </span><span class="n" id="mw9A">i</span><span class="w" id="mw9Q"> </span><span class="o" id="mw9g">>=</span><span class="w" id="mw9w"> </span><span class="mi" id="mw-A">1</span><span class="p" id="mw-Q">;</span><span class="w" id="mw-g"> </span><span class="n" id="mw-w">i</span><span class="o" id="mw_A">--</span><span class="p" id="mw_Q">)</span><span class="w" id="mw_g"> </span><span class="p" id="mw_w">{</span> <span class="w" id="mwAQA"> </span><span class="cm" id="mwAQE">/* process each element's data */</span> <span class="w" id="mwAQI"> </span><span class="n" id="mwAQM">updatePattern</span><span class="p" id="mwAQQ">(</span><span class="n" id="mwAQU">i</span><span class="p" id="mwAQY">,</span><span class="w" id="mwAQc"> </span><span class="n" id="mwAQg">returnedElements</span><span class="p" id="mwAQk">[</span><span class="n" id="mwAQo">i</span><span class="p" id="mwAQs">]);</span> <span class="p" id="mwAQw">}</span>Sometimes code contains a novel or noteworthy solution that warrants an explanatory comment. Such explanations might be lengthy and include diagrams and formal mathematical proofs. This may describe what the code does rather than intent, but may be useful for maintaining the code. This might apply for highly specialized problem domains or rarely used optimizations, constructs or function-calls.
Comment out
A common developer practice is to comment out one or more lines of code. The programmer adds comment syntax that converts program code into comments so that what was executable code will no longer be executed at runtime. Sometimes this technique is used to find the cause of a bug. By systematically commenting out and running parts of the program, the offending source code can be located.
Many IDEs support adding and removing comments with convenient user interface actions such as a keyboard shortcut.
Store metadata
Comments can store metadata about the code. Common metadata includes the name of the original author and subsequent maintainers, dates when first written and modified, link to development and user documentation, and legal information such as copyright and software license. Rarely, text-encoded, binary data can be included.
Some programming tools write metadata into the code as comments. For example, a version control tool might write metadata such as author, date and version number into each file when it's committed to the repository.
Integrate with development tools
Sometimes information stored in comments is used by development tools other than the translator – the primary tool that consumes the code. This information may include metadata (often used by a documentation generator) or tool configuration.
Some source code editors support configuration via metadata in comments. One particular example is the modeline feature of Vim which configures tab character handling. For example:
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
Support documentation generation
An API documentation generator parses information from a codebase to generate API documentation. Many support reading information from comments, often parsing metadata, to control the content and formatting of the resulting document.
Although some claim that API documentation can be higher quality when written in a more traditional and manual way, some claim that storing documentation information in code comments simplifies the documenting process, as well as increases the likelihood that the documentation will be kept up to date. Examples include Javadoc, Ddoc, Doxygen, Visual Expert and PHPDoc. Forms of docstring are supported by Python, Lisp, Elixir, and Clojure. C#, F# and Visual Basic .NET implement a similar feature called "XML Comments" which are read by IntelliSense from the compiled .NET assembly.
Visualization
An ASCII art visualization such as a logo, diagram, or flowchart can be included in a comment.
The following code fragment depicts the process flow of a system administration script (Windows script file). Although a section marking the code appears as a comment, the diagram is in an XML CDATA section, which is technically not a comment, but serves the same purpose here. Although this diagram could be in a comment, the example illustrates one instance where the programmer opted not to use a comment as a way of including resources in source code.
<span id="mwAXU"></span><span class="cm" id="mwAXY"><!-- begin: wsf_resource_nodes --></span> <span class="nt" id="mwAXc"><resource</span><span class="w" id="mwAXg"> </span><span class="na" id="mwAXk">id=</span><span class="s" id="mwAXo">"ProcessDiagram000"</span><span class="nt" id="mwAXs">></span> <span class="cp" id="mwAXw"><![CDATA[</span> <span class="cp" id="mwAX0"> HostApp (Main_process)</span> <span class="cp" id="mwAX4"> |</span> <span class="cp" id="mwAX8"> V</span> <span class="cp" id="mwAYA">script.wsf (app_cmd) --> ClientApp (async_run, batch_process)</span> <span class="cp" id="mwAYE"> |</span> <span class="cp" id="mwAYI"> |</span> <span class="cp" id="mwAYM"> V</span> <span class="cp" id="mwAYQ"> mru.ini (mru_history) </span> <span class="cp" id="mwAYU">]]></span> <span class="nt" id="mwAYY"></resource></span>
Document development process
Sometimes, comments describe development processes related to the code. For example, comments might describe how to build the code or how to submit changes to the software maintainer.
Extend language syntax
Occasionally, code that is formatted as a comment is overloaded to convey additional information to the translator, such as conditional comments. As such, syntax that generally indicates a comment can actually represent program code, not comment code. Such syntax may be a practical way to maintain compatibility while adding additional functionality, but some regard such a solution as a kludge.
Other examples include interpreter directives:
- The Unix "shebang" –
#!– used on the first line of a script to point to the interpreter to be used. - "Magic comments" identifying the encoding a source file is using, e.g. Python's PEP 263.
The script below for a Unix-like system shows both of these uses:
<span id="mwAaU"></span><span class="ch" id="mwAaY">#!/usr/bin/env python3</span> <span class="c1" id="mwAac"># -*- coding: UTF-8 -*-</span> <span class="nb" id="mwAag">print</span><span class="p" id="mwAak">(</span><span class="s2" id="mwAao">"Testing"</span><span class="p" id="mwAas">)</span>
The gcc compiler (since 2017) looks for a comment in a switch statement if a case falls-thru to the next case. If an explicit indication of fall-thru is not found, then the compiler issues a warning about a possible coding problem. Inserting such a comment about fall-thru is a long standing convention, and the compiler has codified the practice. For example:
<span id="mwAbU"></span><span class="linenos" data-line="1" id="mwAbY"></span><span class="k" id="mwAbc">switch</span><span class="w" id="mwAbg"> </span><span class="p" id="mwAbk">(</span><span class="n" id="mwAbo">command</span><span class="p" id="mwAbs">)</span><span class="w" id="mwAbw"> </span><span class="p" id="mwAb0">{</span> <span class="linenos" data-line="2" id="mwAb4"></span><span class="w" id="mwAb8"> </span><span class="k" id="mwAcA">case</span><span class="w" id="mwAcE"> </span><span class="n" id="mwAcI">CMD_SHOW_HELP_AND_EXIT</span><span class="p" id="mwAcM">:</span> <span class="linenos" data-line="3" id="mwAcQ"></span><span class="w" id="mwAcU"> </span><span class="n" id="mwAcY">do_show_help</span><span class="p" id="mwAcc">();</span> <span class="linenos" data-line="4" id="mwAcg"></span><span class="w" id="mwAck"> </span><span class="cm" id="mwAco">/* Fall thru */</span> <span class="linenos" data-line="5" id="mwAcs"></span><span class="w" id="mwAcw"> </span><span class="k" id="mwAc0">case</span><span class="w" id="mwAc4"> </span><span class="n" id="mwAc8">CMD_EXIT</span><span class="p" id="mwAdA">:</span> <span class="linenos" data-line="6" id="mwAdE"></span><span class="w" id="mwAdI"> </span><span class="n" id="mwAdM">do_exit</span><span class="p" id="mwAdQ">();</span> <span class="linenos" data-line="7" id="mwAdU"></span><span class="w" id="mwAdY"> </span><span class="k" id="mwAdc">break</span><span class="p" id="mwAdg">;</span> <span class="linenos" data-line="8" id="mwAdk"></span><span class="w" id="mwAdo"> </span><span class="p" id="mwAds">}</span>Relieve stress
To relieve stress or attempt humor, sometimes programmers add comments about the quality of the code, tools, competitors, employers, working conditions, or other arguably unprofessional topics – sometimes using profanity.
Normative views
There are various normative views and long-standing opinions regarding the proper use of comments in source code. Some of these are informal and based on personal preference, while others are published or promulgated as formal guidelines for a particular community.
Need for comments
Experts have varying viewpoints on whether, and when, comments are appropriate in source code. Some assert that source code should be written with few comments, on the basis that the source code should be self-explanatory or self-documenting. Others suggest code should be extensively commented (it is not uncommon for over 50% of the non-whitespace characters in source code to be contained within comments).
In between these views is the assertion that comments are neither beneficial nor harmful by themselves, and what matters is that they are correct and kept in sync with the source code, and omitted if they are superfluous, excessive, difficult to maintain or otherwise unhelpful.
Comments are sometimes used to document contracts in the design by contract approach to programming.
Level of detail
Depending on the intended audience of the code and other considerations, the level of detail and description may vary considerably.
For example, the following Java comment would be suitable in an introductory text designed to teach beginning programming:
<span id="mwAh4"></span><span class="n" id="mwAh8">String</span><span class="w" id="mwAiA"> </span><span class="n" id="mwAiE">s</span><span class="w" id="mwAiI"> </span><span class="o" id="mwAiM">=</span><span class="w" id="mwAiQ"> </span><span class="s" id="mwAiU">"Wikipedia"</span><span class="p" id="mwAiY">;</span><span class="w" id="mwAic"> </span><span class="cm" id="mwAig">/* Assigns the value "Wikipedia" to the variable s. */</span>
This level of detail, however, would not be appropriate in the context of production code, or other situations involving experienced developers. Such rudimentary descriptions are inconsistent with the guideline: "Good comments ... clarify intent." Further, for professional coding environments, the level of detail is ordinarily well defined to meet a specific performance requirement defined by business operations.
Styles
As free-form text, comments can be styled in a wide variety of ways. Many prefer a style that is consistent, non-obstructive, easy to modify, and difficult to break. As some claim that a level of consistency is valuable and worthwhile, a consistent commenting style is sometimes agreed upon before a project starts or emerges as development progresses.
The following C code fragments show some of the diversity in block comment style:
<span id="mwAjs"></span><span class="cm" id="mwAjw">/*</span> <span class="cm" id="mwAj0"> This is the comment body.</span> <span class="cm" id="mwAj4">*/</span>
<span id="mwAkE"></span><span class="cm" id="mwAkI">/*****************************</span> <span class="cm" id="mwAkM"> * *</span> <span class="cm" id="mwAkQ"> * This is the comment body. *</span> <span class="cm" id="mwAkU"> * *</span> <span class="cm" id="mwAkY"> *****************************/</span>
Factors such as personal preference, flexibility of programming tools can influence the commenting style used. For example, the first might be preferred by programmers who use a source code editor that does not automatically format a comment as shown in the second example.
Software consultant and technology commentator Allen Holub advocates aligning the left edges of comments:
<span id="mwAlU"></span><span class="w" id="mwAlY"> </span><span class="cm" id="mwAlc">/* This is the style recommended by Holub for C and C++.</span> <span class="cm" id="mwAlg"> * It is demonstrated in ''Enough Rope'', in rule 29.</span> <span class="cm" id="mwAlk"> */</span>
<span id="mwAlw"></span><span class="w" id="mwAl0"> </span><span class="cm" id="mwAl4">/* This is another way to do it, also in C.</span> <span class="cm" id="mwAl8"> ** It is easier to do in editors that do not automatically indent the second</span> <span class="cm" id="mwAmA"> ** through last lines of the comment one space from the first.</span> <span class="cm" id="mwAmE"> ** It is also used in Holub's book, in rule 31.</span> <span class="cm" id="mwAmI"> */</span>
In many languages, a line comment can follow program code such that the comment is inline and generally describes the code to the left of it. For example, in this Perl:
<span id="mwAmc"></span><span class="k" id="mwAmg">print</span><span class="w" id="mwAmk"> </span><span class="nv" id="mwAmo">$s</span><span class="w" id="mwAms"> </span><span class="o" id="mwAmw">.</span><span class="w" id="mwAm0"> </span><span class="s" id="mwAm4">"\n"</span><span class="p" id="mwAm8">;</span><span class="w" id="mwAnA"> </span><span class="c1" id="mwAnE"># Add a newline character after printing</span>
If a language supports both line and block comments, programming teams may decide upon a convention of when to use which. For example, line comments only for minor comments, and block comments to for higher-level abstractions.
Tag
Some comments are categorized with a prefix – a tag, codetag or token. Some editors highlight a comment based on its tag.
Commonly used tags include:
- BUG, DEBUG — identifies a known bug, perhaps implying it should be fixed
- FIXME — implies that there is work to do to fix a bug
- HACK, BODGE, KLUDGE — marks a solution that might be considered low quality
- TODO — describes some work to do
- NOTE — relatively general information
- UNDONE — a reversal or "roll back" of previous code
For example:
<span id="mwApA"></span><span class="kt" id="mwApE">int</span><span class="w" id="mwApI"> </span><span class="nf" id="mwApM">current_stock_price</span><span class="p" id="mwApQ">()</span><span class="w" id="mwApU"> </span><span class="p" id="mwApY">{</span> <span class="w" id="mwApc"> </span><span class="k" id="mwApg">return</span><span class="w" id="mwApk"> </span><span class="mi" id="mwApo">100</span><span class="p" id="mwAps">;</span> <span class="w" id="mwApw"> </span><span class="c1" id="mwAp0">// TODO implement API call to fetch actual live price</span> <span class="p" id="mwAp4">}</span>Syntax examples
Syntax for comments varies by programming language. There are common patterns used by multiple languages while also a wide range of syntax among the languages in general. To limit the length of this section, some examples are grouped by languages with the same or very similar syntax. Others are for particular languages that have less common syntax.
Curly brace languages
Many of the curly brace languages such as C, C++ and their many derivatives delimit a line comment with // and a block comment with /* and */. Originally, C lacked the line comment, but it was added in C99. Notable languages include: C, C++, C#, D, Java, JavaScript and Swift. For example:
<span id="mwAq8"></span><span class="cm" id="mwArA">/*</span> <span class="cm" id="mwArE"> * Check if over maximum process limit, but be sure to exclude root.</span> <span class="cm" id="mwArI"> * This is needed to make it possible for login to set per-user</span> <span class="cm" id="mwArM"> * process limit to something lower than processes root is running.</span> <span class="cm" id="mwArQ"> */</span> <span class="kt" id="mwArU">bool</span><span class="w" id="mwArY"> </span><span class="nf" id="mwArc">isOverMaximumProcessLimit</span><span class="p" id="mwArg">()</span><span class="w" id="mwArk"> </span><span class="p" id="mwAro">{</span> <span class="w" id="mwArs"> </span><span class="c1" id="mwArw">// TODO implement</span> <span class="p" id="mwAr0">}</span>Some languages, including D and Swift, allow block comments to be nested while other do not, including C and C++.
An example of nested blocks in D:
<span id="mwAso"></span><span class="c1" id="mwAss">// line comment</span> <span class="cm" id="mwAsw">/* </span> <span class="cm" id="mwAs0"> block comment</span> <span class="cm" id="mwAs4">*/</span> <span class="cm" id="mwAs8">/+ start of outer block</span> <span class="cm" id="mwAtA"> /+ inner block +/</span> <span class="cm" id="mwAtE"> end of outer block +/</span>
An example of nested blocks in Swift:
<span id="mwAtU"></span><span class="cm" id="mwAtY">/* This is the start of the outer comment.</span> <span class="cm" id="mwAtc"> /* This is the nested comment. */</span> <span class="cm" id="mwAtg">This is the end of the outer comment. */</span>
Scripting
A pattern in many scripting languages is to delimit a line comment with #. Support for a block comment varies. Notable languages include: Bash, Raku, Ruby, Perl, PowerShell, Python and R.
An example in R:
<span id="mwAuc"></span><span class="c1" id="mwAug"># This is a comment</span> <span class="nf" id="mwAuk">print</span><span class="p" id="mwAuo">(</span><span class="s" id="mwAus">"This is not a comment"</span><span class="p" id="mwAuw">)</span><span class="w" id="mwAu0"> </span><span class="c1" id="mwAu4"># This is another comment</span>
Block in Ruby
A block comment is delimited by =begin and =end that start a line. For example:
<span id="mwAvU"></span><span class="nb" id="mwAvY">puts</span><span class="w" id="mwAvc"> </span><span class="s2" id="mwAvg">"not a comment"</span> <span class="c1" id="mwAvk"># this is a comment</span> <span class="nb" id="mwAvo">puts</span><span class="w" id="mwAvs"> </span><span class="s2" id="mwAvw">"not a comment"</span> <span class="cm" id="mwAv0">=begin</span> <span class="cm" id="mwAv4">whatever goes in these lines</span> <span class="cm" id="mwAv8">is just for the human reader</span> <span class="cm" id="mwAwA">=end</span> <span class="nb" id="mwAwE">puts</span><span class="w" id="mwAwI"> </span><span class="s2" id="mwAwM">"not a comment"</span>
Block in Perl
Instead of a regular block commenting construct, Perl uses literate programming plain old documentation (POD) markup. For example:
<span id="mwAxI"></span><span class="cm" id="mwAxM">=item Pod::List-E<gt>new()</span> <span class="cm" id="mwAxQ">Create a new list object. Properties may be specified through a hash</span> <span class="cm" id="mwAxU">reference like this:</span> <span class="cm" id="mwAxY"> my $list = Pod::List->new({ -start => $., -indent => 4 });</span> <span class="cm" id="mwAxc">=cut</span> <span class="k" id="mwAxg">sub</span><span class="w" id="mwAxk"> </span><span class="nf" id="mwAxo">new</span><span class="w" id="mwAxs"> </span><span class="p" id="mwAxw">{</span> <span class="w" id="mwAx0"> </span><span class="o" id="mwAx4">...</span> <span class="p" id="mwAx8">}</span>Raku (previously called Perl 6) uses the same line comments and POD comments as Perl, but adds a configurable block comment type: "multi-line / embedded comments". It starts with #` and then an opening bracket character and ends with the matching closing bracket character. For example:
<span id="mwAy0"></span><span class="cm" id="mwAy4">#`{{ "commenting out" this version </span> <span class="cm" id="mwAy8">toggle-case(Str:D $s)</span> <span class="cm" id="mwAzA">Toggles the case of each character in a string:</span> <span class="cm" id="mwAzE"> my Str $toggled-string = toggle-case("mY NAME IS mICHAEL!");</span> <span class="cm" id="mwAzI">}}</span> <span class="k" id="mwAzM">sub</span> <span class="n" id="mwAzQ">toggle-case</span>(<span class="nb" id="mwAzU">Str:D</span> <span class="nv" id="mwAzY">$s</span>) <span class="cm" id="mwAzc">#`( this version of parens is used now )</span>{ ... }Block in PowerShell
PowerShell supports a block comment delimited by <# and #>. For example:
<span id="mwAz4"></span><span class="c" id="mwAz8"># Single line comment</span> <span class="cm" id="mwA0A"><# Multi</span> <span class="cm" id="mwA0E"> Line</span> <span class="cm" id="mwA0I"> Comment #></span>
Block in Python
Although Python does not provide for block comments a bare string literal represented by a triple-quoted string is often used for this purpose. In the examples below, the triple double-quoted strings act like comments, but are also treated as docstrings:
<span id="mwA1U"></span><span class="sd" id="mwA1Y">"""</span> <span class="sd" id="mwA1c">At the top of a file, this is the module docstring</span> <span class="sd" id="mwA1g">"""</span> <span class="k" id="mwA1k">class</span><span class="w" id="mwA1o"> </span><span class="nc" id="mwA1s">MyClass</span><span class="p" id="mwA1w">:</span> <span class="w" id="mwA10"> </span><span class="sd" id="mwA14">"""Class docstring"""</span> <span class="k" id="mwA18">def</span><span class="w" id="mwA2A"> </span><span class="nf" id="mwA2E">my_method</span><span class="p" id="mwA2I">(</span><span class="bp" id="mwA2M">self</span><span class="p" id="mwA2Q">):</span> <span class="w" id="mwA2U"> </span><span class="sd" id="mwA2Y">"""Method docstring"""</span>
Browser markup
Markup languages in general vary in comment syntax, but some of the notable internet markup formats such as HTML and XML delimit a block comment with <!-- and --> and provide no line comment support. An example in XML:
<span id="mwA3E"></span><span class="cm" id="mwA3I"><!-- select the context here --></span> <span class="nt" id="mwA3M"><param</span><span class="w" id="mwA3Q"> </span><span class="na" id="mwA3U">name=</span><span class="s" id="mwA3Y">"context"</span><span class="w" id="mwA3c"> </span><span class="na" id="mwA3g">value=</span><span class="s" id="mwA3k">"public"</span><span class="w" id="mwA3o"> </span><span class="nt" id="mwA3s">/></span>
For compatibility with SGML, double-hyphen (--) is not allowed inside comments.
ColdFusion provides syntax similar to the HTML comment, but uses three dashes instead of two. CodeFusion allows for nested block comments.
Block in Haskell
In Haskell, a block comment is delimited by {- and -}. For example:
<span id="mwA4c"></span><span class="cm" id="mwA4g">{- this is a comment</span> <span class="cm" id="mwA4k">on more lines -}</span> <span class="c1" id="mwA4o">-- and this is a comment on one line</span> <span class="nf" id="mwA4s">putStrLn</span><span class="w" id="mwA4w"> </span><span class="s" id="mwA40">"Wikipedia"</span><span class="w" id="mwA44"> </span><span class="c1" id="mwA48">-- this is another comment</span>Haskell also provides a literate programming method of commenting known as "Bird Style". Lines starting with > are interpreted as code and everything else is considered a comment. One additional requirement is a blank line before and after the code block:
<span id="mwA5k"></span>In Bird-style you have to leave a blank before the code. <span class="cs" id="mwA5o">> </span><span class="nf" id="mwA5s">fact</span><span class="w" id="mwA5w"> </span><span class="ow" id="mwA50">::</span><span class="w" id="mwA54"> </span><span class="kt" id="mwA58">Integer</span><span class="w" id="mwA6A"> </span><span class="ow" id="mwA6E">-></span><span class="w" id="mwA6I"> </span><span class="kt" id="mwA6M">Integer</span> <span class="cs" id="mwA6Q">> </span><span class="nf" id="mwA6U">fact</span><span class="w" id="mwA6Y"> </span><span class="mi" id="mwA6c">0</span><span class="w" id="mwA6g"> </span><span class="ow" id="mwA6k">=</span><span class="w" id="mwA6o"> </span><span class="mi" id="mwA6s">1</span> <span class="cs" id="mwA6w">> </span><span class="nf" id="mwA60">fact</span><span class="w" id="mwA64"> </span><span class="p" id="mwA68">(</span><span class="n" id="mwA7A">n</span><span class="o" id="mwA7E">+</span><span class="mi" id="mwA7I">1</span><span class="p" id="mwA7M">)</span><span class="w" id="mwA7Q"> </span><span class="ow" id="mwA7U">=</span><span class="w" id="mwA7Y"> </span><span class="p" id="mwA7c">(</span><span class="n" id="mwA7g">n</span><span class="o" id="mwA7k">+</span><span class="mi" id="mwA7o">1</span><span class="p" id="mwA7s">)</span><span class="w" id="mwA7w"> </span><span class="o" id="mwA70">*</span><span class="w" id="mwA74"> </span><span class="n" id="mwA78">fact</span><span class="w" id="mwA8A"> </span><span class="n" id="mwA8E">n</span> And you have to leave a blank line after the code as well.
Literate programming can also be accomplished via LaTeX. Example of a definition:
<span id="mwA8Y"></span><span class="k" id="mwA8c">\usepackage</span><span class="nb" id="mwA8g">{</span>verbatim<span class="nb" id="mwA8k">}</span> <span class="k" id="mwA8o">\newenvironment</span><span class="nb" id="mwA8s">{</span>code<span class="nb" id="mwA8w">}{</span><span class="k" id="mwA80">\verbatim</span><span class="nb" id="mwA84">}{</span><span class="k" id="mwA88">\endverbatim</span><span class="nb" id="mwA9A">}</span>Used as follows:
<span id="mwA9Q"></span><span class="c" id="mwA9U">% the LaTeX source file</span> The <span class="k" id="mwA9Y">\verb</span>|fact n| function call computes <span class="s" id="mwA9c">$</span><span class="nb" id="mwA9g">n</span><span class="o" id="mwA9k">!</span><span class="s" id="mwA9o">$</span> if <span class="s" id="mwA9s">$</span><span class="nb" id="mwA9w">n</span><span class="nv" id="mwA90">\ge</span><span class="nb" id="mwA94"> </span><span class="m" id="mwA98">0</span><span class="s" id="mwA-A">$</span>, here is a definition:<span class="k" id="mwA-E">\\</span> <span class="k" id="mwA-I">\begin</span><span class="nb" id="mwA-M">{</span>code<span class="nb" id="mwA-Q">}</span> <span class="nf" id="mwA-U">fact</span><span class="w" id="mwA-Y"> </span><span class="ow" id="mwA-c">::</span><span class="w" id="mwA-g"> </span><span class="kt" id="mwA-k">Integer</span><span class="w" id="mwA-o"> </span><span class="ow" id="mwA-s">-></span><span class="w" id="mwA-w"> </span><span class="kt" id="mwA-0">Integer</span> <span class="nf" id="mwA-4">fact</span><span class="w" id="mwA-8"> </span><span class="mi" id="mwA_A">0</span><span class="w" id="mwA_E"> </span><span class="ow" id="mwA_I">=</span><span class="w" id="mwA_M"> </span><span class="mi" id="mwA_Q">1</span> <span class="nf" id="mwA_U">fact</span><span class="w" id="mwA_Y"> </span><span class="p" id="mwA_c">(</span><span class="n" id="mwA_g">n</span><span class="o" id="mwA_k">+</span><span class="mi" id="mwA_o">1</span><span class="p" id="mwA_s">)</span><span class="w" id="mwA_w"> </span><span class="ow" id="mwA_0">=</span><span class="w" id="mwA_4"> </span><span class="p" id="mwA_8">(</span><span class="n" id="mwBAA">n</span><span class="o" id="mwBAE">+</span><span class="mi" id="mwBAI">1</span><span class="p" id="mwBAM">)</span><span class="w" id="mwBAQ"> </span><span class="o" id="mwBAU">*</span><span class="w" id="mwBAY"> </span><span class="n" id="mwBAc">fact</span><span class="w" id="mwBAg"> </span><span class="n" id="mwBAk">n</span> <span class="k" id="mwBAo">\end</span><span class="nb" id="mwBAs">{</span>code<span class="nb" id="mwBAw">}</span> Here more explanation using <span class="k" id="mwBA0">\LaTeX</span><span class="nb" id="mwBA4">{}</span> markupBlock in Lua
Lua supports block comments delimited by --[[ and ]] For example:
<span id="mwBBk"></span><span class="cm" id="mwBBo">--[[A multi-line</span> <span class="cm" id="mwBBs">long comment</span> <span class="cm" id="mwBBw">]]</span>
Block in SQL
In some variants of SQL, the curly brace language block comment (/**/) is supported. Variants include: Transact-SQL, MySQL, SQLite, PostgreSQL, and Oracle.
MySQL also supports a line comment delimited by #.
Less common syntax
APL
APL uses ⍝ ("lamp") for a line comment. For example:
<span id="mwBEI"></span><span class="c1" id="mwBEM">⍝ Now add the numbers:</span> <span class="nv" id="mwBEQ">c</span><span class="kd" id="mwBEU">←</span><span class="nv" id="mwBEY">a</span><span class="o" id="mwBEc">+</span><span class="nv" id="mwBEg">b</span><span class="w" id="mwBEk"> </span><span class="c1" id="mwBEo">⍝ addition</span>
In dialects that have the ⊣ ("left") and ⊢ ("right") primitives, comments can often be inside or separate statements, in the form of ignored strings:
<span id="mwBFE"></span><span class="nv" id="mwBFI">d</span><span class="kd" id="mwBFM">←</span><span class="m" id="mwBFQ">2</span><span class="o" id="mwBFU">×</span><span class="nv" id="mwBFY">c</span><span class="w" id="mwBFc"> </span><span class="o" id="mwBFg">⊣</span><span class="s1" id="mwBFk">'where'</span><span class="o" id="mwBFo">⊢</span><span class="w" id="mwBFs"> </span><span class="nv" id="mwBFw">c</span><span class="kd" id="mwBF0">←</span><span class="nv" id="mwBF4">a</span><span class="o" id="mwBF8">+</span><span class="w" id="mwBGA"> </span><span class="s1" id="mwBGE">'bound'</span><span class="o" id="mwBGI">⊢</span><span class="w" id="mwBGM"> </span><span class="nv" id="mwBGQ">b</span>
AppleScript
AppleScript supports both line and block comments. For example:
<span id="mwBGo"></span><span class="c" id="mwBGs"># line comment (in later versions)</span> <span class="cm" id="mwBGw">(*</span> <span class="cm" id="mwBG0">This program displays a greeting.</span> <span class="cm" id="mwBG4">*)</span> <span class="k" id="mwBG8">on</span> <span class="nv" id="mwBHA">greet</span><span class="p" id="mwBHE">(</span><span class="nv" id="mwBHI">myGreeting</span><span class="p" id="mwBHM">)</span> <span class="nb" id="mwBHQ">display dialog</span> <span class="nv" id="mwBHU">myGreeting</span> <span class="o" id="mwBHY">&</span> <span class="s2" id="mwBHc">" world!"</span> <span class="k" id="mwBHg">end</span> <span class="nv" id="mwBHk">greet</span> <span class="c" id="mwBHo">-- Show the greeting</span> <span class="nv" id="mwBHs">greet</span><span class="p" id="mwBHw">(</span><span class="s2" id="mwBH0">"Hello"</span><span class="p" id="mwBH4">)</span>
BASIC
Early versions of BASIC used REM (short for remark) for a line comment.
<span id="mwBIU"></span><span class="nl" id="mwBIY">10</span><span class="w" id="mwBIc"> </span><span class="c1" id="mwBIg">REM This BASIC program shows the use of the PRINT and GOTO Statements.</span> <span class="nl" id="mwBIk">15</span><span class="w" id="mwBIo"> </span><span class="c1" id="mwBIs">REM It fills the screen with the phrase "HELLO"</span> <span class="nl" id="mwBIw">20</span><span class="w" id="mwBI0"> </span><span class="kr" id="mwBI4">PRINT</span><span class="w" id="mwBI8"> </span><span class="s2" id="mwBJA">"HELLO"</span> <span class="nl" id="mwBJE">30</span><span class="w" id="mwBJI"> </span><span class="kr" id="mwBJM">GOTO</span><span class="w" id="mwBJQ"> </span><span class="nl" id="mwBJU">20</span>
In later variations, including Quick Basic, Q Basic, Visual Basic (VB), VB.NET, VBScript, FreeBASIC and Gambas, a line comment is delimited with ' (apostrophe). An example in VB.NET:
<span id="mwBKE"></span><span class="k" id="mwBKI">Public</span><span class="w" id="mwBKM"> </span><span class="k" id="mwBKQ">Class</span><span class="w" id="mwBKU"> </span><span class="nc" id="mwBKY">Form1</span> <span class="w" id="mwBKc"> </span><span class="k" id="mwBKg">Private</span><span class="w" id="mwBKk"> </span><span class="k" id="mwBKo">Sub</span><span class="w" id="mwBKs"> </span><span class="nf" id="mwBKw">Button1_Click</span><span class="p" id="mwBK0">(</span><span class="n" id="mwBK4">sender</span><span class="w" id="mwBK8"> </span><span class="ow" id="mwBLA">As</span><span class="w" id="mwBLE"> </span><span class="kt" id="mwBLI">Object</span><span class="p" id="mwBLM">,</span><span class="w" id="mwBLQ"> </span><span class="n" id="mwBLU">e</span><span class="w" id="mwBLY"> </span><span class="ow" id="mwBLc">As</span><span class="w" id="mwBLg"> </span><span class="n" id="mwBLk">EventArgs</span><span class="p" id="mwBLo">)</span><span class="w" id="mwBLs"> </span><span class="k" id="mwBLw">Handles</span><span class="w" id="mwBL0"> </span><span class="n" id="mwBL4">Button1</span><span class="p" id="mwBL8">.</span><span class="n" id="mwBMA">Click</span> <span class="w" id="mwBME"> </span><span class="c" id="mwBMI">' new style line comment</span> <span class="w" id="mwBMM"> </span><span class="c" id="mwBMQ">rem old style line comment still supported</span> <span class="w" id="mwBMU"> </span><span class="n" id="mwBMY">MessageBox</span><span class="p" id="mwBMc">.</span><span class="n" id="mwBMg">Show</span><span class="p" id="mwBMk">(</span><span class="s" id="mwBMo">"Hello, World"</span><span class="p" id="mwBMs">)</span><span class="w" id="mwBMw"> </span><span class="c" id="mwBM0">' show dialog with a greeting</span> <span class="w" id="mwBM4"> </span><span class="k" id="mwBM8">End</span><span class="w" id="mwBNA"> </span><span class="k" id="mwBNE">Sub</span> <span class="k" id="mwBNI">End</span><span class="w" id="mwBNM"> </span><span class="k" id="mwBNQ">Class</span>
Cisco IOS and IOS-XE configuration
The exclamation point (!) may be used to mark comments in a Cisco router's configuration mode, however such comments are not saved to non-volatile memory (which contains the startup-config), nor are they displayed by the "show run" command.
It is possible to insert human-readable content that is actually part of the configuration, and may be saved to the NVRAM startup-config via:
- The "description" command, used to add a description to the configuration of an interface or of a BGP neighbor
- The "name" parameter, to add a remark to a static route
- The "remark" command in access lists
<span id="mwBO0"></span>! Paste the text below to reroute traffic manually config t int gi0/2 no shut ip route 0.0.0.0 0.0.0.0 gi0/2 name ISP2 no ip route 0.0.0.0 0.0.0.0 gi0/1 name ISP1 int gi0/1 shut exit
Fortran
The following fixed-form Fortran code fragment shows that comment syntax is column-oriented. A letter C in the first column causes the entire line to be treated as a comment. In Fortran 77, an asterisk in column 1 also indicates a comment.
<span id="mwBPU"></span><span class="c" id="mwBPY">C</span> <span class="c" id="mwBPc">C Lines beginning with 'C' in the first (a.k.a. comment) column are comments</span> <span class="c" id="mwBPg">C</span> <span class="nl" id="mwBPk"> </span> <span class="k" id="mwBPo">WRITE</span><span class="w" id="mwBPs"> </span><span class="p" id="mwBPw">(</span><span class="mi" id="mwBP0">6</span><span class="p" id="mwBP4">,</span><span class="mi" id="mwBP8">610</span><span class="p" id="mwBQA">)</span> <span class="nl" id="mwBQE"> 610</span> <span class="k" id="mwBQI">FORMAT</span><span class="p" id="mwBQM">(</span><span class="mi" id="mwBQQ">12</span><span class="n" id="mwBQU">H</span><span class="w" id="mwBQY"> </span><span class="n" id="mwBQc">HELLO</span><span class="w" id="mwBQg"> </span><span class="n" id="mwBQk">WORLD</span><span class="p" id="mwBQo">)</span> <span class="nl" id="mwBQs"> </span> <span class="k" id="mwBQw">END</span>
The following Fortran 90 code fragment shows a more modern line comment syntax: text following !.
<span id="mwBRI"></span><span class="c" id="mwBRM">! A comment</span> <span class="k" id="mwBRQ">program </span><span class="n" id="mwBRU">comment_test</span> <span class="w" id="mwBRY"> </span><span class="k" id="mwBRc">print</span><span class="w" id="mwBRg"> </span><span class="s1" id="mwBRk">'(A)'</span><span class="p" id="mwBRo">,</span><span class="w" id="mwBRs"> </span><span class="s1" id="mwBRw">'Hello world'</span><span class="w" id="mwBR0"> </span><span class="c" id="mwBR4">! also a comment</span> <span class="k" id="mwBR8">end program</span>
Free-form Fortran, also introduced with Fortran 90, only supports this latter style of comment.
Although not a part of the Fortran Standard, many Fortran compilers offer an optional C-like preprocessor pass. This can be used to provide block comments:
<span id="mwBSU"></span><span class="cp" id="mwBSY">#if 0</span> <span class="w" id="mwBSc"> </span><span class="n" id="mwBSg">This</span><span class="w" id="mwBSk"> </span><span class="k" id="mwBSo">is </span><span class="n" id="mwBSs">a</span><span class="w" id="mwBSw"> </span><span class="k" id="mwBS0">block </span><span class="n" id="mwBS4">comment</span><span class="w" id="mwBS8"> </span><span class="n" id="mwBTA">spanning</span> <span class="w" id="mwBTE"> </span><span class="n" id="mwBTI">multiple</span><span class="w" id="mwBTM"> </span><span class="n" id="mwBTQ">lines</span><span class="p" id="mwBTU">.</span> <span class="cp" id="mwBTY">#endif</span> <span class="k" id="mwBTc">program </span><span class="n" id="mwBTg">comment_test</span> <span class="w" id="mwBTk"> </span><span class="k" id="mwBTo">print</span><span class="w" id="mwBTs"> </span><span class="s1" id="mwBTw">'(A)'</span><span class="p" id="mwBT0">,</span><span class="w" id="mwBT4"> </span><span class="s1" id="mwBT8">'Hello world'</span><span class="w" id="mwBUA"> </span><span class="c" id="mwBUE">! also a comment</span> <span class="k" id="mwBUI">end program</span>
MATLAB
In MATLAB's programming language, the '%' character indicates a single-line comment. Multi line comments are also available via %{ and %} brackets and can be nested, e.g.
<span id="mwBUo"></span><span class="c" id="mwBUs">% These are the derivatives for each term</span> <span class="n" id="mwBUw">d</span><span class="w" id="mwBU0"> </span><span class="p" id="mwBU4">=</span><span class="w" id="mwBU8"> </span><span class="p" id="mwBVA">[</span><span class="mi" id="mwBVE">0</span><span class="w" id="mwBVI"> </span><span class="o" id="mwBVM">-</span><span class="mi" id="mwBVQ">1</span><span class="w" id="mwBVU"> </span><span class="mi" id="mwBVY">0</span><span class="p" id="mwBVc">];</span> <span class="cm" id="mwBVg">%{</span> <span class="cm" id="mwBVk"> %{</span> <span class="cm" id="mwBVo"> (Example of a nested comment, indentation is for cosmetics (and ignored).)</span> <span class="cm" id="mwBVs"> %}</span> <span class="w" id="mwBVw"> </span><span class="n" id="mwBV0">We</span><span class="w" id="mwBV4"> </span><span class="n" id="mwBV8">form</span><span class="w" id="mwBWA"> </span><span class="n" id="mwBWE">the</span><span class="w" id="mwBWI"> </span><span class="n" id="mwBWM">sequence</span><span class="p" id="mwBWQ">,</span><span class="w" id="mwBWU"> </span><span class="n" id="mwBWY">following</span><span class="w" id="mwBWc"> </span><span class="n" id="mwBWg">the</span><span class="w" id="mwBWk"> </span><span class="n" id="mwBWo">Taylor</span><span class="w" id="mwBWs"> </span><span class="n" id="mwBWw">formula</span><span class="p" id="mwBW0">.</span> <span class="w" id="mwBW4"> </span><span class="n" id="mwBW8">Note</span><span class="w" id="mwBXA"> </span><span class="n" id="mwBXE">that</span><span class="w" id="mwBXI"> </span><span class="n" id="mwBXM">we</span><span class="o" id="mwBXQ">'</span><span class="n" id="mwBXU">re</span><span class="w" id="mwBXY"> </span><span class="n" id="mwBXc">operating</span><span class="w" id="mwBXg"> </span><span class="n" id="mwBXk">on</span><span class="w" id="mwBXo"> </span><span class="n" id="mwBXs">a</span><span class="w" id="mwBXw"> </span><span class="n" id="mwBX0">vector</span><span class="p" id="mwBX4">.</span> <span class="c" id="mwBX8">%}</span> <span class="n" id="mwBYA">seq</span><span class="w" id="mwBYE"> </span><span class="p" id="mwBYI">=</span><span class="w" id="mwBYM"> </span><span class="n" id="mwBYQ">d</span><span class="w" id="mwBYU"> </span><span class="o" id="mwBYY">.*</span><span class="w" id="mwBYc"> </span><span class="p" id="mwBYg">(</span><span class="n" id="mwBYk">x</span><span class="w" id="mwBYo"> </span><span class="o" id="mwBYs">-</span><span class="w" id="mwBYw"> </span><span class="n" id="mwBY0">c</span><span class="p" id="mwBY4">)</span><span class="o" id="mwBY8">.^</span><span class="n" id="mwBZA">n</span><span class="w" id="mwBZE"> </span><span class="o" id="mwBZI">./</span><span class="p" id="mwBZM">(</span><span class="nb" id="mwBZQ">factorial</span><span class="p" id="mwBZU">(</span><span class="n" id="mwBZY">n</span><span class="p" id="mwBZc">))</span> <span class="c" id="mwBZg">% We add-up to get the Taylor approximation</span> <span class="n" id="mwBZk">approx</span><span class="w" id="mwBZo"> </span><span class="p" id="mwBZs">=</span><span class="w" id="mwBZw"> </span><span class="nb" id="mwBZ0">sum</span><span class="p" id="mwBZ4">(</span><span class="n" id="mwBZ8">seq</span><span class="p" id="mwBaA">)</span>Nim
Nim delimits a line comment with # and block comments with #[ and ]#. Block comments can be nested.
Nim also has documentation comments that use mixed Markdown and ReStructuredText markups. A line documentation comment uses '##' and a block documentation comment uses '##[' and ']##'. The compiler can generate HTML, LaTeX and JSON documentation from the documentation comments. Documentation comments are part of the abstract syntax tree and can be extracted using macros.
<span id="mwBbQ"></span><span class="sd" id="mwBbU">## Documentation of the module *ReSTructuredText* and **MarkDown**</span> <span class="c" id="mwBbY"># This is a comment, but it is not a documentation comment.</span> <span class="k" id="mwBbc">type</span><span class="w" id="mwBbg"> </span><span class="n" id="mwBbk">Kitten</span><span class="w" id="mwBbo"> </span><span class="o" id="mwBbs">=</span><span class="w" id="mwBbw"> </span><span class="k" id="mwBb0">object</span><span class="w" id="mwBb4"> </span><span class="sd" id="mwBb8">## Documentation of type</span> <span class="w" id="mwBcA"> </span><span class="n" id="mwBcE">age</span><span class="p" id="mwBcI">:</span><span class="w" id="mwBcM"> </span><span class="nb" id="mwBcQ">int</span><span class="w" id="mwBcU"> </span><span class="sd" id="mwBcY">## Documentation of field</span> <span class="k" id="mwBcc">proc</span><span class="w" id="mwBcg"> </span><span class="nf" id="mwBck">purr</span><span class="p" id="mwBco">(</span><span class="n" id="mwBcs">self</span><span class="p" id="mwBcw">:</span><span class="w" id="mwBc0"> </span><span class="n" id="mwBc4">Kitten</span><span class="p" id="mwBc8">)</span><span class="w" id="mwBdA"> </span><span class="o" id="mwBdE">=</span> <span class="w" id="mwBdI"> </span><span class="sd" id="mwBdM">## Documentation of function</span> <span class="w" id="mwBdQ"> </span><span class="n" id="mwBdU">echo</span><span class="w" id="mwBdY"> </span><span class="s" id="mwBdc">"Purr Purr"</span><span class="w" id="mwBdg"> </span><span class="c" id="mwBdk"># This is a comment, but it is not a documentation comment.</span> <span class="c" id="mwBdo"># This is a comment, but it is not a documentation comment.</span>
OCaml
OCaml supports nestable comments. For example:
<span id="mwBeA"></span><span class="n" id="mwBeE">codeLine</span><span class="c" id="mwBeI">(* comment level 1(*comment level 2*)*)</span>
Pascal, Delphi
In Pascal and Delphi, a block comment is delimited by { and }, and as an alternative for computers that do not support these characters, (* and *) are also supported. A line comment is delimited by \\. In Niklaus Wirth's more modern family of languages (including Modula-2 and Oberon), comments are delimited by (* and *). Comments can be nested. For example:
<span id="mwBf8"></span><span class="cm" id="mwBgA">(* test diagonals *)</span> <span class="n" id="mwBgE">columnDifference</span><span class="w" id="mwBgI"> </span><span class="o" id="mwBgM">:=</span><span class="w" id="mwBgQ"> </span><span class="n" id="mwBgU">testColumn</span><span class="w" id="mwBgY"> </span><span class="o" id="mwBgc">-</span><span class="w" id="mwBgg"> </span><span class="n" id="mwBgk">column</span><span class="o" id="mwBgo">;</span> <span class="k" id="mwBgs">if</span><span class="w" id="mwBgw"> </span><span class="p" id="mwBg0">(</span><span class="n" id="mwBg4">row</span><span class="w" id="mwBg8"> </span><span class="o" id="mwBhA">+</span><span class="w" id="mwBhE"> </span><span class="n" id="mwBhI">columnDifference</span><span class="w" id="mwBhM"> </span><span class="o" id="mwBhQ">=</span><span class="w" id="mwBhU"> </span><span class="n" id="mwBhY">testRow</span><span class="p" id="mwBhc">)</span><span class="w" id="mwBhg"> </span><span class="k" id="mwBhk">or</span> <span class="w" id="mwBho"> </span><span class="o" id="mwBhs">.......</span>
PHP
Comments in PHP can be either curly brace style (both line and block), or line delimited with #l. Blocks cannot be nested. Starting in PHP 8, a # only means comment if it's not immediately followed by [. Otherwise, it delimits an attribute, which continues till the next ]. For example:
<span id="mwBiU"></span><span class="sd" id="mwBiY">/**</span> <span class="sd" id="mwBic"> * This class contains a sample documentation.</span> <span class="sd" id="mwBig"> * @author Unknown</span> <span class="sd" id="mwBik"> */</span> <span class="p" id="mwBio">#[</span><span class="nd" id="mwBis">Attribute</span><span class="p" id="mwBiw">]</span> <span class="k" id="mwBi0">class</span> <span class="nc" id="mwBi4">MyAttribute</span> <span class="p" id="mwBi8">{</span> <span class="k" id="mwBjA">const</span> <span class="no" id="mwBjE">VALUE</span> <span class="o" id="mwBjI">=</span> <span class="s1" id="mwBjM">'value'</span><span class="p" id="mwBjQ">;</span> <span class="c1" id="mwBjU">// C++ style line comment</span> <span class="k" id="mwBjY">private</span> <span class="nv" id="mwBjc">$value</span><span class="p" id="mwBjg">;</span> <span class="c1" id="mwBjk"># script style line comment</span> <span class="k" id="mwBjo">public</span> <span class="k" id="mwBjs">function</span> <span class="fm" id="mwBjw">__construct</span><span class="p" id="mwBj0">(</span><span class="nv" id="mwBj4">$value</span> <span class="o" id="mwBj8">=</span> <span class="k" id="mwBkA">null</span><span class="p" id="mwBkE">)</span> <span class="p" id="mwBkI">{</span> <span class="nv" id="mwBkM">$this</span><span class="o" id="mwBkQ">-></span><span class="na" id="mwBkU">value</span> <span class="o" id="mwBkY">=</span> <span class="nv" id="mwBkc">$value</span><span class="p" id="mwBkg">;</span> <span class="p" id="mwBkk">}</span> <span class="p" id="mwBko">}</span>Double dash
A relatively loose collection of languages use -- for a single line comment. Notable languages include: Ada, Eiffel, Haskell, Lua, SQL and VHDL. Block comment support varies. An example in Ada:
<span id="mwBlY"></span><span class="c1" id="mwBlc">-- the air traffic controller task takes requests for takeoff and landing</span> <span class="kd" id="mwBlg">task</span> <span class="kd" id="mwBlk">type</span> <span class="kt" id="mwBlo">Controller</span> <span class="p" id="mwBls">(</span><span class="nv" id="mwBlw">My_Runway</span><span class="p" id="mwBl0">: </span><span class="nv" id="mwBl4">Runway_Access</span><span class="p" id="mwBl8">)</span> <span class="kr" id="mwBmA">is</span> <span class="c1" id="mwBmE">-- task entries for synchronous message passing</span> <span class="kd" id="mwBmI">entry</span> <span class="nf" id="mwBmM">Request_Takeoff</span> <span class="p" id="mwBmQ">(</span><span class="nv" id="mwBmU">ID</span><span class="p" id="mwBmY">: </span><span class="nv" id="mwBmc">in</span> <span class="nv" id="mwBmg">Airplane_ID</span><span class="p" id="mwBmk">;</span> <span class="nv" id="mwBmo">Takeoff</span><span class="p" id="mwBms">: </span><span class="nv" id="mwBmw">out</span> <span class="nv" id="mwBm0">Runway_Access</span><span class="p" id="mwBm4">);</span> <span class="kd" id="mwBm8">entry</span> <span class="nf" id="mwBnA">Request_Approach</span><span class="p" id="mwBnE">(</span><span class="nv" id="mwBnI">ID</span><span class="p" id="mwBnM">: </span><span class="nv" id="mwBnQ">in</span> <span class="nv" id="mwBnU">Airplane_ID</span><span class="p" id="mwBnY">;</span> <span class="nv" id="mwBnc">Approach</span><span class="p" id="mwBng">: </span><span class="nv" id="mwBnk">out</span> <span class="nv" id="mwBno">Runway_Access</span><span class="p" id="mwBns">);</span> <span class="kr" id="mwBnw">end</span> <span class="nf" id="mwBn0">Controller</span><span class="p" id="mwBn4">;</span>
Security issues
In interpreted languages, depending on the operating system's capabilities and the administrator's choices, the entire source code, including the comments, may be viewable to the program's end user. This may present a security vulnerability, when comments contain secrets or when secret sections of code are merely commented out.

