为什么要在条件中使用赋值?
在许多语言中,赋值在有条件的情况下是合法的。 我一直不明白这背后的原因。 为什么你会写:
if (var1 = var2) {
...
}
而不是:
var1 = var2;
if (var1) {
...
}
?
In many languages, assignments are legal in conditions. I never understood the reason behind this. Why would you write:
if (var1 = var2) {
...
}
instead of:
var1 = var2;
if (var1) {
...
}
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我发现它在经常涉及错误检测等的操作链中最有用。
替代方案(不使用条件中的分配)是:
通过长时间的错误检查,替代方案可以在页面的右侧运行,而分配在-条件版本不这样做。
错误检查也可以是“动作”——
first_action()
、second_action()
、third_action()
——当然,不仅仅是检查。 也就是说,它们可以被检查该功能正在管理的流程中的步骤。 (大多数情况下,在我使用的代码中,这些函数是沿着前置条件检查的路线,或者函数工作所需的内存分配,或者沿着类似的路线)。I find it most useful in chains of actions which often involve error detection, etc.
The alternative (not using the assignment in the condition) is:
With protracted error checking, the alternative can run off the RHS of the page whereas the assignment-in-conditional version does not do that.
The error checks could also be 'actions' —
first_action()
,second_action()
,third_action()
— of course, rather than just checks. That is, they could be checked steps in the process that the function is managing. (Most often in the code I work with, the functions are along the lines of pre-condition checks, or memory allocations needed for the function to work, or along similar lines).for 循环比 if 语句更有用。
否则必须写成
It's more useful for loops than if statements.
Which would otherwise have to be written
原因是:
性能提升(有时)
更少的代码(总是)
举个例子:有一个方法
someMethod()
,在if
条件下你想检查该方法的返回值是否为空
。 如果没有,您将再次使用返回值。由于您两次调用相同的方法,这会影响性能。 而是使用:
The reason is:
Performance improvement (sometimes)
Less code (always)
Take an example: There is a method
someMethod()
and in anif
condition you want to check whether the return value of the method isnull
. If not, you are going to use the return value again.It will hamper the performance since you are calling the same method twice. Instead use:
我今天在 Arduino(C++ 语言的子集)中编程时使用了它。
案例
I 有一个发射器和一个接收器。
发送器想要发送数据直到接收到数据。 我想在该过程完成时设置一个标志。
这里:
结果
当传输不成功时,不设置该标志,而loop为true时,它保持执行
成功时,设置标志,whileloop为假,退出
I used it today while programing in Arduino (Subset of C++ language).
Case
I have a transmitter and a receiver.
The transmitter wants to send the data until it is received. I want to set a flag when the process is done.
Here:
Result
When the transmission is not successful, the flag is not set, while loop is true, it keeps executing
When successful, the flag is set, while loop is false, we exit
我发现它对于返回可选值的函数非常有用(C++17 中的 boost::可选 或 std::可选 ):
这减少了我的变量的范围,使代码更紧凑并且不会妨碍可读性(我发现)。
与指针相同:
I find it very useful with functions returning optionals (
boost::optional
orstd::optional
in C++17):This reduces the scope of my variable, makes code more compact and does not hinder readability (I find).
Same with pointers:
另一个优点是使用GDB。
在下面的代码中,如果我们单步执行,错误代码是未知的。
现在
,在单步执行期间,我们可以知道 checkstatus() 返回的错误代码是什么。
The other advantage comes during the usage of GDB.
In the following code, the error code is not known if we were to single step.
Rather
Now during single stepping, we can know what was the return error code from the checkstatus().
例如,在 PHP 中,它对于循环 SQL 数据库结果很有用:
这看起来比:
In PHP, for example, it's useful for looping through SQL database results:
This looks much better than:
简而言之,面向表达式编程语言允许更简洁的代码。 它们不会强迫您将命令与查询分开。
The short answer is that expression-oriented programming languages allow more succinct code. They don't force you to separate commands from queries.
当您编写
while
循环而不是if
语句时,该习惯用法更有用。 对于if
语句,您可以按照您的描述将其分解。 但是如果没有这个构造,您要么必须重复自己:要么使用循环半结构:
我通常更喜欢循环半形式。
The idiom is more useful when you're writing a
while
loop instead of anif
statement. For anif
statement, you can break it up as you describe. But without this construct, you would either have to repeat yourself:or use a loop-and-a-half structure:
I would usually prefer the loop-and-a-half form.
如果您要调用函数,它会更有用:
当然,您可以直接输入 n = foo(); 在单独的语句 then if (n) 上,但我认为上面是一个相当可读的习惯用法。
It's more useful if you are calling a function:
Sure, you can just put the n = foo(); on a separate statement then if (n), but I think the above is a fairly readable idiom.
如果您调用的函数返回要处理的数据或返回指示错误(或已完成)的标志,那么它会很有用。
比如:
就我个人而言,我不太喜欢这个习语,但有时替代方案更丑陋。
It can be useful if you're calling a function that returns either data to work on or a flag to indicate an error (or that you're done).
Something like:
Personally it's an idiom I'm not hugely fond of, but sometimes the alternative is uglier.
GCC 可以帮助您检测(使用 -Wall)是否无意中尝试使用赋值作为真值,以防它建议您编写
I.e. 使用额外的括号来表明这确实是您想要的。
GCC can help you detect (with -Wall) if you unintentionally try to use an assignment as a truth value, in case it recommends you write
I.e. use extra parenthesis to indicate that this is really what you want.