How to avoid multiple nested IFs

前端 未结 6 550
情话喂你
情话喂你 2021-02-01 19:08

I am currently trying to restructure my program to be more OO and to better implement known patterns etc.

I have quite many nested IF-statements and want to get rid of t

6条回答
  •  礼貌的吻别
    2021-02-01 19:52

    Your problem is known as the arrowhead anti-pattern.

    There are pragmatic approaches, such as the Guard statements you've shown in your sample, to whole design patterns, avoiding if (and else) all together...

    Lots of resources on how to solve them:

    http://www.codinghorror.com/blog/2006/01/flattening-arrow-code.html

    http://www.lostechies.com/blogs/chrismissal/archive/2009/05/27/anti-patterns-and-worst-practices-the-arrowhead-anti-pattern.aspx

    http://elegantcode.com/2009/08/14/observations-on-the-if-statement/

提交回复
热议问题