Switching between code variants using comments

Posted on April 1, 2024 by Linus Heckemann

Warning: this post is for entertainment purposes only and explains a hack that everybody may hate you for actually using, as well as post-hoc justification for the hack. Proceed with caution.

Sometimes I have a piece of code where I want to toggle between two variants frequently.

int a = do_one_thing();
// int a = do_another_thing();

Commenting one out and uncommenting the other is a pain.

A sensible person might introduce a conditional:

int a = true
    ? do_one_thing()
    : do_another_thing();

However, toggling that in the obvious way* also requires typing multiple characters. I’m too lazy for that! Also, it introduces more indentation and it just doesn’t sit very well with my aesthetic preferences.

Plus I like that syntax highlighting makes it really clear which piece of code is currently active. Maybe we can implement a toggle using comments?

We have at our disposal:

  • // to introduce a single-line comment
  • /* to start a multiline comment
  • */ to end a multiline comment

Let’s start by commenting out do_one_thing with its own multiline comment:

/*
int a = do_one_thing();
*/

Now, if we want to bring it back we can comment out the multiline comment opening:

//*
int a = do_one_thing();
*/

Unfortunately that introduces a syntax error, so we need to comment out the end of the multiline comment too:

//*
int a = do_one_thing();
//*/

Now we can toggle do_one_thing on and off by adding/removing the extra / at the beginning: when it’s there, the multiline comment beginning is commented out by the single-line comment, and when it’s not there the single-line comment that terminates the multiline comment doesn’t cause any problems because it itself is commented out. So far so good!

Now what about do_another_thing? Let’s start by commenting it out in the same way:

//*
int a = do_one_thing();
//*/
/*
int a = do_another_thing();
//*/

This is ugly. Can’t we reuse that /* we have in the line before? Of course we can!

//*
int a = do_one_thing();
/*
int a = do_another_thing();
//*/

Now all we need to do is add another / to ensure that if we toggle out do_one_thing by removing the / at the beginning, the comment is still closed:

/*
int a = do_one_thing();
/*/
int a = do_another_thing();
//*/

Perfect! Now we can switch between the two variants simply by adding/removing that one /, and syntax highlighting makes it clear which variant is active:

//*
int a = do_one_thing();
/*/
int a = do_another_thing();
//*/

This pattern should work in any language which:

  • Supports /* */-style multiline comments,
  • Has single-line comments,
  • and doesn’t ruin the fun by having nested comment syntax.

For example, Nix:

let
a =
#/*
do_one_thing
/*/
do_another_thing
#*/
;
b =
/*
do_one_thing
/*/
do_another_thing
#*/
;

or PHP:

#/*
$stmt = "SELECT *";
/*/
$stmt = "DELETE";
#*/
$stmt .= " FROM customers;";
/*
$stmt = "SELECT *";
/*/
$stmt = "DELETE";
#*/
$stmt .= " FROM outstanding_invoices;";

Isn’t it beautiful? Don’t @ me.