Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Visual Studio resort to single-line comments when commenting a multi-line selection with "Comment Selection"?

Something small that I've always wondered about regarding the Comment Selection option in Visual Studio (Ctrl + K, Ctrl + C).

When I comment this method's implementation single-line comment format is used.

private void Foo()
{
    //Bar b = new Bar();
}

When I comment the parameters from the constructor here (partial line) delimited comment format is used.

private void Foo(Qux q)
{
    Bar b = new Bar(/*q*/);
}

While commenting out the entire method results in this:

//private void Foo()
//{
//    Bar b = new Bar();
//}

I feel like the delimited comment format would be more appropriate in the last situation, since the spec says:

Single-line comments extend to the end of the source line. Delimited comments may span multiple lines.

Does anyone know why this was chosen as the default format when commenting a multi-line selection in Visual Studio?

like image 577
Saragis Avatar asked Feb 10 '23 09:02

Saragis


2 Answers

There would be a few problems doing so:


If any of the code lines had a */ located in it, it wouldn't work:

private void Foo(Qux q)
{
    //we use "*/image/*" flag here to find only images
    Bar b = new Bar("Some wildcard: */image/*");
}

Commented to:

/*
private void Foo(Qux q)
{
    //we use "*/image/*" flag here to find only images
    Bar b = new Bar("Some wildcard: */image/*");
}
*/

If you were hitting "Comment Selection" on a section that already contained a delimited comment, then trying to wrap the code with a delimited comment wouldn't work:

/*
private void Foo(Qux q)
{
    /* Some multiline 
     * comment
     */
    Bar b = new Bar();
}
*/

But fine, we can work around that by a combination of inserting multiple delimited comments and single-line comments:

/*
private void Foo(Qux q)
{
    /* Some multiline 
     * comment
*/
//   */
/*
    Bar b = new Bar();
}
*/

Kinda ugly, but it works. If you came across that commented code, would you be able to immediately recognize what the code parts are and what the comment parts are? Further, if you hit the "Uncomment Selection" command, would you know what you were going to get?

Going further, imagine if you comment this comment, it would get even uglier and more unreadable.


It gets even worse (in my opinion) to workaround/escape the comments if you had */ in your text that you were commenting out:

private void Foo(Qux q)
{
    //we use "*/image/*" flag here to find only images
    Bar b = new Bar("Some wildcard: */image/*");
}

Converts to:

/*
private void Foo(Qux q)
{
    //we use "**//*/image/*" flag here to find only images
    Bar b = new Bar("Some wildcard: **//*/image/*");
}
/*

Compare the above confusing commented code to the existing single-line implementation:

//private void Foo(Qux q)
//{
//    /* Some multiline 
//     * comment
//     */
//    Bar b = new Bar();
//}

//private void Foo(Qux q)
//{
//    //we use "*/image/*" flag here to find only images
//    Bar b = new Bar("Some wildcard: */image/*");
//}

Benefit of this is that the code is pretty much 1:1 exactly what it looked like before, just prefixed with // characters. Still fully readable and still completely predictable what it would look like if you further commented or uncommented it. No issues whatsoever with nested single-line comments or nested delimited comments.


Perhaps finally, it's really, really simple to implement from an IDE standpoint: "Comment Selection" means add a // prefix to every line, "Uncomment Selection" means remove a preceding //. No mucking about with parsing code/comments, or parsing incorrect syntax code/comments.

like image 191
Chris Sinclair Avatar answered Feb 11 '23 22:02

Chris Sinclair


It was described to me that the multi-line comment is an artifact from C and that most C# programmers prefer to use the // style of commenting.

The multi-line style /**/ can get messy if it is used multiple times in a nested structure, and is therefore more error-prone than the single-line counterpart. See this question for more information.

like image 21
aiokos Avatar answered Feb 11 '23 23:02

aiokos