Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the distinction between single-line and multi-line comments? [closed]

In the languages like python, ruby, javascript, there is separate syntax for single-line and multi-line comments. I don't see the benefit of this complexity. Why, from a language design perspective, is there a distinction between the two? Using javascript as an example, here's the implementation:

// I'm a single-line comment!

/* 
I'm a 
multi-line
comment
*/

Why not simply (something like):

/* Just a comment */

/* 
Doesn't matter 
how many 
lines 
/*
like image 858
rainbowsorbet Avatar asked Nov 16 '25 02:11

rainbowsorbet


2 Answers

  • People need to comment out blocks of code at once
  • People also need to comment out single lines

Commenting out a single line is faster when there’s only one change to make.

There are languages that only support comments with a starting and ending delimiter — C89 is a notable example (ever think C99 added // for a reason?). However, using the same starting and ending character would make a complete mess if you forgot to take one (or the other) out. Every comment following it would be flipped, and how would the compiler (if it’s a somewhat helpful one) know where the syntax error was?

like image 151
Ry- Avatar answered Nov 17 '25 16:11

Ry-


One possible answer is that single-line comments are faster to add. For example, in JS you need 2 special characters to add single-line comment and 4 to add multi-line comment.

like image 42
Lukasz Kamieniecki-Mruk Avatar answered Nov 17 '25 15:11

Lukasz Kamieniecki-Mruk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!