|
In computer programming, a comment is a programming language construct that provides a mechanism for embedding information in the source code that is (generally) ignored by compilers but may be of use to software developers or other programming tools that process the source. Comments may be one of the most talked about language constructs within computer programming circles. They are a subject that crosses programming language boundaries and anybody who writes software can have their own opinion about them as there is almost no experimental evidence backing up any of the claims made about comments. Some people believe that comments are unnecessary because code should be written in a way that makes it self explanatory (it is not uncommon to encounter source code that contains no comments); other people believe that source 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).
Types There are generally two types of comments: block comments and line comments. Block comments are delimited by a sequence of characters that mark the start of the comment and continue until the sequence of characters that mark the end of the comment. Block comments are allowed to span multiple lines of the source code. Typically, block comments do not nest, so any comment start delimiter encountered within a comment body is ignored. Some languages allow nested block comments to facilitate using comments to comment-out blocks of code that may itself contain block comments. Line comments on the other hand either start with a comment delimiter and continue until the end of the line, or in some cases, start at a specific column (character line offset) in the source code and continue until the end of the line. Many programming languages employ both block and line comments with different comment delimiters. For example, C++ has block comments delimited by / and that can span multiple lines and line comments delimited by //.
Usage Comments could summarise code or explain the programmer's intent. This is called the why rather than how approach. The two are often close, but not always. According to this school of thought, restating the code in plain English may be a waste of time; the need to explain the code may be a sign that it is too complex and should be rewritten. "Don't document bad code – rewrite it" (The Elements of Programming Style, Kernighan & Plauger). "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." (Code Complete, McConnell) Comments could also be used to guide a new programmer through source code that performs some task. In this case almost every line could be commented. New programmers can gain much insight in various branches of programming and computer science by reading through extensively commented source code. Typical things that could be commented on are function calls and arguments, algorithms used, and caveats. Sometimes a programmer thinks up a neat trick to perform a certain task. Comments could in this case provide an explanation of the trick used. Although the why rather than how approach discourages such comments, sometimes an explanation is just what is needed to make a future programmer understand what a certain piece of source code is doing. This might especially be true in the case of rarely-used optimizations, constructs or function-calls. For example, a programmer may add a comment to explain why an insertion sort was chosen instead of a quicksort, as the former is, in theory, slower than the latter. This could be written as follows: list = f (b), f (b), f (c), f (d), f (a), ...; // Need a stable sort. Besides, the performance really does not matter. insertion_sort (list); Comments may also be used to explain why a block of code does not seem to fit conventions or best practices. This is especially true of projects involving very little development time, or in bug fixing. For example: ' Second variable dim because of server errors produced when reuse form data. No ' documentation available on server behavior issue, so just coding around it. Logos, diagrams, and flowcharts consisting of ASCII art constructions can be inserted into source code formatted as a comment. Additionally, copyright notices can be embedded within source code as comments. In debugging, programmers will sometimes mark a code fragment as a comment, such that the program will not compile or interpret it. This is called commenting out the fragment. Styles Comment styles are often agreed upon before a project starts. Usually programmers prefer styles that are easy to modify and difficult to break. For example, C-style comments could look like this / This is the comment body. / If a programmer's editor doesn't manage the second variant automatically, it may discourage changes to the comments, thus leading to comments which are out of date with respect to the code. On the other hand, the visibility of the second comment variant is much higher. Some people, such as Allen Holub (in his book Enough Rope to Shoot Yourself in the Foot, ISBN 0-07-029689-8, 1995, McGraw-Hill), advocate aligning the left edges of comments: / / Different styles can be chosen for different areas of code, from individual lines to paragraphs, routines, files, and programs. If the syntax supports both line comments and block comments, one approach is to use line comments only for minor comments (declarations, blocks and edits) and to use block comments to describe higher-level abstractions (functions, classes, files and modules). Certain projects try to define rules like "one comment every ten lines". They can be counterproductive when adhered to with too much rigor, but might still be of use when one wants to quickly judge after writing a body of code whether one needs to check if enough comments are in place. Automatic documentation generation Some programming tools can read structured information in comments in order to generate documentation automatically. Automatic documentation generation from the comments in the source code allows the documentation of the interface and use of the code to be maintained in the source file with the code, but to be viewed in independent stand-alone documentation. Keeping the documentation close to the code makes it easier, and thus more likely, to keep the documentation up to date with changes in the code. Examples of documenation generators include the javadoc program, designed to be used with the Java programming language, Ddoc for the D programming language and doxygen, to be used with C++, C, Java, IDL and others. C# implements a similar feature called "XML Comments" which are read by IntelliSense from the compiled .NET assembly. The next revision of Visual Basic will also feature this. Other uses Developer tools might store "meta" data in comments, such as insert positions for automatic header file inclusion, commands to set the file's syntax highlighting mode, or the file's revision number. These functional control comments are commonly referred to as annotations. Comparison See main article: Programming Language Comparison: Comments. C
&& !capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RESOURCE)) goto bad_fork_free; (This is from the fork.c file from the Linux kernel source)Java
(from the Sun Microsystems javadoc documentation; the comment is designed to be read by the javadoc processor) Perl (from perlbug.PL in the standard perl distribution) // commentrem commentPHP
$j++; // commentrem comment(from , the software which powers Wikipedia, an on-line collaborative encyclopedia) Visual Basic ' ' Cut off HKEY_USERS if present. ' This makes interoperating with regedit easier. ' If Len(SIDstring) > 11 Then If Left(SIDstring, 11) = "HKEY_USERS" Then SIDstring = Mid(SIDstring, 12) End If End If ' ' Open the WMI Service and retrieve the SID ' On Error Resume Next Set SIDobject = GetObject( _ "winmgmts:" _ ).Get("Win32_SID.SID='" & SIDstring & "'") If Err Then MsgBox "Could not retrieve the SID.", vbOkOnly, "Sorry" Exit Do End If (Adapted from an example illustrating ways to create system administration tools.) | ||||||||
|
| |||||||||
![]() |
|
| |