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

前端 未结 9 1896
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  • 2021-01-12 03:49

    There's one thing that hasn't been mentioned here.

    With an if-else statement, every time the code runs, at least 1 of the condition is guaranteed to be evaluated executed. I'm sure we all know how an if-else-elseif works, but to be clear... the if portion of the statement will always be evaluated, if it's false then the following else-if is evaluated, and so forth until only the else is left to evaluate.

    So using an if-else statement will impact your performance. Not significantly (in most cases), but it does take CPU time to perform the evaluations.

    try-catch statements, and correct me if I'm wrong, don't get considered at runtime until they're required (i.e. an exception is thrown). So simply wrapping your code in a try-catch will not affect performance until an exception is actually caught by it.

    Also, it is not the catch that causes the performance hit, but the throw.

    And one major point to make is, try-catch statements should NEVER be used for conditional logic. They should only be used for what they're designed for: exception handling!

    Catching exceptions is essential, if you know what to do with them. If you have no way of properly handling the exception then you should let it go, for some code further up the chain may have a better way to handle it.

    Its usually a good idea to have an exception hander at the absolute top level of your application to catch exceptions before they are seen by the user. In ASP.NET you can do this in the Application_Error event of the global.asax. In other languages/environments you would do so in your main loop, whatever that may be.

    But note, there are some exceptions that are always best left uncaught. Sometimes when an exception happens it is an indicator that the state of your application has been severely compromised and cannot be trusted. The only safe thing to do is to kill and restart.

    0 讨论(0)
  • 2021-01-12 03:50

    Using exceptions is universal method to notify other part of code that something wrong happened in loosely coupled way. Let imagine that if you would like to handle some exceptional condition by using if.. nad else.. you need to insert into different part of your code some arbitrary variables and other stuff which probably would easily led to have spaghetti code soon after.

    Let next imagine that you are using any external library/package and it's author decided to put in his/her code other arbitrary way to handle wrong states - it would force you to adjust to its way of dealing with it - for example you would need to check if particular methods returns true or false or whatever.

    Using exceptions makes handling errors much more easy - you just assume that if something goes wrong - the other code will throw exception, so you just wrap the code in try block and handle possible exception on your own way.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-12 03:54

    I think if statements are better. You can't surround every line of code with a try..catch (well you can but you should not do it). You can surround a block of code with try catch but not every line.

    And exceptions slow things down.

    0 讨论(0)
  • 2021-01-12 03:56

    this debate (2003) was good:

    http://www.joelonsoftware.com/items/2003/10/13.html

    http://nedbatchelder.com/blog/200310/joel_on_exceptions.html

    0 讨论(0)
  • 2021-01-12 04:03

    @PersonalPerson - Sorry, but this is just lazy coding. Rather than using a try-catch because there's too many if statements, why not refactor your code (i.e. put your validation logic in its own method). This will keep your code cleaner and more readable and maintain best practices for performance.

    Never try to write your code to achieve the least # of lines. This will always end up badly. Sure you can find a more elegant way to write something that your coder-buddies will ooh and aah at, but it just makes things less readable.

    I swear, I've worked with your code before, and it made my head hurt.

    0 讨论(0)
提交回复
热议问题