【问题标题】:Simplifying return of If Statements [closed]简化 If 语句的返回 [关闭]
【发布时间】:2021-03-05 16:10:12
【问题描述】:

我让作者阻止了这个 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) || attribute1 || attribute2 || attribute3)
  • 结合条件即可。 if (string == "something") &&(object1 == null || object2 == null || object3 == null) || (attribute 1 || attribute2 ...等
  • 我知道很多人不喜欢嵌套,但是太长的“if”条件比嵌套的 if 更难阅读。

标签: c# if-statement refactoring simplify


【解决方案1】:

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

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

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

        return state 2; 

但我想花点时间问一下为什么你有object1object2object3 等。

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

        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;

【讨论】:

    【解决方案2】:

    该代码并没有让我感到震惊,但如果你想摆脱嵌套的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"
    

    如果ternary 运算符变得过于复杂,您还可以合并更多变量,也许“meaningfulVariable1”和“meaningfulVariable2”一旦放在一起代表其他东西?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      • 2018-12-01
      • 2021-09-05
      • 1970-01-01
      • 2015-09-12
      相关资源
      最近更新 更多