If you DO want to do this either:
a) delete the code. It'll be in version control anyway right? You do do version control right?
b) put it under a NAMED feature flag. Test is not a suitable name. Put it in a separate file which is conditionally compiled into the build with #ifdef SHINY_FEATURE shiny_feature(); #endif
The only time I write comments is when:
1. Someone fucked up big and shipped an iPad app or something that relies on an error in the API or something. Can't force users to upgrade iPad apps, especially on older ones who are fearful of the app breaking or getting worse, so you end up with these massive comments fully explaining the problem, the hack that solves it, the conditions that need to be met in order for the hack to be solved (for example, "check google analytics to see if the number of people using version X of the iPad app or lower is less than 1%, if it is, replace the bellow hack with the following solution"), and possibly other hacks investigated and the reasons that they were declined. The reason you do this in the code rather than just in the commit message is that it protects the message from getting watered down as people change the code. I assume that someone is in a massive hurry when they are reading code and massive wtfs should be explained without further investigation.
2. When the performance of the obvious solution is terrible and the efficient solution is complex. Especially when math is involved (for example, the core match method of a distributed recommendation engine).
i.e:
// This is how it should be // foo(...) // But, since the code is horribly broken, we'll have to do: bar()
From "97 Things Every Programmer Should Know" http://programmer.97things.oreilly.com/wiki/index.php/Commen...
> Commented-out code is not executable code, so it has no useful effect for either reader or runtime. It also becomes stale very quickly. Version-related comments and commented-out code try to address questions of versioning and history. These questions have already been answered (far more effectively) by version control tools.
If only digging code up from a previous version was as easy as uncommenting it.
However though, please read https://news.ycombinator.com/item?id=5626456 and summary is:
It is a trick, not a silver bullet. It prevents code from being compiled to the binary, in a portable - yet hacky - way.
#define DEBUG 1
#if DEBUG
test_code()
#else
real_code()
#endif
Much more readable and the DEBUG macro can be defined in a common header, so all of the code can share it.The comment trick isn't useless during development, but I'd see myself using a macro rather than trying to remember just where a slash goes.
But there are equivalents for any programming language, but others here have already touched on them.
I'm not sure what SQL has built in, but I'd just use the C preprocessor if I needed that. However, I'd much rather not muck with my queries like that, because that will cause nasty surprises in production.
And I don't eat carbs, so your off the hook for a cake.
This is an interesting idea! What was the problem you were solving that required this?
---
I think actually the C preprocessor would solve a lot of the problems that, say, SCSS solves too:
// put these in a header file, 'cssutil.h'
#define HASH #
#define f(x) x
#define COLOR(c) f(HASH)c
#define PREFIXED(x, y) \
-webkit- ## x : y;\
-moz- ## x : y;\
-o- ## x : y;\
x : y
#define SHADOWED PREFIXED(box-shadow, 10px)
// then in your css
#include "cssutil.h"
#define THEME_BG COLOR(fff)
div.main {
background: THEME_BG;
SHADOWED;
}
There we go, pristine(ish) CSS, unsullied by magic constants and messy prefixes! (note that my preprocessor-fu is weak, I don't know how to do variadic prefixes)Different languages have different optimization techniques for removing code during compilation. Java, for example [1], can optimize out based on static boolean variables. I recommend learning more about each individual language and what it offers to get the best out of it.
[1] http://stackoverflow.com/questions/1344270/java-preprocessor
More sins have been committed in the cause of "Hey, this will save a few keystrokes" than . . . well, a lot of sins, okay?
I have no trouble doing this in the heat of bring-up or a debugging session, but I'd never check it in, and I hope that none of my cow-orkers ever will, either.
But he's right. It's a slippery slope to bad things when keystroke convenience takes precedence over clarity. We're human after all; it's easy to overlook something critical (especially when there's not another set of eyes going over it) and spend a lot more time and energy hunting for it later.
Alternatively if you don't need to check in your comments then your IDE should have a "comment selection" option (CTRL K, C chord in VS)
Before a commit I'll sweep through and delete the intermediate code blocks that I've commented out. If I use pre-compiler flags to comment out the code, it's easy to miss it in the sweep. Because of this I never use the pre-compiler flags for something I don't want to get committed.
Well, it's really only error prone if your editor doesn't have syntax highlighting. Seeing a whole swatch of code turn into the comment color pretty much tells you what's going on.
Still, I think personally I'd rather do an "#if 0". It's still only 1 character to toggle the code on or off...
My only issue using this with things like JS, won't all that code still be loaded into memory giving a misrepresentation of the memory profile of your app?
Yes it will. The alternative for that might be a modular design with very loose coupling and using Inversion of Control. Then, the only change you would need to make would be in the Inversion of Control configuration to load either the test code or the production code. You could place all the debug and trace code inside a single unit and use an empty stub in production. This would avoid sending the test code to the client on the production environment.
I am not an expert in JS, please correct me if this is wrong. I'd do something like this (for checking pre-conditions in method calls):
function Assert(condition, message ){} // this module is loaded/included in production only
function Assert(condition, message) { if(!condition) alert(message);} // this module is loaded/included in tests only
Better would be to collect that code into one or two files at can be included when needed. I would imagine that's really only a concern when downloading code because the compiler should throw dead code ("if false...") away.
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.