문제

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();
}
도움이 되었습니까?

해결책

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.

다른 팁

What about this?

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

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.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 softwareengineering.stackexchange
scroll top