DEV Community

Camilo
Camilo

Posted on

Using Single and Multiline Comments

This is just a quick trick I have used. Specially in long SQL scripts.

The idea is mixing single and multiline comments for easier handling.

SQL Basics

  • -- Single line comment.
  • /* Multiline comment opening.
  • */ Multiline comment closing.

Mixing

Take this simple SQL operation.

SELECT * FROM users;
Enter fullscreen mode Exit fullscreen mode

Now let's use both single and multiline comments.

--/*
SELECT * FROM users;
--*/
Enter fullscreen mode Exit fullscreen mode

The operation will be the same, but if we delete the single line comment -- before the multiline opening /*, then the operation will be commented out. The --*/ will be interpreted as the end of the multiline comment if multiline is enabled or just a regular comment otherwise, so we do not need to touch it.

This have the following advantage, just need to delete or add the single line -- once, and a whole block of code will be (un)commented out.

Multiline Enabled

/*
SELECT * FROM users;
--*/
Enter fullscreen mode Exit fullscreen mode

Other Programming Languages

This technique can be used in any programming language that supports single and multiline comments.

<?php
// /*
echo "Hello";
// */

my_not_commented_out_function();
Enter fullscreen mode Exit fullscreen mode
<?php
/*
echo "Hello";
// */

my_not_commented_out_function();
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
0x04 profile image
Oliver Kühn

Yeah, I do this since decades 🤓 nice to see it in the wild.

For /, /* comments you can save a key stroke:

//*
echo "Hello";
//*/
/*
echo "Hello";
//*/

I've always called that a scissor comment, but nobody understood what I meant 😅

Collapse
 
clsource profile image
Camilo

Thanks! Nice catch too 🔥