26

It appears in not just one language that comments can't be nested. Do you have a good solution for this problem? One workaround in C/C++ and Java is to only use the single-line comment but it becomes impossible then to comment out a larger block. I'm facing something like this:

</li><!--
                <li><!-- Save -->

So I must manually go through and edit the comments. Can you advice how we should handle this, in many languages? I'm not sure but maybe python has a solution for this with the ''' way that might be able to include a # comment in python? `

4
  • 4
    I think only editors can help you there. IDLE will block comment for you on Python IIRC though. Jun 13, 2013 at 6:59
  • 8
    Python does not have block comments. The ''' and """ are string literals. It happens that the interpreter will evaluate them during compilation(to bytecode) and recognize the string literals as no-ops(hence they do not slow down the bytecode execution/loading time). Docstrings, i.e. string literals right after a def but before the body, aren't stripped away since the interpreter assumes they provide documentation for the function.
    – Bakuriu
    Jun 13, 2013 at 7:57
  • 7
    In C/C++ if you want to remove large sections you use #if 0 <code> #endif. So this is not really an issue. Using comments to do this is the wrong tool. Jun 13, 2013 at 15:35
  • 1
    I switched to using only line comments long ago (as long as I'm not forced, e.g. Javadoc). Of course, you need editor support (or at least column mode).
    – ziggystar
    Nov 30, 2014 at 15:55

6 Answers 6

46
+450

The best solution is, obviously, to just not nest your comments. Nested comments are usually a sign that you are using comments wrong. The most common example is commented-out code that contains comments itself, and the fix is to remove the code instead of commenting it out.

That said, many programming languages have more than one type of comment syntax, and you can use this fact to nest at least one level deep. For example, in Java:

/* This is commented out!
Foo.bar.baz();
// And now for something completely different...
Quux.runWith(theMoney);
*/

Also, in many languages, at least one type of comment is kind-of-nestable; in C-like languages, line comments inside line comments are ignored:

// some_commented_out(code);
// // This is a comment inside the comment!
// // Still inside the nested comment.
// some_more_code_in(outer_comment);

Most IDEs support commenting entire blocks of code with line comments in one action, and they handle this kind of commenting style correctly. The same example in Python:

# some_commented_out(code)
# # This is a comment inside the comment!
# # Still inside the nested comment.
# some_more_code_in(outer_comment)

Often, coding standards for a particular project 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;
    }
}

With such a style, it is unlikely that you'll ever need to nest /* */ comments (if you have to temporarily disable entire methods or classes, renaming them work just as nicely, if not better); and // comments do nest, at least with a little help from your IDE.

Finally, to disable code, you have other options in many programming languages; for example, in C, you can leverage the preprocessor:

this_is(activated);
#if 0
this_is(!activated);
/* Comments inside this block don't really nest, they are simply removed
   along with the rest of the block! */
#endif

In dynamic languages, you can often just use regular if statements instead:

<?php

if (0) {
   // This should never run... 
   some_stuff_that_should_never_run();
}

However, unlike the CPP example, this strategy requires the source file as a whole to be syntactically valid, so it's by far not as flexible.

And finally, there are at least some languages that allow for nested comments. In case you're interested, wikipedia has a nice comparison chart.

3
  • 2
    does some variant of SQL allow nested comments ? Jun 18, 2013 at 10:54
  • 4
    +1 for // And now for something completely different...
    – Vorac
    Jun 20, 2013 at 8:03
  • 1
    @Vorac: glad you like the reference :D
    – tdammers
    Jun 20, 2013 at 9:19
19

C and C++ does have a nested block comments:

#if 0
#endif

Many highlighting editors understand this as a comment and many other will at least highlight it as any other conditionally disabled code.

In many other languages you have to rely on editor support. For languages that only have line-based comments (perl, python, ruby, shell...) it is rather simple to prepend the comment character to all lines in a range, so most editors can do this. You can still tell what were comments before commenting out the whole block because the comment character is doubled — doing it simply is advantage here.

XML and SGML is probably biggest pain, it's comment definition is just stupid. The comments would have been trivial to nest, but they not only don't, it is completely prohibited to have -- inside comment. Unfortunately I don't know which editors have good support for commenting out in SGML/XML.

1
  • 2
    Never thought of using those preprocessor directives as actual comments. Interesting, also for C#, but in that case, you'd have to do something like #if _ which works nice as well and gets greyed out in my VS with Re#. Nice tip! Jun 13, 2013 at 10:54
2

While not a general solution, and certainly not ideal, one way to tackle this particular problem is to use the server side template processing language to do block comments for nested code comment elements. This leaves the content essentially intact, but prevents sending it to the client browser.

That doesn't help a lot if the file is otherwise straight and pure content that requires no other server side processing. In that case and the more general case of nested comments, ask why do you want to do that. In most of those cases, one might find that the best way to handle it is to not handle it all. In other words, if you want to eliminate a section, then eliminate it, and let version control handle remembering the differences if that section as an artifact ever needs to be resurrected.

2

The D Programming Language has nested comments built in:

/+ This is a nested comment 
  /+ This is part of that a comment +/
  /* So is this */
+/
/+ /* This is another nested comment */ +/
/* /* This is not a nested comment */

In other words, /+ and +/ comments nest.

0

In the HTML/XML case you can use a non-existing processing instruction: see my answer on SO

<?ignore
  <band height="20">
    <staticText>
      <reportElement x="180" y="0" width="200" height="20"/>
      <text><![CDATA[Hello World!]]></text>
    </staticText>
  </band>
?>
</detail>
0

Swift supports nested comments so "it appears in not just one language that comments can't be nested" is no longer a true statement. If you are unhappy with the lack of support for nested comments in your programming language I would suggest giving Swift a try.

/* This is the start of the first multiline comment.
 /* This is the second, nested multiline comment. */
 This is the end of the first multiline comment. */

Swift Programming Language: The Basics

Not the answer you're looking for? Browse other questions tagged or ask your own question.