Click to See Complete Forum and Search --> : How to comment large blocks of code?
Martin O
January 17th, 2007, 04:27 PM
Hi All,
In C++, I could get around not being able to nest comments (the /* */ type) by using #if 0....#endif. So if I want to comment out a big section of code that has comments, I could do it.
How could I do the same in Java?
Thanks
-Martin
Tab
January 17th, 2007, 05:15 PM
There's no real equivalent, sorry. Believe me, I wish there was. I usually run // down the side, as in:
// This is the first line of code,
// this is the second
// this is the third
And the // takes precedence over the /* */, the drawback is that you have to run them all the way down the side. On the other hand, in C, I used to hate trying to debug code only to find someone had if-def'd it out 100 lines back.
Martin O
January 17th, 2007, 06:40 PM
Thanks Tab,
Yeah, when I do the #ifdef 0 thing its usually just temporary. Though it would be cool if VC++ had an option to treat #ifdef 0 as a comment and syntax-color the text comment colored.
Tab
January 17th, 2007, 07:58 PM
Although what you could do is wrap it like this:
if (0)
{
// All the code you don't want to run
}
Depending on what you're breaking apart, you might have to play with the parens, but that's what I usually do in a pinch. It still will try to compile, though - since it's not a true preprocessor statement, but if you can live with that, it'll take it out of the running, so to speak.
masher
January 17th, 2007, 08:31 PM
This works for me
if (false)
{
// Don't run this code
}
wildfrog
January 17th, 2007, 09:11 PM
There's no real equivalent, sorry. Believe me, I wish there was. I usually run // down the side, as in:
// This is the first line of code,
// this is the second
// this is the third
And the // takes precedence over the /* */, the drawback is that you have to run them all the way down the side. On the other hand, in C, I used to hate trying to debug code only to find someone had if-def'd it out 100 lines back.Several of todays IDEs has added features to make this somewhat easier. You simply select the code that you want to comment out (or in), then click a button in the toolbar and the IDE will fill in (or remove) those '//'.
- petter
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.