What's the best alternative to an out of control switch statement?

后端 未结 11 1425
悲&欢浪女
悲&欢浪女 2021-02-14 14:20

I have inherited a project that has some huge switch statement blocks, with some containing up to 20 cases. What is a good way to rewrite these?

11条回答
  •  天涯浪人
    2021-02-14 14:57

    Why would you want to rewrite them in a different structure? If you really have 20 cases that have to be handled individually, a switch/case is the way to go. A big chain of if/then logic would be horrible to maintain.

    Polymorphism is another option if you are using an object-oriented language. Each subclass would implement it's own functionality in the method.

提交回复
热议问题