How to prevent the arrowhead anti-pattern

后端 未结 13 1315
南旧
南旧 2020-12-05 06:37

I\'m a bit confused about how to best refactor my code into something more readable.

Consider this piece of code:

var foo = getfoo();
if(foo!=null)
{         


        
13条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-05 06:55

    I'd go for the multiple return statements. This makes the code easy to read and understand.

    Don't use goto for obvious reasons.

    Don't use exceptions because the check you are doing isn't exceptional, it's something you can expect so you should just take that into account. Programming against exceptions is also an anti-pattern.

提交回复
热议问题