什么是最好的做法关于C#短路的评价? [英] What is the best practice concerning C# short-circuit evaluation?

查看:243
本文介绍了什么是最好的做法关于C#短路的评价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在评论答案,随后辩论在另一个线程的促使我要问:

在C#||和&功放;&安培;是逻辑运算符的短路版本|和&安培;分别。

实例:

 如果(String.IsNullOrEmpty(文本1)| String.IsNullOrEmpty(文本2)| String.IsNullOrEmpty(文字3))
{
    // ...
}

 如果(String.IsNullOrEmpty(文本1)|| String.IsNullOrEmpty(文本2)|| String.IsNullOrEmpty(文字3))
{
    // ...
}

在编码的做法是使用更好,为什么?

条款

注:我的确意识到这个问题是类似这个问题,但我认为这是值得的语言具体的讨论。


解决方案

  

在编码的做法是使用更好,为什么?

条款

答案很简单:总是用短路版本。有根本没有理由不这么做。此外,你让你的code更清楚,因为你前preSS您的意图的:合理的评估。使用位(逻辑)操作意味着你只想是:位运算,而不是逻辑的评估(即使MSDN称他们为逻辑运算符吧,当应用于布尔值)

此外,由于短路只判断哪些地方需要评估,它往往更快,它允许写出这样code作为

 布尔nullorempty = STR == NULL || str.Length == 0;

(注意:要解决一个更好的功能已经存在,即这个特殊问题 string.IsNullOrEmpty 您还你的问题中使用。)这code止跌 T为可能的位逻辑操作,因为即使 STR ,第二个前pression会得到评估,造成了的NullReferenceException

修改:如果你的希望的副作用,在逻辑背景下发生的,请的还是的不使用位运算。这是过于聪明的一个典型例子。在code的下一个维护者(甚至你自己,经过数周)禾认为这code会觉得嗯,这code可以清理使用条件运算符,从而inadvertedly打破code。我怜悯谁负责修复这个bug的。

相反,如果你有依靠的一面,效果,使他们明确的:

 布尔hasBuzzed = checkMakeBuzz();
布尔isFrobbed = checkMakeFrob();
布尔导致= hasBuzzed || isFrobbed;

当然,三线而不是一个。但是,一个更清晰的code作为一个结果。

An answer and subsequent debate in the comments in another thread prompted me to ask:

In C# || and && are the short-circuited versions of the logical operators | and & respectively.

Example usage:

if (String.IsNullOrEmpty(text1) | String.IsNullOrEmpty(text2) | String.IsNullOrEmpty(text3))
{
    //...
}

versus:

if (String.IsNullOrEmpty(text1) || String.IsNullOrEmpty(text2) || String.IsNullOrEmpty(text3))
{
    //...
}

In terms of coding practice which is the better to use and why?

Note: I do realize this question is similar to this question but I believe it warrants a language specific discussion.

解决方案

In terms of coding practice which is the better to use and why?

Simple answer: always use the short-circuited versions. There’s simply no reason not to. Additionally, you make your code clearer because you express your intent: logical evaluation. Using the bitwise (logical) operations implies that you want just that: bit operations, not logical evaluation (even though the MSDN calls them "logical operators" as well, when applied to boolean values).

Additionally, since short-circuiting only evaluates what needs evaluating, it’s often faster, and it allows to write such code as

bool nullorempty = str == null || str.Length == 0;

(Notice that to solve this particular problem a better function already exists, namely string.IsNullOrEmpty which you also used in your question.) This code wouldn’t be possible with the bitwise logical operations because even if str were null, the second expression would get evaluated, resulting in a NullReferenceException.

EDIT: If you want side-effects to occur in a logical context please still don’t use bitwise operations. This is a typical example of being too clever. The next maintainer of the code (or even yourself, after a few weeks) wo sees this code will think "hmm, this code can be cleared up to use conditional operators," thus inadvertedly breaking the code. I pity whoever is in charge of fixing this bug.

Instead, if you have to rely on side, effects, make them explicit:

bool hasBuzzed = checkMakeBuzz();
bool isFrobbed = checkMakeFrob();
bool result = hasBuzzed || isFrobbed;

Granted, three lines instead of one. But a much clearer code as a result.

这篇关于什么是最好的做法关于C#短路的评价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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