Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting block comments

Tags:

coding-style

The first style of formatting seems to be much more popular than the second. Why is that?


The first (asterisk on every line)

/*
 * line 1
 * line 2
 * line 3
 */

The second (the minimum amount of asterisks)

/*
line 1
line 2
line 3
*/
like image 700
Emanuil Rusev Avatar asked Sep 24 '10 13:09

Emanuil Rusev


People also ask

How do you format multiple lines in comments?

Multiline (Block) Comments Javascript multiline comments, also known as block comments, start with a forward slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/). They do not require a comment delimiter character on every line and may contain newlines.

How do you write a block comment?

Everything from the // to the end of the line is a comment. To mark an entire region as a comment, use /* to start the comment and */ to end the comment. * This is a block comment.

What are block comments in C?

You can comment out one or more lines of code in any C/C++ editor view. The leading characters // are added to the beginning of each line when commenting one or more lines of code. You can also block comment multiple lines of code using the characters /* */ .

What is the symbol for block comments?

The beginning of a block comment or a multi-line comment is marked by the symbol /* and the symbol */ marks its end. This type of a comment is called so as this can extend over more than one line; once the compiler reads the /* it ignores whatever follows until it enounters a */ .


3 Answers

Probably because it is more readable, in case the comment has a lot of rows you know you are reading a comment even if you do not see the end.

like image 120
GôTô Avatar answered Sep 16 '22 23:09

GôTô


The main reason is because of the PHP Documenters.

Documenters such as PHPDoc are built to parse comment blocks in that form, an example of a parsable comment block is like so:

/**
 * Page-Level DocBlock
 * @package MyPackage
 * @category mycategory
 */

As you can see that the asterisk is on each line and some lines contain an @ symbol, this is what you call a tag denoter, it tells the parser that this line should be processed and file under the category value for the documentation.

Also taking a look at the Zend Coding Standards - Inline Documentation this also states that you should use this type of commenting for such parsers and readability.

like image 24
RobertPitt Avatar answered Sep 16 '22 23:09

RobertPitt


It is easier to see where the comment begins and ends.

One need only scan down the left column until the asterisks 'run out' to find the next bit of code.

Where the first method breaks down is when it comes time to rewrite the comments. Now it requires reformatting the lines to make the asterisks line up. That is a no-no unless you have a tool to do that for you automatically.

In McConnell's "Code Complete" (second ed), p 790, he says:

For longer comments, the task of creating long columns of double slashes, manually breaking lines of text between rows, and similar activities is not very rewarding, and so the /* ... */ syntax is more appropriate for multiline comments.

The point is that you should pay attention to how you spend your time. If you spend a lot of time entering and deleting [text] to make [the asterisks] line up, you're not programming; you're wasting time. Find a more efficient style.

like image 33
BryanH Avatar answered Sep 18 '22 23:09

BryanH