C# 简化If语句的返回

C# 简化If语句的返回,c#,if-statement,refactoring,simplify,C#,If Statement,Refactoring,Simplify,我让编剧们封锁这个if语句并简化它。如果嵌套返回相同的值,是否有更有效的方法来实现这一点?我可能只是错过了 if (string == "something") { if (object1 == null || object2 == null || object3 == null) { return state1;

我让编剧们封锁这个if语句并简化它。如果嵌套返回相同的值,是否有更有效的方法来实现这一点?我可能只是错过了

            if (string == "something")
            {
                if (object1 == null || object2 == null || object3 == null)
                {
                    return state1;
                }
            }

            if (attribute1 || attribute2 || attribute3)
            {
                return state1;
            }

            return state 2; 

马上,你可以结合你的条件:

        if (string == "something" 
            && (object1 == null || object2 == null || object3 == null))
        {
            return state1;
        }

        if (attribute1 || attribute2 || attribute3)
        {
            return state1;
        }

        return state 2; 
但是我想花点时间问你为什么会有
object1
object2
object3
,等等

对象的数量可能会发生变化吗?如果是这样,它们会一起改变,还是分开改变?这些对象和属性是否有相似之处将它们联系在一起?如果是这样,您可能会得到如下结果:

        var objects = new[]{object1,object2,object3}; // a list of your objects
        if (string == "something" && objects.Any(o => o == null))
        {
            return state1;
        }

        var attributes=new[]{attribute1,attribute2,attribute3};
        if (attributes.Any(a => a))
        {
            return state1;
        }

        return state 2; 
或者这个:

        var things = ...; // each thing has an object and an attribute?
        if (string == "something" && things.Any(t => t.Object == null))
        {
            return state1;
        }

        if (things.Any(a => a.Attribute))
        {
            return state1;
        }

        return state 2; 
另一方面,如果这些对象和属性彼此完全不相关,但是组合条件有一些特殊之处,那么您可能希望使用变量名来至少表明您的意图

        var objectsAreRequired = string == "something";
        var objectIsMissing = object1 == null || object2 == null || object3 == null;
        if (objectsAreRequired && objectIsMissing)
        {
            return state1;
        }
        ...
您还可以将这些技术结合起来,这样就可以使三元运算符更具可读性

var objectsAreRequired = string == "something";
var objectIsMissing = things.Any(t => t.Object == null);
var isBlocked = things.Any(t => t.Attribute);
var errorExists = objectsAreRequired && objectIsMissing || isBlocked;
return errorExists ? state1 : state2;

这段代码并没有让我感到震惊,但是如果你想摆脱嵌套的
if
,你可以这样做:

        if (string == "something" && (object1 == null || object2 == null || object3 == null) || attribute1 || attribute2 || attribute3)
        {
            return state1;
        }
但在我看来,条件太多会使阅读比嵌套的
if
更复杂。另一种选择是:

var meaningfulVariable1 = string == "something"           
var meaningfulVariable2 = (object1 == null || object2 == null || object3 == null)
var meaningfulVariable3 = (attribute1 || attribute2 || attribute3)

return ((meaningfulVariable1 && meaningfulVariable2) || meaningfulVariable3) ? "state1" : "state2"
如果
三元
运算符变得太复杂,您还可以合并更多变量,可能“meaningfullvariable1”和“meaningfullvariable2”放在一起表示其他变量


重要的是,您的代码可以很容易地被其他人读取。这就是为什么你应该有一个有意义的变量名。当然,这里的代码非常通用,所以我可以猜出它的正确名称:-)

你能解释一下你想要这个代码做什么吗?嵌套可以与
&
组合,第二个块可以与
|
组合。只要将所有
|
条件组合在一行
if(string==“something”&&(object1==null | | object2==null | | object3==null)| attribute1 | | | attribute2 | | attribute3)
只需组合这些条件。
if(string==something)&(object1==null | | object2==null | object3==null)| |(attribute1 | | | attribute2
等。我知道很多人不喜欢嵌套,但嵌套时间太长。”条件使其比嵌套if更难读取。