Thorough use of 'if' statements or 'try/catch' blocks?

前端 未结 9 1958
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 03:14

Give me some of your thoughts on which is a better coding practice/makes more efficient code/looks prettier/whatever: Increasing and improving your ability to use if statem

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-12 03:51

    From what I've been told by more experienced developers and analysts, try/catch is more object oriented and if is more procedural.

    I personally don't care.

    I'm aware of the fact that a try/catch is slower and causes a performance hit, but if I'm going to use a dozen ifs to validate before I can do what I want to do, I will always use a try/catch to save on the number of lines of code.

    It makes my life so much easier to not have to validate anything and if the statement fails, just do what I would have done in my 'else' block...in my 'catch' block.

    Sometimes, I obviously enclose some if statements in a try/catch, but anyways.

    I use an if when there's only a small number of things to validate (1 or 2) before doing what I need to do.

提交回复
热议问题