do this without using an “if” | if(s == “value1”){…} else if(s == “value2”) { …}

前端 未结 18 2161
温柔的废话
温柔的废话 2021-01-30 09:36

According to anti-if campaign it is a best practice not to use ifs in our code. Can anyone tell me if it possible to get rid of the if in this piece of code ? (switch is also

18条回答
  •  粉色の甜心
    2021-01-30 10:08

    I don't think you are making a fair comparison here.

    From the look of it the Anti-if campaign is just about practicing a better design approach.

    However in your case you can see from all the above examples that if can not be removed from the surface and will always exist somewhere down in the center.

    And why exactly is that?

    Well If is a general purpose of life. I don't mean to say start coding if every where but in general without if there is no differentiation, if brings decisions and purpose, if that wasn't there then every object in the world would just execute as its suppose to without even knowing anything other then it self. And very simple you wouldn't have asked this question. :)

提交回复
热议问题