Q: 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?


*

*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


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

A: I just came across StyleCop's SA1005 rule, which states: 

A violation of this rule occurs when a
  single-line comment does not begin
  with a single space. For example:
private void Method1()
{
  //A single-line comment.
  //   A single-line comment.
}

The comments should begin with a single space after the leading forward slashes:
private void Method1()
{
  // A single-line comment.
  // A single-line comment.
}


Since StyleCop is in one way or another a Microsoft product, I'd say this qualifies as an official coding standard with respect to spaces in line-comments.

A: I've developed software in many languages for about 10 years on projects large and small. I have yet to see anyone intentionally not use a space. In the scheme of things it doesn't really matter that much (after all, we all know those are comments and can read them), but I do think the no-space version looks similar to commented-out code and requires an extra millisecond of brain power to confirm it is a comment :-)

A: 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;-).

A: In the last 24 years, I've developed and maintained code professionally in C, C++, Pascal, BASIC, Java, Perl, Objective C, Bourne shell, bash, csh, tcsh, and assembly for 68K, PowerPC and x86. During this time, I've noticed a few things...


*

*Comments with leading spaces are at least 1000 times more common than comments without spaces. Missing leading spaces in comments are most often typos due to hasty typing.

*I can't remember ever seeing comments in sample code in a professional book or manual without the space.

*The only professional developers I've known who routinely omit the leading space in comments grew up using a non-Western, ideographic writing system that doesn't use spaces.

*I have never, ever seen an official company coding style that tells people to omit the leading space in comments.
So all in all, I would say the overwhelming evidence is that a space after the line-comment is preferred.

A: I mostly avoid having comments on the end of a line of code, because then the comments hang off the end and aren't as easy to parse when scanning. When I have a good reason, though, I like to use two spaces to separate code and comments (then one space after the comment marker). It just makes it easier for the eye...
Consider:
    int top;  // Index of the top of the stack.

versus:
    int top; // Index of the top of the stack.

Subjectively, it seems like two spaces just makes it easier to split up what's code and what's not.

A: Well I have found the standard (according to Wikipedia) for commenting in Java. This is supposed to be "consistent with the Sun Microsystems Javadoc standards":

/**
 * Registers the text to display in a tool tip.   The text
 * displays when the cursor lingers over the component.
 * @param text  the string to display.  If the text is null, 
 *              the tool tip is turned off for this component.
 */


So I'm starting to think the standard is a space.
Also, all the other examples have a space.

A: The Google C++ Styleguide requires two spaces https://github.com/google/styleguide/blob/gh-pages/cpplint/cpplint.py#L3014
i.e. int foo = 1337 //  bar

A: 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.
...

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

