Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Suppose I have some code

void some_block(void)
{
    some_statement_1();
    some_statement_2();
    some_statement_3();
}

Now I wish to out-comment statement #2, but here I never can find a way where it feels right. Usually I will do the following:

void some_block(void)
{
    some_statement_1();
//    some_statement_2();
    some_statement_3();
}

But now we got what looks like extra indentation of statement #2, yet it is arguable whether the indentation is still "correct". Alternatively we could do:

void some_block(void)
{
    some_statement_1();
    //some_statement_2();
    some_statement_3();
}

This looks a bit better, yet still somehow also wrong. However the following just looks misplaced:

void some_block(void)
{
    some_statement_1();
  //some_statement_2();
    some_statement_3();
}

How do you outcomment code and keep "correct" indentation?

Update

It seems most people who have provided answers did not answer my question, but simply state that it is bad practice to leave dead code around. While I completely agree with that, that was not the point of the question.

Right now I am facing a case of example where I have 5 helper functions, that manage 5 similar but slightly different structs for me. I use these helper functions to make my code easier to read.

But, at the moment (and for the next month probably) one of the structs are not going to be used. And I cannot commit anything which causes compiler warnings, so I have to outcomment that part of the code and commit it like that.

Also I forgot this last variant, which I think just looks plain wrong:

void some_block(void)
{
    some_statement_1();
//  some_statement_2();
    some_statement_3();
}
share|improve this question

4 Answers 4

up vote 22 down vote accepted

Format it so it sticks out and makes your code look so ugly that you'll be compelled to delete it. Comments are for comments and not for dealing with unnecessary code; that's the job of version control.

share|improve this answer
1  
Well said... I agree make it stick out so it bothers you enough to do something about it! –  Chris Dec 1 '10 at 13:29
    
I agree completely. Dead code is dead, whether it be commented out or not it should never stay in the code. –  Jason Dec 1 '10 at 13:33
2  
You WANT it to stick out and hit you in the eyes. You should add a comment block explaining WHY the code is commented out, as opposed to deleted completely. –  John R. Strohm Dec 1 '10 at 14:12
    
Agree. The only time I comment out code now is when I'm making sure that a unit test actually tests something. I comment out a line to "break" it, prove to myself that the test fails, then then put it back in. It's commented out for about 5 seconds. No time to worry about formatting. –  Scott Whitlock Dec 1 '10 at 14:38
    
What if the case is just temporary, like I need to comment out something because I am debugging a problem. –  Bjarke Freund-Hansen Dec 1 '10 at 15:52

What about this?

void some_block(void)
{
    some_statement_1();
    some_statement_3();
}
share|improve this answer

I hit Ctrl-/ and whatever my IDE does, I do that. Color highlighting tells me to ignore that line anyway (or to pay attention, depending on my context).

Before then, I used to replace spaces with slashes, keeping alignment:

block {
    codeline1;
//  codeline2;
    codeline3;
}

Obviously, just prepending slashes for flush-left code.

share|improve this answer
    
Same, I use whatever Ctrl + K + C puts. –  rmx Dec 1 '10 at 14:12
    
NetBeans does this by default, I leave it as it is. I don't think there's anything wrong with such indentation. –  Malcolm Dec 22 '11 at 21:07

As said by Alex in the emacs I use comment-region and let the emacs handle it. (Please no -ve votes from vi user for using emacs. :-) ). Once I am sure about my code then I delete it. The point is leave it to your favorite editor and get used to what editor does.

share|improve this answer
2  
-1 for using emacs, +1 for mentioning vi, so let's call it even ;-) –  user281377 Dec 1 '10 at 14:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.