4

Using Visual Studio 2012 with C#, when I write multi-line comments, VS automatically begins each line with an asterisk, like this:

/*
 *
 *
 */

I was just curious if this was the case in programming languages other than C#, or if there was a specific reason for this template.

  • This is common in Java too oracle.com/technetwork/java/javase/documentation/… – scunliffe Mar 14 '14 at 3:09
  • I'd assume this originated in C – Brendan Mar 14 '14 at 5:17
  • I guess it also aids readability because you can clearly see its a multiline comment. – Keagan Ladds Mar 14 '14 at 9:27
  • 8
    N.B. for this style of comment the middle asterisks aren't required. I'd imagine they're just there to aid readability for the case where the IDE (or editor) doesn't have coloured syntax highlighting. – Robbie Dee Mar 14 '14 at 9:57
  • I think it's present in a variety of broadly understood C-like (in the syntactic aspect) languages, including eg. PHP – Konrad Morawski Mar 19 '14 at 13:06
10

Some of the first comment syntax introduced into programming where statement comments. These kinds of comments required their own line.

Such as in basic

REM This is a comment.

This created longer source code and used a keyword. Around the same time came end-of-line comments that requires only one character to start the comment.

Such as Fortran or Assembly

MOV C,1 ; This is a comment

For languages that had simple grammar this was fine, but as more advanced grammar was developed two characters were used to start a comment.

Such as in C++

int c = 1; // This is a comment

Or such as SQL

SELECT * FROM x -- This is a comment

Many languages supported block comments in addition to end-of-line comments. These languages often supported the use of math operators *+/ to write expressions.

A block comment can not use the same grammar syntax used for code blocks. For example, if { } is used to block code then it can't be reused for comments. The same is true for () brackets since they are used to order math expressions. So often two characters were used to open/close the block comment.

We take syntax highlighting of today's IDEs for granted. When block comments were introduced to programming there was no syntax highlighting. Someone could easily introduce a bug if block comments were too close to math expressions.

For example; If you used (- this is a comment -) to mark blocks, then you could easily write a math expression as;

int x = 1+(-10);
int y = 2+(z-);

The above would actually compile to int x = 1+;

These kinds of problems actually do exist in some older languages. As a result the use of /* this is a comment */ for multi-line comments became very popular. The reason is that visually /* is not a valid math operations.

For example;

int x = 1 /* 8;
int y = 3 */;

To a human reader there is no mistake. It's not valid math. You can still read the comment inside the expression. StackExchange is adding syntax highlight (lol) but picture it without and colors.

This became a standard comment block because it solved many grammar problems in parsing the language while remaining easily identifiable to humans as a comment.

When VS adds an extra * to each line. It's to ensure readability of the comment when there is no syntax highlighting. If you open the source code in a regular text editor, then those lines are clearly shown as a comment.

If the start of a comment /* scrolls above the screen. There is no way to know if what you're reading is a comment or not. Example; someone blocked out a chunk of code.

I was just curious if this was the case in programming languages other than C#, or if there was a specific reason for this template.

The specific reason is human readability, and has nothing to do with the programming language itself. For long comments that may scroll off screen it allows the reader to understand the text is a comment.

Any character would suffice as this example;

/*
 # Still a comment.
 # Still readable if very long.
 #
 #
 */

Visual Studio will automatically add * characters to multi-line comments. This is a configurable options in the IDE.

It should be noted that other popular IDEs also offer this feature. I've seen it in Eclipse, WebStorm, IntelliJ and NetBeans. Those editors will add new lines to comment blocks while you type for languaging such as C++, Java and PHP.

I've never seen this done for XML or HTML block comments in any editor. It appears to be a common feature for C style languages.

  • 1
    Some good stuff here, but as with many other answers we've had on this question before, it doesn't answer the question... – Robbie Dee Mar 19 '14 at 15:14
  • @RobbieDee not sure how I understand. What did I miss? – Reactgular Mar 19 '14 at 15:30
  • I believe he does answer it in the last 2 paragraphs. He gives the specific reason. The rest of the answer covers languages other than C#, like C and also gives great background (or really good legend). Now, does VS do it for other languages that it may support? He didn't say, but it doesn't matter either. – BPugh Mar 19 '14 at 15:36
  • The I was just curious if this was the case in programming languages other than C# part of the question isn't addressed at all and the other half is a little speculative in the absence of authoritative sources. The majority of the answer reads as a history of coding comments which whilst interesting, isn't really relevant IMHO. Nice though. – Robbie Dee Mar 19 '14 at 16:37
  • 1
    @RobbieDee: The OP asked two questions, the 2nd being, whether "there was a specific reason for this template." Mathew picked one to answer (and, IMO, he picked the more interesting one). If this kind of thing bothers you, vote to close the question as too broad; this is the price we pay when an asker tries to stick more than one question into a post. Conveniently, dgvid's answer answers both questions, though more tersely. – Brian Mar 19 '14 at 18:23
4

I've seen this pattern many times in various C-syntax languages such as C, C++, Java, C#, and even JavaScript. The extra asterisks at the beginning of each line have no syntactic significance. They are there purely to aid in readability by catching your eye and making obvious that you are looking at a multi-line comment.

The use of this template (comment pattern) predates by many, many years the appearance of IDEs and text editors that could style comments by displaying them in a unique color. So to get a feel for the utility of this approach, look at file that uses a variety of comment types on the console or in an editor that displays all text in the same style. Try to quickly spot the comments.

  • +1 Yep, was very common practice on the mainframe as I recall. Especially useful when blocks of code were commented out. If there was no accompanying comment, this was very easy to miss. – Robbie Dee Mar 19 '14 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.