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

前端 未结 11 770
野性不改
野性不改 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: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()
    

提交回复
热议问题