Comment (computer programming): Difference between revisions

Content deleted Content added
Examples: Limit examples by grouping by similar syntax
Examples: Group scripting languages
Line 346:
 
====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]]. The list ofNotable languages that use this syntax is long but a few of the more notable include: C, C++, [[C# (programming language)|C#]], [[Java (programming language)|Java]], and [[Javascript]]. For example:
 
<syntaxhighlight lang="c">
Line 359:
</syntaxhighlight>
 
====RScripting====
 
A pattern in many [[scripting language]]s is to delimit a line comment with <code>#</code> and provide no support for a block comment. Notable languages include: [[Bash (Unix shell)|Bash]], [[Python (programming language)|Python]] and [[R (programming language)|R]].
 
An example in R:
 
<syntaxhighlight lang="R">
# This is a comment
print("This is not a comment") # This is another comment
</syntaxhighlight>
 
=====In Python=====
 
Although Python does not provide for block comments<ref name=triple>{{cite web |url=https://www.tutorialdocs.com/tutorial/python3/python3-basic-syntax.html |title=Python 3 Basic Syntax |access-date=25 February 2019 |quote=Triple quotes are treated as regular strings with the exception that they can span multiple lines. By regular strings I mean that if they are not assigned to a variable they will be immediately garbage collected as soon as that code executes. hence are not ignored by the interpreter in the same way that #a comment is. |archive-date=19 August 2021 |archive-url=https://web.archive.org/web/20210819164828/https://www.tutorialdocs.com/tutorial/python3/python3-basic-syntax.html |url-status=dead }}</ref> but a bare [[string literal]] represented by a triple-quoted string can be andis often is used for this purpose.<ref>[https://twitter.com/gvanrossum/status/112670605505077248 "Python tip: You can use multi-line strings as multi-line comments"], 11 September 2011, Guido van Rossum</ref><ref name=triple/> In the examples below, the triple double-quoted strings act like comments, but are also treated as [[docstring]]s:
 
<syntaxhighlight lang="python">
"""
At the top of a file, this is the module docstring
"""
 
class MyClass:
"""The class'sClass docstring"""
 
def my_method(self):
"""The method'sMethod docstring"""
</syntaxhighlight>
===By language===
 
Line 643 ⟶ 669:
</syntaxhighlight>
 
Instead of a regular block commenting construct, Perl uses [[Plainliterate Old Documentationprogramming]], a[[Plain markupOld languageDocumentation|plain forold [[literatedocumentation programming(POD)]], markup.<ref>
{{cite web
| title = perlpod – the Plain Old Documentation format
| url = http://perldoc.perl.org/perlpod.html|access-date=2011-09-12
}}
</ref> forFor instanceexample:<ref>
{{cite web
| title = Pod::ParseUtils – helpers for POD parsing and conversion
Line 674 ⟶ 700:
return $self;
}
</syntaxhighlight>
 
====R====
[[R (programming language)|R]] only supports inline comments started by the hash (#) character.
<syntaxhighlight lang="R">
# This is a comment
print("This is not a comment") # This is another comment
</syntaxhighlight>
 
Line 748 ⟶ 767:
 
Write-Host "Goodbye, world!"
</syntaxhighlight>
 
====Python====
Line comments in [[Python (programming language)|Python]] are delimiated by the hash (#) character. For example:
 
<syntaxhighlight lang="python">
# This program prints "Hello World" to the screen
print("Hello World!") # Note the new syntax
</syntaxhighlight>
 
Python does not provide for block comments<ref name=triple>{{cite web |url=https://www.tutorialdocs.com/tutorial/python3/python3-basic-syntax.html |title=Python 3 Basic Syntax |access-date=25 February 2019 |quote=Triple quotes are treated as regular strings with the exception that they can span multiple lines. By regular strings I mean that if they are not assigned to a variable they will be immediately garbage collected as soon as that code executes. hence are not ignored by the interpreter in the same way that #a comment is. |archive-date=19 August 2021 |archive-url=https://web.archive.org/web/20210819164828/https://www.tutorialdocs.com/tutorial/python3/python3-basic-syntax.html |url-status=dead }}</ref> but a bare [[string literal]] represented by a triple-quoted string can be and often is used for this purpose.<ref>[https://twitter.com/gvanrossum/status/112670605505077248 "Python tip: You can use multi-line strings as multi-line comments"], 11 September 2011, Guido van Rossum</ref><ref name=triple/> In the examples below, the triple double-quoted strings act like comments, but are also treated as [[docstring]]s:
 
<syntaxhighlight lang="python">
"""
Assuming this is file mymodule.py, then this string, being the
first statement in the file, will become the "mymodule" module's
docstring when the file is imported.
"""
 
class MyClass:
"""The class's docstring"""
 
def my_method(self):
"""The method's docstring"""
 
def my_function():
"""The function's docstring"""
</syntaxhighlight>