Or, toggle ability by adding (and optionally and only optionally) removing only a single character:
int a = true ? do_one_thing() : do_another_thing();
// and
int a = !true ? do_one_thing() : do_another_thing();
// and
int a = !!true ? do_one_thing() : do_another_thing();
This is really clever. It’s hardly obvious to another reader, though.
In C and C++, I have written
#if 0
code variant 1
#else
code variant 2
#endif
The intent is clear. I can switch between variants by modifying a single character, changing 0 to 1 or vice versa. The code highlighting in my editor (vim) figures this out and greys out the disabled code.
If C/C++ can do this, maybe you should demand that your favourite language should support this feature as well (statically switching between code variants I mean, not necessarily by adding the C preprocessor).
This kind of technique is done strictly for your eyes only, in the heat of debugging. It’s only for the convenience of the author/debugger. If it needs to be committed longer-term, yeah: please do anything else. However such techniques are still extremely valuable to have in your personal toolbox; do not disregard them just because they shouldn’t be committed for others to read.
It’s the same difference as writing for thinking vs. writing for an audience.
I use crate features for this in Rust, which is somewhat tedious if the condition is only in one place, and immediately wonderful as soon as I need the same condition in a second place. Temporarily of course. IntelliJ highlights enabled/disables features appropriately.
Wow. I’ve known about the ‘enable/disable element with one character’ trick mentioned first for many years, but the switching trick is even more brilliant.
If you use Vim, you can set up a way to toggle conditionals like this with one keystroke:
int a = true
? do_one_thing()
: do_another_thing();
Vim’s built-in ^A and ^X (Ctrl-A and Ctrl-X) mappings increment or decrement the number under the cursor. The Vim plugin SwapIt extends ^A and ^X to support incrementing or decrementing other types of values.
Install SwapIt using your Vim plugin manager of choice. Position the cursor on true, then press ^A. It will be instantly changed to false.
If you want to enhance ^A and ^X further, you can install speeddating.vim. It adds support for incrementing and decrementing dates, times, and Roman numerals.
IDK but it feels wrong in a good way to turn comments into some weird form of conditional programming. I enjoyed reading this and seeing someone else devise the same mechanism. (I don’t think you can really find another one with how basic comments are, but I would live to find an alternative for the sake of hacking)
I have happily used the following to switch between variants in Ruby (which doesn’t have block comments). With this method, switching requires changing only one character to another:
# change the index from `0` to `1` or `2`
mode = [:do_one_thing, :do_second_thing, :do_third_thing][0]
if mode == :do_one_thing
# …
elsif mode == :do_second_thing
# …
else
# …
end
This should be imitable in any language that has array and string literals. You can also index on the array [false, true] if you have only two variants and your language doesn’t interpret 0 and 1 as falsy and truthy.
In imperative languages, if the statements don’t have side effects, I started just uncommenting all of the assignments and reorder them such that the current one is at the bottom /shrug
Or, toggle ability by adding (and optionally and only optionally) removing only a single character:
This is really clever. It’s hardly obvious to another reader, though. In C and C++, I have written
The intent is clear. I can switch between variants by modifying a single character, changing 0 to 1 or vice versa. The code highlighting in my editor (vim) figures this out and greys out the disabled code.
If C/C++ can do this, maybe you should demand that your favourite language should support this feature as well (statically switching between code variants I mean, not necessarily by adding the C preprocessor).
This kind of technique is done strictly for your eyes only, in the heat of debugging. It’s only for the convenience of the author/debugger. If it needs to be committed longer-term, yeah: please do anything else. However such techniques are still extremely valuable to have in your personal toolbox; do not disregard them just because they shouldn’t be committed for others to read.
It’s the same difference as writing for thinking vs. writing for an audience.
I use crate features for this in Rust, which is somewhat tedious if the condition is only in one place, and immediately wonderful as soon as I need the same condition in a second place. Temporarily of course. IntelliJ highlights enabled/disables features appropriately.
For a single place use, this should solve your problem? https://stackoverflow.com/a/72154186
Oh, that’s clever. Why didn’t I think of that? Thank you!
Truly, today is a special day
Wow. I’ve known about the ‘enable/disable element with one character’ trick mentioned first for many years, but the switching trick is even more brilliant.
If you use Vim, you can set up a way to toggle conditionals like this with one keystroke:
Vim’s built-in
^Aand^X(Ctrl-A and Ctrl-X) mappings increment or decrement the number under the cursor. The Vim plugin SwapIt extends^Aand^Xto support incrementing or decrementing other types of values.Install SwapIt using your Vim plugin manager of choice. Position the cursor on
true, then press^A. It will be instantly changed tofalse.If you want to enhance
^Aand^Xfurther, you can install speeddating.vim. It adds support for incrementing and decrementing dates, times, and Roman numerals.and if anyone prefers a lua plugin there is boole.nvim that does the same thing
I wrote a similar post a long time ago: https://kevincox.ca/2013/03/22/c-comment-fun/
IDK but it feels wrong in a good way to turn comments into some weird form of conditional programming. I enjoyed reading this and seeing someone else devise the same mechanism. (I don’t think you can really find another one with how basic comments are, but I would live to find an alternative for the sake of hacking)
I have happily used the following to switch between variants in Ruby (which doesn’t have block comments). With this method, switching requires changing only one character to another:
This should be imitable in any language that has array and string literals. You can also index on the array
[false, true]if you have only two variants and your language doesn’t interpret0and1as falsy and truthy.In imperative languages, if the statements don’t have side effects, I started just uncommenting all of the assignments and reorder them such that the current one is at the bottom /shrug