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

后端 未结 21 1199
迷失自我
迷失自我 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:23

    In this simple case you could use a switch.

    Otherwise a table-based approach looks fine, it would be my second choice whenever the conditions are regular enough to make it applicable, especially when the number of cases is large.

    Polymorphism would be an option if there are not too many cases, and conditions and behaviour are irregular.

提交回复
热议问题