views:

616

answers:

9
/*/ comment here
do some thing.
/*/
do some thing.
//*/

Why people write code like that? Is this a good practice?

+2  A: 

Everyone has there own way of doing things... I'm guessing they did this so all you have to do is add a / to the first comment and remove the last / and then it toggles the comment block to the other set of instructions.

EDIT: Actually all you have to do is add a / to the first comment and then remove it to toggle it back.

Kane Wallmann
+21  A: 

It's usually only used when testing something out for the moment. That is, you should never commit code like that to version control, because it can be confusing.

For example, if you are testing two different computation methods, you can use this to switch between them. Personally I have rarely done this, if at all.


For those that don't know, you can toggle between the two code sections by adding one forward slash:

/*/ comment here
do some thing.
/*/
do some thing else.
//*/

//*/ comment here
do some thing.
/*/
do some thing else.
//*/
GMan
This styles smacks of something more permanent, though, given the attention to the ease of doing/undoing the various blocks. When testing, I'd be more inclined to simply comment out/uncomment the entire block using // on each line. A good IDE should make this trivial as well.
tvanfosson
+1 for "never commit...code like that", though.
tvanfosson
while that is neat if your stuck using pico, I still dislike it.
Simeon Pilgrim
Yea, I tend to just copy out blocks, too. I think for a while I shall try to apply this technique so I can get a more fair opinion on it.
GMan
Absolutely not. This is terrible even for what you describe.
Martin York
Quite. To switch between two blocks of code I use #if 1 ... #else ... #endif. Then toggle between 1 and 0. I really don't need my syntax highlighter to tell me which version is enabled.
Steve Jessop
+14  A: 

I would rather do

#ifdef DOIT_ONE_WAY
do one way
#else
do another way
#endif

but that's a matter of taste

Arkadiy
Even thats bad practive. You should use the #if block in header files to set up macros. The code then uses the macros (like functions) without any #if so that real code is aeasy to read. The macros then just deal with the implementation details.
Martin York
Certainly bad practice for real macros, but a better alternative to commenting out the code. Of course, tvanfosson is right - dead code should not be there in the first place.
Arkadiy
+17  A: 

Crappy practice. Dead code has no business hanging around in any production quality code. If there really are situations where the old, dead code would apply, then it should be refactored into something that can be turned on/off using configuration not recompilation.

tvanfosson
+1 dead code should die.
Mercer Traieste
The question doesn't actually say it is in production code. I think I may have used this sort of thing in bug reports.
Tom Hawtin - tackline
@Tom Hawtin-tackline - Typically when one asks if this is good practice, they are not asking if there is any corner case where it might be acceptable. I'm assuming that the OP wants to know if this is something that should regularly be done. IMO, it shouldn't.
tvanfosson
+3  A: 

That confuses me and would take me time to parse, so no I don't think it's good practice.

Any ease of toggling mentioned in Kane's answer is not worth it, I think. Ease of commenting should be taken care of by the developers browser.

To me this is just confusing and definitely not standard.

jskulski
A: 

It's just an easy way to switch between two blocks of code (as Kane Wallmann said).

It's probably not great to leave it in production code (just delete it and get it back from source control if you need it), but while developing it's a handy way of being able to quickly toggle two implementations (or stub out some code etc).

RodeoClown
+1  A: 

I think this and the #ifdef method Arkadiy mentions are bad, as a multi-file search for do something both look like the code is live, while it may not be depending on previous lines.

If the code is valid #ifdef platform dependence sure do that,

but if it just testing code, etc I much prefer highlighting the lot and inserting the C++ // comment, which in VisualStudio is Ctrl-K, C

Simeon Pilgrim
A: 

Not not necessarily bad practice, but expect mixed responses from someone who reads your code. For example, I think its really cool but would never commit code like that to the repository.

Nippysaurus
+2  A: 

That's confusing and lacks programming aesthetics. A good way to do it is using the C preprocessor and coding it like:

#if 0

code block disabled

#endif

code block enabled
piotr
Better yet, #ifdef TEMPORARILY_REMOVED_BECAUSE_I_AM_TESTING_SAYS_JAF code block disabled #endifThen when you forget to take the block out before committing, the next guy will know why it was disabled and whether he should re-enable it.
Jeremy Friesner