Why nested comments use by some programming languages such as MATLAB ,I just want to know usage of this kind comments in a program and what are the advantages we can gain by using this nested comments ?
-
15For one, you can easily comment out code blocks that already contain comment blocks. – DrummerB Dec 11 '13 at 19:17
-
@DrummerB : +1! Can you post this as an answer. – 0xF1 Dec 11 '13 at 19:39
-
Not to be pedantic, but I think that you're referring to block comments (e.g., `/* ... */` in C or `%{ ... %}` in Matlab) rather than single line comments (e.g., `// ...` in C or `% ...` in Matlab). – horchler Dec 11 '13 at 19:51
-
2thanks @DrummerB , horchler I was referring to comment blocks inside another comment blocks – Kelum Dec 12 '13 at 09:38
5 Answers
The answer is nested comments allows commented-out code that contains comments itself example in C++ has block comments delimited by /../ that can span multiple lines and line comments delimited by //.

- 419
- 4
- 7
-
just a suggestion: you could add a code example to illustrate your point – chharvey Jun 24 '19 at 04:11
Usually, coding standards for a particular project or program have rules about which comment style to use when; a common convention is to use block comments (/* */) for method and class documentation, and inline comments (//) for remarks inside method bodies and such, e.g.:
/**
* Helper class to store Foo objects inside a bar.
*/
public class Foobar {
/**
* Stores a Foo in this Foobar's bar, unless the bar already contains
* an equivalent Foo.
* Returns the number of Foos added (always 0 or 1).
*/
public int storeFoo(Foo foo) {
// Don't add a foo we already have!
if (this.bar.contains(foo)) {
return 0;
}
// OK, we don't have this foo yet, so we'll add it.
this.bar.append(foo);
return 1;
}
}
If someone wants to temporarily disable entire methods or classes in the above program.It's very helpful, if that language allows nested comments.

- 1,745
- 5
- 24
- 45
You can use comments...:
to temporally disable some lines of code.
as titles for sections.
to comment each line.
to add some notations or comments on other comments.
to send macro orders.
And you can mix all of them. That's why we need different ways to mark comments and create nested comments.

- 7,423
- 14
- 59
- 96
Good old Turbo Pascal aka Borland Pascal allows multi-line comments either with curly braces { }
or with parenthesis star (* *)
, which nest independently of one another even though multi-line comments in the same style do not nest.
A good workaround from my old work place was use of typical brace { }
comments for all informational comments and specialized use of the less common parenthesis star (* *)
only to comment out code. Marking the middle lines of commented out code with something like **
is still a decent idea, and macros can be used to achieve this in programmer editors
function ComputeCost(var x : longint);
{ Wide version: Apply discounts to raw price.}
(* CODE GRAVEYARD!
** function ComputeCost(var x : integer);
** {Apply discounts to raw price.}
*)
Minimalists will always discount the need for nested comments by saying that C style languages allow constructs like #ifdef SOMETHING
or the elegantly short #if 0
to disable code. True minimalists want old code removed completely and say version control takes the place of keeping old code. A good counter is that commented out code together with programmer editors with folding support, e.g. Vim, allows visually stepping over dead code while keeping it for reference.

- 11
- 2
I feel that nested comments are not necessary! In general a comment is omitted by the compiler so comments serve a main purpose for indicating the programmer what he had done or a new programmer to know the flow of the program..why unnecessarily nest comments..just an indication that can be without nesting.. eg:
for(;;)
{
if()
{
}
}/* a loop with an if condition*/
**need not be as**
/*a loop/*if condition*/for n times*/