How to Solve the Problem of Nested Comments in Java, C++, Python, and C

ccommentsjavapython

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?
`

Best Answer

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.

Related Topic