"This trick below can be used to toggle between two sets of code, with one symbol in the code with no additional short-keys to learn, and no tool dependencies!"
So can #ifdef TEST / #ifndef TEST , which also has the advantage of not being horrific.
Why are there so many angry comments on this thread?
I use this sort of trick all of the time when debugging, but I rarely commit any commented out code as a matter of principle.
Nobody was suggesting using this for "ghetto source control" or that this is an alternative to compiler directives.
Can't we just be polite and say "Cool hack, but obviously don't commit this type of code", instead of getting out the pitchforks?
Yikes. At least give the next coder that will maintain your code (and perhaps your future-self) a chance at understanding it. If the goal is to switch between prod and test code by flipping one char, then this works, too
if (1)
productionStuff();
else
testStuff();
This should not be used. I would reject the code review if this came by me and recommend using ifdefs.
Compilers are not uniform in their interpretation of nested comments.
Better way:
/**
testCode(); testCode();
/**/
(...) /**/
productionCode(); productionCode();
/**/
I think this is better because it's much easier to see what you're supposed to do in order to switch the block on or off, and it's only a single character change per block. (though you can't switch from test to production in one character, but meh).However, I tend to see commented code as ugly cruft. To me, comments are for explaining in natural language what you're doing, not for deactivating bits of code. If it's bad get rid of it, if it's good keep it. Why comment code?
The only time I comment out code is to warn developers away from doing something which is obvious but a bad idea, for example:
/*
// This was a neat idea but it makes it break if the user isn't logged in
userPreference = getPrefFromUserSession();
*/
No, no, no, no, no! This is a terrible idea, and will end up causing you a massive headache in the long-run. If I see commented-out code like this, I will delete it because that's what version control is for.
If you really need to turn code on/off in different environments, you should be using feature flags: http://code.flickr.net/2009/12/02/flipping-out/ and at the very least you should be detecting the environment:
if (ENV == 'dev')
testStuff();
else
productionStuff();
This is much more meaningful to other developers (and to yourself in the future).This will compile to different things depending on whether you are using a C or a C++ compiler. Worse, it may compile to different things depending on which standard you compile to. If you don't want you colleagues to hate you, you should probably use:
#ifdef DEBUG
testCode();
#else
productionCode();
#endif
Or, better yet, if it really is test code then write a test.Yikes at those extra slashes. I've always preferred this way:
/**/
block one
/*/
block two
/**/
Simply add or remove the second slash to toggle.If you actually commit this code to a repository I later encounter, I will find you.
Cool trick. But there is no reason whatsoever to prefer this to a simple:
git co branch
Sorry. I've been burned one too may times from source-control-by-commenting-out.I use this "trick" a lot, but I'll never ever dare committing it; just for small and quick checks (of the "what if I use this other implementation for the loop?"). If I'm going to commit, if (1) or #if (1) (plus a small comment explaining why I resort to that) are better options and immediately understandable for any other team member.
Dijkstra is rolling in his grave right now
As a long-time C++ developer I wish people don't use any commenting tricks such as this, and I wish people don't use /* * / to comment their code, even if you develop in C. Instead, use // in modern C/C++.
The reason is that we often need to comment OUT code during development. In C++, the 4 choices are: (1) #ifdef, (2) bool dothis = false; if (dothis) { ... }, (3) /* * /, (4) // ...
Among them, I would say the best and least-confusing way to comment out a block of code is /* * /. #ifdef's are very confusing especially if you have multiple of them; // needs to be applied to every line so only works in small scale; if (dothis) doesn't work across multiple functions, and at a glance it is harder to discern whether this is a "comment-out", or a legit condition.
However, by using /* * / in your comments, you eliminate the possibility of commenting out blocks of code that contains /* * / comments. The problem, of course, is due to how the opening and closing symbols are matched.
I understand the criticism, it's not supposed to be _the_ way to toggle code in and out.
It's a TRICK, and that's all it will ever be.
However, it is the only way I know of that is not language specific for a particular dialect of C (#ifdef) that enables code to _not_ become a part of the compiled binary, or in JS terms - not available from the runtime.
Best regards -OP
This is a nice trick for quick comparison and thats it.
I actually use this a lot for performance comparison. This way it is easy to toggle between the olt d and the new implementation and see how long it takes to run the code 1000 time in javascript.
But I think anyone who check ins something likes this should be slaped.
This is a pretty cool trick, and I like that they shared it, but I don't think it's better than #ifdef!
Slightly relevant.. in an introductory C ourse at my university, we had to write a program that would tell us which version of the standard was used by the compiler. I can't think of a solution now but I'm sure it was based on different comment syntax.
This is ingenious but if used in a work environment should result in instant dismissal.
I've been using this for a while in my JS projects. I'm about to start my first job as a web developer for a medium-sized company so I'm glad I came here and read all the backlash to this technique!
This is terrifying!
Everyone with a syntax highlighting editor discovers this trick sooner or later. Why spoil the fun of the discovery and post the solution? :)
Cute trick, and lovely addition to the Obfuscated C code contest. Not for code to be used by anyone else (or by yourself after a few months).
Reminds me of another trick for debugging C++ programs. This allows you to dump data structures easily:
#define private public
include <foo.h>
Why would you do this instead of #if 0/#if 1? Has exactly the same effect, is a one character toggle (sure, it's editing one character rather than inserting/removing one character), and is much easier to read and remember.
This trick only works if there isn't a * / inside a string in commented out code (or there isn't a * / within the commented-out code itself in a language that allows such syntax, e.g. GrOOvy).
Rather than relying on syntax tricks, I'm sure most editors allow for block commenting or comment inversion.
For example with NERD Commentor vim plugin, you can toggle block comments with `<leader>c<space>`. Inverting comments is `<leader>ci`.
Fun related trick:
$ cat test.c
int compiler_supports_single_line_comments = 1 //*
//*/ 2
;
int main(void)
{
return compiler_supports_single_line_comments;
}
$ gcc test.c -o test && ./test ; echo $?
1
$ gcc -std=c89 test.c -o test && ./test ; echo $?
0
A much more elegant solution is to use Commentary[1] in Vim (and I'm sure there's an emacs equivalent too). Just use visual mode to select your text, hit `gc`, and voilà .
Dependency injection[1] would be preferable. Alter behavior based on defined environments (dev, test, staging, production), not whimsy.
In Eclipse you can hit CTRL+/ to comment and uncomment a block of code you have selected.
In Python:
#'''
some_code()
'''
some_other_code()
#'''
... with one character toggled, becomes: '''
some_code()
'''
some_other_code()
#'''
If you work with anyone who says "Don't do this," don't do this.Why don't you use macros?
Additionally, source control tools make deleting code much more convenient, no?
Let's not do this.
for any vim users I would recommend having a look at NERDcommenter, from the author of the very popular NERDtree
There's a slightly less-interesting Lua analog as well:
--[[
Code block one
--[=[ Explanatory comment here ]]
Code block too
--]=]
This seems . . . silly. I mean, honestly, do code editors besides Emacs not have something similar to comment-dwim?
Please just use if/else and a flag. It's much clearer and more maintainable.
I never liked this kind of code practice, makes it very difficult to read!
on non-gcc compilers this is valid
#define include_them 1 #if (include_them==1) #define hotcode /##/ #else #define hotcode #endif
void func (void) { hotcode printf("if included_them true, compile this line"); }
Is it bad that I first saw this trick on NerdParadise.com?
Or just select the text and hit CTRL + K, C
At least in my IDE anyway.
tldr: Don't do this.
I like 'Code Opinion Blogs'. Interesting insight into what people prefer when coding!
worse idea ever.
if you need to have branches in your code, learn how to use your source control environment. even cvs lets you branch your source tree.
DON'T DO THIS!
When your code base grows you will end with dozens of places to change your code, all of them error prone. More things you need to keep control means more potential problems.
There are 2 alternatives to this. For languages that have macros (C#, C, C++) use the #ifdef construct, as others suggested here. For languages that don't have them (Java, Perl) use a less error prone alternative:
static boolean Test = false; // Only one place to change code
...
if(Test) doTest();
The difference in this last technique is that since you have just one place to change code there is less chance for errors and the change is easier to automate.