C语言 为什么要使用#if 0 来注释块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3630601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:21:54  来源:igfitidea点击:

Why use #if 0 for block commenting out?

ccompilationc-preprocessor

提问by Jason R. Mick

Reverse engineering code and I'm kind of appalled at the style, but I wanted to make sure there's no good reason for doing these things....

逆向工程代码,我对这种风格感到震惊,但我想确保做这些事情没有充分的理由......

Is it just me or is this a horrible coding style

是只有我还是这是一种可怕的编码风格

if ( pwbuf ) sprintf(username,"%s",pwbuf->pw_name);
else sprintf(username,"%d",user_id);

And why wrap code not intended for compilation in an

以及为什么将不用于编译的代码包装在

#if 0
....
#endif

Instead of comments?

而不是评论?



EDIT: So as some explained below, this is due to the possibility to flummox /* */ which I didn't realize.

编辑:正如下面的一些解释,这是由于我没有意识到flummox /* */ 的可能性。

But I still don't understand, why not just use your programming environment tools or favorite text editor's macro's to block comment it out using "//"

但我还是不明白,为什么不直接使用你的编程环境工具或最喜欢的文本编辑器的宏来阻止使用“//”注释掉它

wouldn't this be MUCH more straightforward and easy to know to visually skip?

这不是更直接,更容易知道视觉跳过吗?



Am I just inexperienced in Cand missing why these things might be a good idea -- or is there no excuse, and I'm justified in feeling irritated at how ugly this code is?

我是否只是缺乏C 方面的经验并且不知道为什么这些东西可能是一个好主意——或者没有任何借口,我有理由对这段代码有多丑感到恼火?

回答by jkerian

#if 0is used pretty frequently when the removed block contains block-comments

#if 0当删除的块包含块注释时经常使用

I won't say it's a good practice, but I see it rather often.

我不会说这是一个好的做法,但我经常看到它。

The single line flow-control+statement is easy enough to understand, although I personally avoid it (and most of the coding guidelines I've worked under forbid it)

单行 flow-control+statement 很容易理解,尽管我个人避免使用它(并且我工作过的大多数编码指南都禁止它)

BTW, I'd probably edit the title to be somewhat useful "Why use #if 0 instead of block comments"

顺便说一句,我可能会将标题编辑为有点有用“为什么使用 #if 0 而不是块注释”

If you have the following

如果您有以下情况

#if 0
        silly();
        if(foo)
           bar();
        /* baz is a flumuxiation */
        baz = fib+3;
#endif

If you naively replace the #if 0/#endifwith /* */, that will cause the comment to end right after flumuxiation, causing a syntax error when you hit the */in the place of the #endifabove..

如果你天真地更换#if 0/#endif/* */,将导致注释flumuxiation之后结束,导致一个语法错误,当你打*/在的地方#endif上面..

EDIT: One final note, often the #if 0syntax is just used while developing, particularly if you have to support multiple versions or dependencies or hardware platforms. It's not unusual for the code to be modified to

编辑:最后一点,#if 0语法通常只是在开发时使用,特别是如果您必须支持多个版本或依赖项或硬件平台。将代码修改为并不罕见

#ifdef _COMPILED_WITHOUT_FEATURE_BAZ_
    much_code();
#endif

With a centralized header defining (or not) hundreds of those #define constants. It's not the prettiest thing in the world, but every time I've worked on a decent sized project, we've used some combination of runtime switches, compile-time constants (this), compile-time compilation decisions (just use different .cpp's depending on the version), and the occasional template solution. It all depends on the details.

使用集中式标头定义(或不​​定义)数百个 #define 常量。这不是世界上最漂亮的东西,但每次我处理一个体面大小的项目时,我们都会使用运行时开关、编译时常量(this)、编译时编译决策(只需使用不同的 . cpp 取决于版本),以及偶尔的模板解决方案。这一切都取决于细节。

While you're the developer just getting the thing working in the first place, though... #if 0is pretty common if you're not sure if the old code still has value.

虽然您是开发人员,但首先要让事情正常工作,但是……#if 0如果您不确定旧代码是否仍然有价值,这很常见。

回答by Jeff Dege

Comments are comments. They describe the code.

评论就是评论。他们描述了代码。

Code that's being excluded from compilation is code, not comments. It will often include comments, that describe the code that isn't being compiled, for the moment.

从编译中排除的代码是代码,而不是注释。它通常会包含注释,用于描述目前尚未编译的代码。

They are two distinct concepts, and forcing the same syntax strikes me as being a mistake.

它们是两个不同的概念,强制使用相同的语法在我看来是一个错误。

回答by bta

Besides the problem with C-style comments not nesting, disabling blocks of code with #if 0has the advantage of being able to be collapsed if you are using an editor that supports code folding. It is also very easy to do in any editor, whereas disabling large blocks of code with C++-style comments can be unwieldy without editor support/macros.

除了 C 样式注释不嵌套的问题之外,#if 0如果您使用支持代码折叠的编辑器,禁用代码块的优点是能够折叠。在任何编辑器中也很容易做到,而在没有编辑器支持/宏的情况下,禁用带有 C++ 样式注释的大块代码可能会很笨拙。

Also, many #if 0blocks have an elseblock as well. This gives an easy way to swap between two implementations/algorithms, and is arguably less error-prone than mass-commenting out one section and mass-uncommenting another. However, you'd be better off using something more readable like #if DEBUGin that event.

此外,许多#if 0块也有一个else块。这提供了一种在两个实现/算法之间交换的简单方法,并且可以说比批量注释一个部分并批量取消另一个部分更不容易出错。但是,您最好#if DEBUG在该事件中使用更具可读性的内容。

回答by Aaron Klotz

As far as block commenting using //is concerned, one reason that I can think of is that, should you check that code into your source control system, the blame log will show you as the last editor for those lines of code. While you probably want the commenting to be attributed to you, at the same time the code itself is also being attributed to you. Sure, you can go back and look at previous revisions if you need to check the blame log for the "real" author of the code, but it would save time if one preserved that information in the current revision.

就使用块注释//而言,我能想到的一个原因是,如果您将该代码检查到您的源代码控制系统中,则blame 日志将显示您作为这些代码行的最后一个编辑器。虽然您可能希望评论归于您,但同时代码本身也归于您。当然,如果您需要检查代码“真正”作者的责任日志,您可以返回并查看以前的修订版本,但如果在当前修订版本中保留该信息,则会节省时间。

回答by Steven Schlansker

That's pretty idiomatic C right there. I don't see what's so wrong with it. It's not a beautiful piece of code but it's easy to read and is clear what's going on and why, even without context.

这就是非常惯用的 C 语言。我不明白这有什么问题。这不是一段漂亮的代码,但它很容易阅读,并且即使没有上下文也很清楚发生了什么以及为什么。

The variable names could be better, and and it'd probably be safer to use snprintfor perhaps strncpy.

变量名称可能会更好,而且使用可能更安全,snprintf或者strncpy.

If you think it could be better, what would you prefer it look like?

如果你认为它可以更好,你更喜欢它是什么样的?

I might make a slight change:

我可能会稍作改动:

char username[32];
strncpy(username, 30, (pwbuf ? pwbuf->pw_name : user_id));
username[31] = '
if (pwbuf) sprintf(username,"%s",pwbuf->pw_name);
else       sprintf(username,"%d",user_id);
';

回答by Oliver Charlesworth

Obviously, everyone has their own opinions on this sort of thing. So here's mine:

显然,对于这种事情,每个人都有自己的看法。所以这是我的:

I would neverwrite code like the above, and would think less of anyone who did. I can't count the number of times people think it's ok to get away without scope braces, and then been bitten by it.

永远不会写出像上面那样的代码,也不会考虑任何这样做的人。我数不清有多少次人们认为没有范围大括号可以逃脱,然后被它咬伤。

Putting the control statement on the same line as the code block is even worse; the lack of indenting makes it harder to see the flow control whilst reading. Once you've been coding for a few years, you get used to being able to read and interpret code quickly and accurately, so long as you can rely on certain visual cues. Circumventing these cues for "special cases" means that the reader has to stop and do a double-take, for no good reason.

把控制语句和代码块放在同一行就更糟了;缺少缩进使得阅读时更难看到流量控制。一旦你已经编码了几年,你就会习惯于能够快速准确地阅读和解释代码,只要你能依靠某些视觉线索。为“特殊情况”规避这些线索意味着读者不得不停下来重新审视,没有充分的理由。

#if (0), on the other hand, is ok during development, but should be removed once code is "stable" (or at least replace 0with some meaningful preprocessor symbol name).

#if (0),另一方面,在开发过程中是可以的,但是一旦代码“稳定”就应该删除(或者至少0用一些有意义的预处理器符号名称替换)。

回答by Sanjay Manohar

points above noted. But monitors being widescreen and all, these days, I sort of don't mind

上面提到的几点。但是显示器是宽屏的,这些天,我有点不介意

if ( pwbuf ) sprintf(username,"%s",pwbuf->pw_name);
else sprintf(username,"%d",user_id);

Always seem to have too much horizontal space, and not enough vertical space on my screen!

我的屏幕上似乎总是有太多的水平空间,而垂直空间却不够!

Also, if the code block already has preprocessor directives, don't use #if 0; if the code already has block comments, don't use /* */. If it already has both, either resort to an editor that has a ctrl+/, to comment out lots of lines. If not, you're stuffed, delete the code outright!

另外,如果代码块已经有预处理器指令,不要使用#if 0; 如果代码已经有块注释,请不要使用/* */. 如果它已经有两个,要么求助于一个带有ctrl+的编辑器/,以注释掉很多行。如果没有,你被塞满了,直接删除代码!

回答by Paul Nathan

#if 0
....
#endif

Idiomatic and concise. If it got touched more than 2 or 3 times, I would bracket and next-line it. It's not very maintainable if you add logging information or other conditions.

地道和简洁。如果它被触摸超过 2 或 3 次,我会将它括起来并放在下一行。如果添加日志信息或其他条件,则不太容易维护。

/* line comment */
...
/* line comment again */

Good to turn on blocks of debug code or not. Also, would avoid compilation errors related to trying to block comment this sort of thing out:

是否打开调试代码块很好。此外,将避免与试图阻止评论此类事情相关的编译错误:

if (pwbuf) 
  sprintf(username,"%s",pwbuf->pw_name); 
else 
  sprintf(username,"%d",user_id); 

Since C block comments don't nest.

由于 C 块注释不嵌套。

回答by smdrager

Woah there! Don't overreact...

哇哦!不要反应过度...

I would call it sloppier for more the inconsistant spacing than anything else. I have had time where I found it better to put short statements on the same line as their IF, though those statements are stretching it.

对于不一致的间距,我认为它比其他任何东西都更草率。我有时间发现将简短的语句与它们的 IF 放在同一行更好,尽管这些语句会拉伸它。

The inline style is better for vertical brevity... could easily be broken into 4, more lines

内联样式更适合垂直简洁......可以很容易地分成 4 行,更多行

if (pwbuf) 
{
  sprintf(username,"%s",pwbuf->pw_name); 
}
else
{ 
  sprintf(username,"%d",user_id); 
}

Personally I hate the next style since it so long-winded, making it difficult to skim a file.

我个人讨厌下一种风格,因为它冗长,很难浏览文件。

if (strcmp(s, "foo") == 0)
{
    bitmap = 0x00000001UL;
    bit = 0;
}
else if (strcmp(s, "bar") == 0)
{
    bitmap = 0x00000002UL;
    bit = 1;
}
else if (strcmp(s, "baz") == 0)
{
    bitmap = 0x00000003UL;
    bit = 2;
}
else if (strcmp(s, "qux") == 0)
{
    bitmap = 0x00000008UL;
    bit = 3;
}
else
{
    bitmap = 0;
    bit = -1;
}

回答by Secure

Very occasionally I use the more concise style when it supports the symmetry of code and the lines don't get too long. Take the following contrived example:

偶尔我会使用更简洁的风格,因为它支持代码的对称性并且行不会太长。看下面这个人为的例子:

if      (strcmp(s, "foo") == 0) { bitmap = 0x00000001UL; bit = 0;  }
else if (strcmp(s, "bar") == 0) { bitmap = 0x00000002UL; bit = 1;  }
else if (strcmp(s, "baz") == 0) { bitmap = 0x00000003UL; bit = 2;  }
else if (strcmp(s, "qux") == 0) { bitmap = 0x00000008UL; bit = 3;  }
else                            { bitmap = 0;            bit = -1; }

and the concise version:

和简洁版本:

##代码##

Bugs are much more likely to jump straight into your face.

虫子更有可能直接跳到你的脸上。

Disclaimer: This example is contrived, as I said. Feel free to discuss the use of strcmp, magic numbers and if a table based approach would be better. ;)

免责声明:正如我所说,这个例子是人为的。随意讨论 strcmp 的使用、幻数以及基于表的方法是否更好。;)