Space between line-comment character(s) and start of actual comment

Viewed 15360

I realize that this rule might differ from one company's coding standards to another, but in general, which is preferred?

  1. With a space after the line-comment:

    int foo = Bar(quux + 1); // compensate for quux being off by 1
    
    foo = Bar(quux + 1) # compensate for quux being off by 1
    
  2. No space after the line comment:

    int foo = Bar(quux + 1); //compensate for quux being off by 1
    
    foo = Bar(quux + 1) #compensate for quux being off by 1
    

I haven't been able to find anything online regarding this aspect of coding style. My guess is that including a space is the preferred style for all languages, but I'd like some "hard evidence" to confirm or deny this.


It sounds so far like everyone has anecdotal evidence that using a space is preferred. Can anyone point me in the direction of some official or otherwise published coding standards that directly address the issue of comment formatting and whether a space should be used?

8 Answers

The Google Java Style Guide Formatting section requires putting a space on both sides of a comment:

4.6.2 Horizontal whitespace

Beyond where required by the language or other style rules, and apart from literals, comments and Javadoc, a single ASCII space also appears in the following places only.

...

  1. On both sides of the double slash (//) that begins an end-of-line comment. Here, multiple spaces are allowed, but not required.
Related