Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 386
Mark Rushakoff Avatar asked Sep 23 '09 16:09

Mark Rushakoff


People also ask

How many spaces should there be after a hash in a comment?

Write comments in English. For a block comment with multiple paragraphs, add a blank line between paragraphs with a single comment tag preceding a blank line. For inline comments, leave at least two spaces between the code and the comment.

What do line comments start with?

Summary of comment types: use // for a single line. Everything from the // to the end of that line of the file is ignored by the program and is only for use by the human reader of the code.

What are the two character for single line commenting?

// Single-line comments The comment begins with a double slash (//) and ends at a newline character (\n), a carriage return (\r), or the end of the file.


2 Answers

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 :-)

like image 90
SingleShot Avatar answered Sep 22 '22 03:09

SingleShot


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

like image 21
Alex Martelli Avatar answered Sep 22 '22 03:09

Alex Martelli