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

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

    There's two parts to that question.

    How to dispatch based on a value? Use a switch statement. It displays your intent most clearly.

    When to dispatch based on a value? Only at one place per value: create a polymorphic object that knows how to provide the expected behavior for the value.

提交回复
热议问题