这是一个合理使用三元运算符的? [英] Is this a reasonable use of the ternary operator?

查看:130
本文介绍了这是一个合理使用三元运算符的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有从code测试结果如

Are there any understanding / maintainability issues that result from code like

inVar1 == 0 ? NULL : v.push_back(inVar1);
inVar2 == 0 ? NULL : v.push_back(inVar2);

和等等。

可能为混乱的想法是使用程序流,而不是变量赋值三元运算符,这是通常的解释。

The possibly confusing idea is using the ternary operator for program flow rather than variable assignment, which is the usual explanation.

我还没有看到编码工作,解决这种用法,所以虽然我很舒服这样做,我想看看是否有一个很好的理由不这样做。

I haven't seen coding standards at work that address this usage, so while I'm comfortable doing this I'd like to find out if there is a good reason not to.

推荐答案

我认为这是令人困惑和困难得多比简单地输入读取;

I think it's confusing and a lot harder to read than simply typing;

if (inVar != 0)
  v.push_back(inVar);

我不得不扫描您的例子几次搞清楚,结果将是有把握的。我甚preFER单行如果(){}语句比你的例子 - 我恨单行if语句:)

I had to scan your example several times to figure out what the result would be with any certainty. I'd even prefer a single-line if() {} statement than your example - and I hate single-line if statements :)

这篇关于这是一个合理使用三元运算符的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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