嵌套条件表达式----好主意/坏主意? [英] Nested conditional expressions ---- good idea/bad idea?

查看:41
本文介绍了嵌套条件表达式----好主意/坏主意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面你将看到一个嵌套的条件表达式的例子,这是我的同事喜欢的b $ b。他声称,使用多层if-else-if结构更有效率。而且,我们的复杂性分析器

工具据说不会捡到它。这真的更有效吗?

就个人而言,我发现这种编码风格非常神秘,误导和

容易出错。


我相信我已经通过多次文本替换从这个

编码示例中删除了所有专有权的痕迹。


--NS

/ ********************************************* ** *********** /

#include< stdio.h>


#define QWER 0x1000

#define TYUI 0x2000

#define ASDF 0x4000

#define GHJK 0x8000

#define ZXCV 0x10000

#define BNML 0x20000

void

print_value(int code,int type)

{

printf( " ==>%s,输入%02X \ n",

(代码== QWER?" QWER":

code == TYUI?" ; TYUI":

code == ASDF?" ASDF":

code == GHJK?" GHJK":

code = = ZXCV?ZXCV:

代码== BNML? BNML:????,

type);

}

main(void)

{

print_value(BNML,256);

print_value(ZXCV,512);

print_value(GHJK,1024) ;

print_value(ASDF,768);

print_value(TYUI,128);

print_value(QWER,64);

print_value(BNML | QWER,32);

}

Below you will see an example of a nested conditional expression that
this colleague of mine loves. He claims that it is more efficient that
a multi-level if-else-if structure. Moreover, our complexity analyzer
tool supposedly does not pick it up. Is it really more efficient?
Personally I find this coding style extremely cryptic, misleading and
error-prone.

I believe that I have removed all traces of proprietary-ness from this
coding example by multiple text substitutions.

--NS
/************************************************** ***********/
#include <stdio.h>

#define QWER 0x1000
#define TYUI 0x2000
#define ASDF 0x4000
#define GHJK 0x8000
#define ZXCV 0x10000
#define BNML 0x20000
void
print_value(int code, int type)
{
printf(" ==> %s, Type %02X \n",
(code == QWER ? "QWER":
code == TYUI ? "TYUI":
code == ASDF ? "ASDF":
code == GHJK ? "GHJK":
code == ZXCV ? "ZXCV":
code == BNML ? "BNML": "????"),
type);
}
main(void)
{
print_value(BNML, 256);
print_value(ZXCV, 512);
print_value(GHJK, 1024);
print_value(ASDF, 768);
print_value(TYUI, 128);
print_value(QWER, 64);
print_value(BNML|QWER, 32);
}

推荐答案


ni*************@yahoo.com 写道:
下面你将看到一个嵌套的条件表达式的例子,这是我的同事所喜欢的。他声称多层if-else-if结构更有效
。此外,我们的复杂性
分析工具据称不会捡起来。它真的更有效吗?


效率的概念不是C语言问题。

我个人觉得这种编码风格非常神秘,误导和容易出错。




我相信我已经通过多次文本替换从
这个编码示例中删除了所有私有的痕迹。

-

/ ********************************* ***************** *********** /
#include< stdio.h>

#定义QWER 0x1000
#define TYUI 0x2000
#define ASDF 0x4000
#define GHJK 0x8000
#define ZXCV 0x10000
#define BNML 0x20000

void
print_value(int code,int type)
{/> printf(" ==>%s,Type%02X \\\
",
(code == QWER?QWER:
代码== TYUI?" TYUI":
代码== ASDF?" ASDF":
代码== GHJK?" GHJK":
code == ZXCV?" ZXCV":
code == BNML?" BNML":" ????"),
type);
}

main(无效)
{
print_value(BNML,256);
print_value(ZXCV,512);
print_value(GHJK,1024); < br / > print_value(ASDF,768);
print_value(TYUI,128);
print_value(QWER,64);
print_value(BNML | QWER,32);
}
Below you will see an example of a nested conditional expression that
this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our complexity analyzer tool supposedly does not pick it up. Is it really more efficient?
The idea of efficiency is not a C language issue.
Personally I find this coding style extremely cryptic, misleading and
error-prone.
it is

I believe that I have removed all traces of proprietary-ness from this coding example by multiple text substitutions.

--NS
/************************************************** ***********/
#include <stdio.h>

#define QWER 0x1000
#define TYUI 0x2000
#define ASDF 0x4000
#define GHJK 0x8000
#define ZXCV 0x10000
#define BNML 0x20000
void
print_value(int code, int type)
{
printf(" ==> %s, Type %02X \n",
(code == QWER ? "QWER":
code == TYUI ? "TYUI":
code == ASDF ? "ASDF":
code == GHJK ? "GHJK":
code == ZXCV ? "ZXCV":
code == BNML ? "BNML": "????"),
type);
}
main(void)
{
print_value(BNML, 256);
print_value(ZXCV, 512);
print_value(GHJK, 1024);
print_value(ASDF, 768);
print_value(TYUI, 128);
print_value(QWER, 64);
print_value(BNML|QWER, 32);
}




效率取决于C范围以外的东西(编译器,

处理器等)。我怀疑会有什么有意义的效率从这样做获得
,甚至可能效率更低(混淆了b
编译器的优化阶段?!?)。我个人永远不会写这样的代码,而且我质疑程序员的动机

(炫耀?让他们的代码更难以理解?)。 br />

[OT]如果这是一个资源稀缺的嵌入式系统,你需要每隔纳秒的时间和内存,然后运行测试(不要)通过查看C代码来猜测

- 这是不可能的!!!!)。在

软件工程流程的早期,您的公司可能对可能的粗略进行了彻底的研究。粒度优化实践将为您的特定实现提供有效的代码。坚持

这个,但不要玩C源中的小细节(保持

代码看起来干净)。在你的设计周期即将结束时,如果你发现你仍然需要更多的优化(如果你准备好了,那你就不应该这样做了),然后对它进行微调(很好) - 粒度优化)。但是这些天的编译器非常擅长对你进行这些优化,所以只有试试。如果其他一切都失败了你的调整(如果你计划得当,它不应该是b $ b不应该有)。此外,通过构建适当的接口(例如为所有内容制作客户

接口,包括数据类型,准备好
优化。想象一下编码

是一切都完成了,但是你意识到使用int而不是整个

部分的代码会将速度提高2倍而不会破坏

代码...你呢想换1行或者...你得到

图片。



Efficiency depends on things outside the scope of C (compiler,
processor, etc.). I doubt there would be any meaningful efficiency
gain from doing this, it might even be more inefficient (confuses
compiler''s optimization stages?!?). I personally would never write
code like this, and I question the motives of programmers that do
(showing off? making their code harder for other to understand?).

[OT] If this is an embedded system with scarce resources, and you need
every nanosecond of time and bit of memory, then run tests (don''t guess
by looking at C code- that is out of the question!!!!). Early on in the
software engineering process, your company probably did a thorough
study of possible "coarse" granular optimization practices that will
help make efficient code for your particular implementation. Stick to
this, but don''t play around with little details in the C source (keep
code looking clean). Near the end of your design cycle, if you find you
still need more optimization (which you shouldn''t if you prepared
properly) then fine tune it (fine-granular optimizations). But
compilers these days are quite good at doing these optimizations for
you, so only "try" your tweaks if everything else fails (which it
shouldn''t have, if you planned properly). Also, prepare for
optimizations by building proper interfaces (like making custome
interfaces for everything, including data types.. imagine that coding
is all done, but you realize that using int instead long for a whole
section of code will increase speed by a factor of 2 without breaking
the code... do you want to make a 1 line change or ... you get the
picture).


ni ************* @ yahoo.com 写道:

下面你将看到我的同事喜欢的嵌套条件表达式的例子。他声称,多级if-else-if结构更有效。此外,我们的复杂性分析工具据称不会捡起来。它真的更有效吗?我个人觉得这种编码风格非常神秘,误导和容易出错。

我相信我已经通过多个文本从这个编码示例中删除了所有专有的痕迹换人。


.... snip ...


在进行了一些不错的缩进(见下面的签名)后,你有:

printf(" ==>%s,输入%02X \ n",
(代码== QWER?" QWER":
代码== TYUI?" TYUI" ;:
代码== ASDF?" ASDF":
代码== GHJK?" GHJK":
代码== ZXCV?" ZXCV":
代码= = BNML?BNML:????,
类型);

Below you will see an example of a nested conditional expression
that this colleague of mine loves. He claims that it is more
efficient that a multi-level if-else-if structure. Moreover, our
complexity analyzer tool supposedly does not pick it up. Is it
really more efficient? Personally I find this coding style
extremely cryptic, misleading and error-prone.

I believe that I have removed all traces of proprietary-ness from this
coding example by multiple text substitutions.
.... snip ...

After putting some decent indentation in (see sig below) you have:
printf(" ==> %s, Type %02X \n",
(code == QWER ? "QWER":
code == TYUI ? "TYUI":
code == ASDF ? "ASDF":
code == GHJK ? "GHJK":
code == ZXCV ? "ZXCV":
code == BNML ? "BNML": "????"),
type);




这里的另一个选择是临时的,一个if else链:


char * tmp;


if(code == QWER)tmp =" QWER" ;;

else if(code == TYUI)tmp =" TYUI" ;;

else if(code == ASDF)tmp =" ASDF";

else if(code == GHJK)tmp =" GHJK" ;;

else if(code == ZXCV)tmp =" ZXCV";

else if( code == BNML)tmp =" BNML&qu ot ;;

else tmp =" ????" ;;

printf(" ==> %s,类型%02X \ n",tmp,type);


现在,哪个更干净,更具可读性?无视效率,我认为你的同事在这种情况下做得很好。

我会用的几个地方?运营商,但这是他们中的一个。但是,我可能会将它隔离在例如:


char * typetostring(enum type t);


这将允许


printf(" ==>%s,Type%02X \ n",typetostring(code),type);


你还有暂时的,它只受功能

的影响。但是,如果在其他地方需要

,你可以重复使用翻译例程。


-

"如果你想发布一个后续行动通过groups.google.com,不要使用

破损的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。 - Keith Thompson



The other choice here is a temporary, and an if else chain:

char *tmp;

if (code == QWER) tmp = "QWER";
else if (code == TYUI) tmp = "TYUI";
else if (code == ASDF) tmp = "ASDF";
else if (code == GHJK) tmp = "GHJK";
else if (code == ZXCV) tmp = "ZXCV";
else if (code == BNML) tmp = "BNML";
else tmp = "????";
printf(" ==> %s, Type %02X \n", tmp, type);

Now, which is cleaner and more readable? Ignoring efficiency, I
think your colleague is doing just fine in this case. There are
few places where I would use the ? operator, yet this is one of
them. I would, however, probably isolate it in a routine such as:

char *typetostring(enum type t);

which would allow

printf(" ==> %s, Type %02X \n", typetostring(code), type);

You still have the temporary, it is just effected by the function
return value. However you can reuse the translation routine if
needed elsewhere.

--
"If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson




CBFalconer写道:

CBFalconer wrote:
ni ************* @ yahoo.com 写道:

下面你将看到我的同事喜欢的嵌套条件表达式的例子。他声称,多级if-else-if结构更有效。此外,我们的复杂性分析工具据称不会捡起来。它真的更有效吗?我个人觉得这种编码风格非常神秘,误导和容易出错。

我相信我已经从
这个编码示例中删除了所有私有的痕迹多个文本替换。

Below you will see an example of a nested conditional expression
that this colleague of mine loves. He claims that it is more
efficient that a multi-level if-else-if structure. Moreover, our
complexity analyzer tool supposedly does not pick it up. Is it
really more efficient? Personally I find this coding style
extremely cryptic, misleading and error-prone.

I believe that I have removed all traces of proprietary-ness from this coding example by multiple text substitutions.



...剪断...

在进行了一些不错的缩进后(见下面的sig),你有:



... snip ...

After putting some decent indentation in (see sig below) you have:

printf(" ==>%s,类型%02X \ n",
(代码== QWER?" QWER":
代码== TYUI?" TYUI":
code == ASDF?" ASDF":
code == GHJK?" GHJK":
code == ZXCV?" ZXCV":
代码== BNML?" BNML"" ????"),
type);
printf(" ==> %s, Type %02X \n",
(code == QWER ? "QWER":
code == TYUI ? "TYUI":
code == ASDF ? "ASDF":
code == GHJK ? "GHJK":
code == ZXCV ? "ZXCV":
code == BNML ? "BNML": "????"),
type);



这里的另一个选择是临时的,还有一个if else链:

char * tmp;

if(code == QWER)tmp =" QWER" ;;
else if(code == TYUI)tmp = " TY UI"
else if(code == ASDF)tmp =" ASDF" ;;
else if(code == GHJK)tmp =" GHJK" ;;
else if(code = = ZXCV)tmp =" ZXCV" ;;
if if(code == BNML)tmp =" BNML" ;;
else tmp =" ????" ;;
printf (QUOT; ==> %s,类型%02X \ n",tmp,type);

现在,哪个更干净,更具可读性?忽略效率,我认为你的同事在这种情况下做得很好。我用的地方很少?运营商,但这是他们之一。但是,我可能会在例行程序中将其隔离:



The other choice here is a temporary, and an if else chain:

char *tmp;

if (code == QWER) tmp = "QWER";
else if (code == TYUI) tmp = "TYUI";
else if (code == ASDF) tmp = "ASDF";
else if (code == GHJK) tmp = "GHJK";
else if (code == ZXCV) tmp = "ZXCV";
else if (code == BNML) tmp = "BNML";
else tmp = "????";
printf(" ==> %s, Type %02X \n", tmp, type);

Now, which is cleaner and more readable? Ignoring efficiency, I
think your colleague is doing just fine in this case. There are
few places where I would use the ? operator, yet this is one of
them. I would, however, probably isolate it in a routine such as:




我个人永远不会嵌套三元:这样的opeartors

(特别是永远不会把整个东西放在printf参数中!)。

如果语句不容易出错,在这种情况下,确实没有

使用理由:?操作员(计算

行数的差异???)。



I personally would never nest ternary : ? opeartors like this
(especially never putting the whole thing inside a printf argument!).
If statements are less error prone and in this case, there really is no
reason to use :? operators (count the difference in number of
lines???).


这篇关于嵌套条件表达式----好主意/坏主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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