Comment Styles in Different Programming Languages.

Comment Styles, In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters. The syntax of comments in various programming languages varies considerably.

The flexibility provided by comments allows for a wide degree of variability, but formal conventions for their use are commonly part of programming style guides.

How best to make use of comments is subject to dispute; different commentators have offered varied and sometimes opposing viewpoints. There are many different ways of writing comments and many commentators offer conflicting advice.

Ada

The Ada programming language uses ‘–‘ to indicate a comment up to the end of the line.

  -- your comment
  -- your comment

AppleScript

This section of AppleScript code shows the two styles of comments used in that language.

(*
your comment
*)

-- Show the greeting

BASIC

In this classic early BASIC code fragment the REM (“Remark”) keyword is used to add comments.

Microsoft BASICs, including Quick Basic, Q Basic, Visual Basic, Visual Basic .NET, and VB Script; and in descendants such as FreeBASIC and Gambas any text on a line after an ‘ (apostrophe) character is also treated as a comment.

10 REM your comment
15 REM your comment

' your comment
' your comment
rem your comment.

C

This C code fragment demonstrates the use of a prologue comment or “block comment” to describe the purpose of a conditional statement.

 /*
  * your comment
  * your comment
  * your comment
  * your comment
  */

ColdFusion

ColdFusion uses comments similar to HTML comments, but instead of two dashes, it uses three. These comments are caught by the ColdFusion engine and not printed to the browser.

<!--- your comment --->

Fortran IV

This Fortran IV code fragment demonstrates how comments are used in that language, which is very column-oriented. A letter “C” in column 1 causes the entire line to be treated as a comment.

C
C your comment
C

Haskell

Line comments in Haskell start with ‘–‘ (two hyphens) until the end of the line, and multiple line comments start with ‘{-‘ and end with ‘-}’.

{- your comment
your comment -}

-- your comment

Java

This Java code fragment shows a block comment used to describe the method. The formatting is consistent with Sun Microsystems Javadoc standards. The comment is designed to be read by the Javadoc processor.

/**
 * your comment
 */

// your comment

JavaScript

JavaScript uses // to precede comments and /* */ for multi-line comments.

// A single line JavaScript comment

/*
multi-line
JavaScript comment
*/

Lua

The Lua programming language uses double-hyphens, –, for single line comments in a similar way to Ada, Eiffel, Haskell, SQL and VHDL languages. Lua also has block comments, which start with –[[ your comment goes here ]]

--[[
Code block for comment or un-used code
--]]

-- no action (commented out)

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

% Single Line Comments

%{
    Multiline Comments
  
 %}

Nim

Nim uses the ‘#’ character for inline comments. Multi-line block comments are opened with ‘#[‘ and closed with ‘]#’. Multi-line block comments can be nested.

## Documentation of the module 
# This is a comment, but it is not a documentation comment.

#[' your Comments ']#

Objective-C

Objective-C uses // to precede comments

// A single line Objective-C comment

OCaml

OCaml uses nestable comments, which is useful when commenting a code block.

(* comment level 1(*comment level 2*)*)

Pascal

In pascal comments are opened with ‘(‘ and completed with ‘)’.

(* comment here *)

Perl

Line comments in Perl, and many other scripting languages, begin with a hash (#) symbol.

# A simple Comment

R

R only supports inline comments started by the hash (#) character.

# your Comments

PHP

Comments in PHP can be either in C++ style (both inline and block), or use hashes. PHPDoc is a style adapted from Javadoc and is a common standard for documenting PHP code.

# Single line comment

Python

Inline comments in Python use the hash (#) character

# your Comments

Ruby

Single line comments: (line starts with hash “#”)

# this is a comment

Multi-line commenting: (comments goes between keywords "begin" and "end")

=begin

your Comments

your Comments

=end

SQL

Standard comments in SQL are in single-line-only form.

-- your Comments
-- your Comments

Swift

Single-line comments begin with two forward-slashes (//):

// This is a comment.

/* your Comments
 your Comments */

XML (or HTML)

Comments in XML (or HTML) with , single and multiline comments

<!-- your Comments -->

Happy Commenting 🙂


Discover more from CODE t!ps

Subscribe to get the latest posts sent to your email.

Scroll to Top

Discover more from CODE t!ps

Subscribe now to keep reading and get access to the full archive.

Continue reading