avoiding if statements

前端 未结 24 788
心在旅途
心在旅途 2021-01-30 08:39

I was thinking about object oriented design today, and I was wondering if you should avoid if statements. My thought is that in any case where you require an if statement you ca

24条回答
  •  佛祖请我去吃肉
    2021-01-30 09:32

    Completely eliminating if statements is not realistic and I don't think that is what Ori is suggesting. But they can often be replaced using polymorphism. (And so can many switch statements).

    Francesco Cirillo started the Anti-If Campaign to raise awareness of this issue. He says:

    Knowing how to use objects lets developers eliminate IFs based on type, those that most often compromise software's flexibility and ability to evolve.

    You or your team can also join the campaign.

提交回复
热议问题