Search code examples
coding-stylecomments

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


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?


Solution

  • Python's official style guide, PEP 8, is very clear about this issue:

    Each line of a block comment starts with a # and a single space (unless it is indented text inside the comment).

    and:

    Inline comments should be separated by at least two spaces from the statement. They should start with a # and a single space.

    This confirms everybody's anecdotal evidence, but I think this is the first answer to quote "some official or otherwise published coding standards" as requested;-).