What is the best way to replace or substitute if..else if..else trees in programs?

后端 未结 21 1147
迷失自我
迷失自我 2020-11-28 06:23

This question is motivated by something I\'ve lately started to see a bit too often, the if..else if..else structure. While it\'s simple and has its uses, somet

21条回答
  •  死守一世寂寞
    2020-11-28 07:26

    In Object Oriented languages, it's common to use polymorphism to replace if's.

    I liked this Google Clean Code Talk that covers the subject:

    The Clean Code Talks -- Inheritance, Polymorphism, & Testing

    ABSTRACT

    Is your code full of if statements? Switch statements? Do you have the same switch statement in various places? When you make changes do you find yourself making the same change to the same if/switch in several places? Did you ever forget one?

    This talk will discuss approaches to using Object Oriented techniques to remove many of those conditionals. The result is cleaner, tighter, better designed code that's easier to test, understand and maintain.

提交回复
热议问题