Methods of simplifying ugly nested if-else trees in C#

前端 未结 11 669
野性不改
野性不改 2021-02-02 04:23

Sometimes I\'m writing ugly if-else statements in C# 3.5; I\'m aware of some different approaches to simplifying that with table-driven development, class hierarchy, anonimous m

相关标签:
11条回答
  • 2021-02-02 04:31

    In simple cases you should be able to get around with basic functional decomposition. For more complex scenarios I used Specification Pattern with great success.

    0 讨论(0)
  • 2021-02-02 04:32

    try to use patterns like strategy or command

    0 讨论(0)
  • 2021-02-02 04:35

    I'm a big fan of the ternary operator which get's overlooked by a lot of people. It's great for assigning values to variables based on conditions. like this

    foobarString = (foo == bar) ? "foo equals bar" : "foo does not equal bar";
    

    Try this article for more info.

    It wont solve all your problems, but it is very economical.

    0 讨论(0)
  • 2021-02-02 04:36

    If the entire purpose is to assign a different value to some variable based upon the state of various conditionals, I use a ternery operator.

    If the If Else clauses are performing separate chunks of functionality. and the conditions are complex, simplify by creating temporary boolean variables to hold the true/false value of the complex boolean expressions. These variables should be suitably named to represent the business sense of what the complex expression is calculating. Then use the boolean variables in the If else synatx instead of the complex boolean expressions.

    0 讨论(0)
  • 2021-02-02 04:38

    There are very old "formalisms" for trying to encapsulate extremely complex expressions that evaluate many possibly independent variables, for example, "decision tables" :

    http://en.wikipedia.org/wiki/Decision_table

    But, I'll join in the choir here to second the ideas mentioned of judicious use of the ternary operator if possible, identifying the most unlikely conditions which if met allow you to terminate the rest of the evaluation by excluding them first, and add ... the reverse of that ... trying to factor out the most probable conditions and states that can allow you to proceed without testing of the "fringe" cases.

    The suggestion by Miriam (above) is fascinating, even elegant, as "conceptual art;" and I am actually going to try it out, trying to "bracket" my suspicion that it will lead to code that is harder to maintain.

    My pragmatic side says there is no "one size fits all" answer here in the absence of a pretty specific code example, and complete description of the conditions and their interactions.

    I'm a fan of "flag setting" : meaning anytime my application goes into some less common "mode" or "state" I set a boolean flag (which might even be static for the class) : for me that simplifies writing complex if/then else evaluations later on.

    best, Bill

    0 讨论(0)
  • 2021-02-02 04:38

    Simple. Take the body of the if and make a method out of it.

    This works because most if statements are of the form:

    if (condition):
       action()
    

    In other cases, more specifically :

    if (condition1):
       if (condition2):
          action()
    

    simplify to:

    if (condition1 && condition2):
       action()
    
    0 讨论(0)
提交回复
热议问题