开发者

what should comment say when programmer is aware that it is bad style but necessary

In short I have code like this. I know its bad style but have given it a lot of thought and alternatives are worse. What should I say in a comment and where should I put it?

while(1)
{
 开发者_如何学Python   if(x+y == z)//some comparison
    {
        …//do something
    }
    else
    {
        break;
    }
}


Assuming that there is more code involved, so the obvious

while(x+y == z)
{
}

is not possible, you could use an additional variable to flag the loop status.

do_loop = 1;
while (do_loop)
{
    // more code
    do_loop = (x+y == z);
    if (do_loop)
    ...
}

It offers more possibilities, esp. if deeper nesting is involved, since break; will only leave the innermost loop. Of course you should use a more exact naming for the condition instead of a generic do_loop, e.g. coords_are_equal.

If all alternatives are worse, including this, then just comment it as it is: "All alternatives are found to be worse than this."


You might consider rewriting this as

while (x+y == z) {
   // Do something...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜