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

前端 未结 11 674
野性不改
野性不改 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条回答
  •  闹比i
    闹比i (楼主)
    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.

提交回复
热议问题