Comment (computer programming): Difference between revisions

Content deleted Content added
Clarify how comments are ignored by a translator yet are parsed by other tools
 
(18 intermediate revisions by 10 users not shown)
Line 26:
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''' blockis or[[delimiter#Bracket sequencedelimiters|delimited]] ofwith linetext commentsthat located nearmarks the topstart ofand anend associatedof programmingcomment topic,text. suchIt ascan beforespan amultiple symbol declarationlines or atoccupy theany toppart of a file,line. isSome calledlanguages aallow ''prologueblock comment''.comments Ato commentbe thatrecursively isnested on onlyinside one lineanother, usuallybut afterothers programdo code, is called an ''inline comment''not.<ref name="JBDixit000Higham000">{{cite book | last = Dixit | first = J.B. | title = Computer Fundamentals and Programming in C | publisher = Laxmi Publications | year = 2003 | isbn = 978-81-7008-882-0 }}</ref>
 
A '''block comment''' is [[delimiter#Bracket delimiters|delimited]] with text that marks the start and end of the block. It can span multiple lines or occupy any part of a line. Some languages (such as [[MATLAB]]) allow block comments to be recursively nested inside one another, but others (such as [[Java (programming language)|Java]]) do not.<ref name="Higham000">{{cite book
| title = MATLAB Guide
| first = Desmond
Line 48 ⟶ 46:
| access-date = 2007-07-24
| date = 2000-03-04
Most}}</ref> 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.<ref name="javadude000" /> Many languages support both block and line comments {{endash}} using different delimiters for each. For example, [[C (programming language)|C]], [[C++]] and their many derivatives support block comments delimited by <code>/*</code> and <code>*/</code> and line comments delimited by <code>//</code>. Other languages support only one type of comment.<ref name="javadude000" />
}}</ref>
 
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 is refers.<ref name="JBDixit000">{{cite book | last = Dixit | first = J.B. | title = Computer Fundamentals and Programming in C | publisher = Laxmi Publications | year = 2003 | isbn = 978-81-7008-882-0 }}</ref> Both prologue and inline comments can be represented as either line or block comments. For example:
In modern languages, a '''line comment''' starts with a delimiter and continues until the end of the line. Some older languages designated a column at which subsequent text is considered comment text.<ref name="javadude000" />
 
<syntaxhighlight lang="c">
Most modern languages support both block and line comments {{endash}} using different delimiters for each. For example, [[C (programming language)|C]], [[C++]] and their many derivatives support block comments delimited by <code>/*</code> and <code>*/</code> and line comments delimited by <code>//</code>. Other languages support only one type of comment.<ref name="javadude000" />
/*
* prologue block comment; if is about foo()
*/
bool foo() {
return true; /* inline block comment; if is about this return */
}
 
//
// prologue line comment; if is about bar()
//
bool bar() {
return false; // inline line comment; if is about this return
}
</syntaxhighlight>
 
==Examples of use==
Line 114 ⟶ 126:
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]].
 
Some [[programming tools]] write metadata into the code as comments.<ref>See e.g., {{cite book |last=Wynne-Powell |first=Rod |year=2008 |title=Mac OS X for Photographers: Optimized Image Workflow for the Mac User |page=243 |publisher=Focal Press |___location=Oxford |isbn=978-0-240-52027-8 |url=https://archive.org/details/macosxforphotogr0000wynn}}</ref> For example, a [[version control]] tool might write metadata such as author, date and version number into each file when it's commitedcommitted to the repository.<ref>See e.g., {{cite book |last=Berlin |first=Daniel |year=2006 |title=Practical Subversion, Second Edition |page=168 |publisher=APress |___location=Berkeley |isbn=978-1-59059-753-8}}</ref>
 
=== Integrate with development tools ===
Line 133 ⟶ 145:
| publisher=Cambridge University Press
| isbn=978-1-397-80521-8
}}</ref> Examples include [[Javadoc]], [[Ddoc]], [[Doxygen]], [[Visual Expert]] and [[PHPDoc]]. Forms of [[docstring]] are supported by [[Python (programming language)|Python]], [[Lisp (programming language)|Lisp]], [[Elixir (programming language)|Elixir]], and [[Clojure]].<ref>[https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/defn Function definition with docstring in Clojure]</ref> [[C Sharp (programming language)|C#]], [[F Sharp (programming language)|F#]] and [[Visual Basic .NET]] implement a similar feature called "XML Comments" which are read by [[IntelliSense]] from the compiled [[.NET]] assembly.<ref>{{cite book|last=Murach|title=C# 2005|page=56}}</ref>
 
=== Visualization ===
Line 181 ⟶ 193:
=== Extend language syntax ===
 
Occasionally, code that is formatted as a comment is overloaded to convey additional information to the translator, such as [[conditional comment]]scomments. 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]].<ref>[[c2: HotComments]]</ref>
 
Other examples include interpreter [[Directive (programming)|directives]]:
Line 280 ⟶ 292:
 
<syntaxhighlight lang="c">
/***************************\**
* *
* This is the comment body. *
* *
\ *****************************/
</syntaxhighlight>
 
Line 321 ⟶ 333:
 
==Tags==
Programmers often use one of select words {{endash}} also konwnknown as [[tag (metadata)|tags]], codetags<ref>[https://www.python.org/dev/peps/pep-0350/#what-are-codetags "PEP 0350 – Codetags"], Python Software Foundation</ref><ref>[https://medium.com/@eido.askayo/never-forget-anything-before-after-and-while-coding-98d187ae4cf1 "Never Forget Anything Before, After and While Coding"], Using "codetag" comments as productive remainders</ref> and tokens<ref>[https://msdn.microsoft.com/en-us/library/txtwdysk.aspx#tokenscomments "Using the Task List"], msdn.microsoft.com</ref> {{endash}} to categorize the information in a comment. Programmers may leverage these tags by searching for them via a [[text editor]] or [[grep]]. Some editors [[Syntax highlighting|highlight]] comment text based on tags.
 
Commonly used tags include:
Line 342 ⟶ 354:
 
===Curly brace languages===
Many of the [[curly brace language]]s such as C, C++ and their many derivatives delimit a line comment with {{code|//}} and a block comment with {{code|/*}} and {{code|*/}}. Originally, C lacked the line comment, but it was added in [[C99]]. Notable languages include: C, C++, [[C# (programming language)|C#]], [[D (programming language)|D]], [[Java (programming language)|Java]], [[JavascriptJavaScript]] and [[Swift (programming language)|Swift]]. For example:
 
<syntaxhighlight lang="c">
Line 562 ⟶ 574:
====APL====
 
[[APL (programming language)|APL]] uses <code>⍝</code> ("lamp") for a line comment. For example:
 
<syntaxhighlight lang="apl">
Line 578 ⟶ 590:
 
<syntaxhighlight lang="AppleScript">
# line comment (in later versions)
(*
This program displays a greeting.
Line 632 ⟶ 645:
 
====Fortran====
The following fixed-form [[Fortran IV]] code fragment shows that comment syntax is column-oriented. A letter <code>C</code> 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.
 
<syntaxhighlight lang="fortranfixed">
Line 647 ⟶ 660:
<syntaxhighlight lang="Fortran">
! A comment
program comment_test
print '(A)', 'Hello world' ! also a comment
end program
</syntaxhighlight>
 
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:
 
<syntaxhighlight lang="Fortran">
#if 0
This is a block comment spanning
multiple lines.
#endif
program comment_test
print '(A)', 'Hello world' ! also a comment
Line 695 ⟶ 722:
 
====OCaml====
[[OCaml]] supports nestable comments. For exmapleexample:
<syntaxhighlight lang="ocaml">
codeLine(* comment level 1(*comment level 2*)*)
Line 702 ⟶ 729:
====Pascal, Delphi====
 
AsIn [[Pascal (programming language)|Pascal]] and [[Delphi (software)|Delphi]], a block comment is delimited by <code>{</code> and <code>}</code>, and as an alternative, for computers that do not support these characters, '<code>(*&nbsp;...&nbsp;</code> and <code>*)'</code> are allowedalso supported. A line comment is delimited by <code>\\</code>.<ref>{{cite book|author=Kathleen Jensen, Niklaus Wirth|year=1985|title=Pascal User Manual and Report|publisher=Springer-Verlag|isbn=0-387-96048-1}}</ref> In [[Niklaus Wirth]]'s more modern family of languages (including [[Modula-2]] and [[Oberon (programming language)|Oberon]]), comments are delimited by '<code>(*&nbsp;...&nbsp;</code> and <code>*)'</code>.<ref>{{cite book|author=Niklaus Wirth|year=1983|title=Programming in Modula-2|publisher=Springer-Verlag|isbn=0-387-15078-1}}</ref><ref>*{{cite book|author=Martin Reiser, Niklaus Wirth|year=1992|title=Programming in Oberon|publisher=Addison-Wesley|isbn=0-201-56543-9}}</ref> Comments can be nested. // can be included in a {} and {} can be included in a (**). For example:
In [[Pascal (programming language)|Pascal]] and [[Delphi (software)|Delphi]], comments are delimited by '{&nbsp;...&nbsp;}'. Comment lines can also start with '\\' .
As an alternative, for computers that do not support these characters, '(*&nbsp;...&nbsp;*)' are allowed.<ref>{{cite book|author=Kathleen Jensen, Niklaus Wirth|year=1985|title=Pascal User Manual and Report|publisher=Springer-Verlag|isbn=0-387-96048-1}}</ref> In [[Niklaus Wirth]]'s more modern family of languages (including [[Modula-2]] and [[Oberon (programming language)|Oberon]]), comments are delimited by '(*&nbsp;...&nbsp;*)'.<ref>{{cite book|author=Niklaus Wirth|year=1983|title=Programming in Modula-2|publisher=Springer-Verlag|isbn=0-387-15078-1}}</ref><ref>*{{cite book|author=Martin Reiser, Niklaus Wirth|year=1992|title=Programming in Oberon|publisher=Addison-Wesley|isbn=0-201-56543-9}}</ref> Comments can be nested. // can be included in a {} and {} can be included in a (**). For example:
 
<syntaxhighlight lang="pascal">