Using Single and Multiline Comments

Camilo - Dec 31 '19 - - Dev Community

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
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .