为什么使用#if 0进行块注释? [英] Why use #if 0 for block commenting out?

查看:257
本文介绍了为什么使用#if 0进行块注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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

为什么要包装不适合在$ p
$ b $中编译的代码b

  #if 0 
....
#endif

代替评论吗?






编辑:因此,如以下解释,这是由于可能会出现flummox / * * /这是我没有意识到的可能性。编辑器的宏使用 //阻止注释掉它。



这不是更直接,更容易直观地跳过吗?






我只是对 C 没有经验,却错过了为什么这些东西可能是个好主意的原因-或没有任何借口,

解决方案

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



我不会说这很好练习,但我经常看到它。



单行流控制+陈述很容易理解,尽管我个人避免这样做(以及大多数编码准则)我一直在禁止下工作)



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



如果您具有以下内容

  #if 0 
silly( );
if(foo)
bar();
/ * baz是fluxuxiation * /
baz = fib + 3;
#endif

如果您天真地替换 #if 0 / #endif / * * / ,将导致注释在flumuxiation,当您在上面的 #endif 处按下 * / 时,导致语法错误。 p>

编辑:最后一条注释,通常在开发时使用 #if 0 语法,尤其是在您必须支持的情况下多个版本或依赖项或硬件平台。将代码修改为

  #ifdef _COMPILED_WITHOUT_FEATURE_BAZ_ 
much_code()并不罕见。
#endif

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



虽然您是开发人员,但只是将事情放在首位,但是... #if 0 非常不确定,如果您不确定旧代码是否仍然有价值。


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.

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 C and 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?

解决方案

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

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)

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

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/#endif with /* */, that will cause the comment to end right after flumuxiation, causing a syntax error when you hit the */ in the place of the #endif above..

EDIT: One final note, often the #if 0 syntax 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

#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.

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

这篇关于为什么使用#if 0进行块注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆