0

I'm trying implement below simple logic with a best way (performance wise), will I get a advantage using switch over if else statements in this scenario. (I went through some similar questions here and found that polymorphism can be use in situations like this but I cannot find a way to apply it since I'm new to OOP )

Thank you!

 status = statusCheck();

if(status == done){
    // done
} else if (status == fail){
    // fail
} else if (status == processing){

    thread.sleep();
    status = statusCheck();

    if(status == done){
        // done
    } else if (status == fail){
        // fail
    } else {
        //do somthing
    }
}
Vishal Gupta
  • 124
  • 6
Sameera.San
  • 67
  • 2
  • 15
  • You can do the test yourself, with several cases and a log of execution time... count the difference in milliseconds before and after your logic – Incepter May 10 '18 at 09:02
  • 12
    Switch vs multiple else-ifs will give you no discernable performance advantage. Worry about writing readable, maintainable code, not about making micro-optimisations. – Michael May 10 '18 at 09:02
  • 5
    Performance will not matter with this problem. You should aspire to write the clearest, most readable, easiest to test code you can. If you repeat this logic elsewhere you should encapsulate it in a method that you can call. I don't know what that Thread.sleep() is for. – duffymo May 10 '18 at 09:02
  • There is no significant performance difference. Even if you fall through all three equality checks, that is fast enough. And maybe the JIT also does its magic. – Thilo May 10 '18 at 09:02
  • 3
    Do not consider performance when writing code like this. It simply will not matter! Consider cleanliness and readability! – Seelenvirtuose May 10 '18 at 09:03
  • 1
    If you want to improve the code: Don't reassign to variables. Try to avoid polling with Thread.sleep, see if you can get some notification/callback mechanism working. – Thilo May 10 '18 at 09:04
  • @Michael well, if you know that switch is faster why not make it then? a switch is `O(1)` (it's either tableswitch or lookupswitch). – Eugene May 10 '18 at 09:06
  • 1
    @Eugene if you know that hand-optimised assembly code will be faster, why not do that? – Michael May 10 '18 at 09:10
  • @Eugene regardless if it is faster or not, he is saying something like this DON'T search for unnoticeable performance rather than make the code easy to read – Basil Battikhi May 10 '18 at 09:10
  • @Michael :) right, I should have added "just sayin... " – Eugene May 10 '18 at 09:11
  • @BasilBattikhi well *I* find a switch easier to read than multiple if/statements – Eugene May 10 '18 at 09:14
  • 1
    Any difference in performance will only be noticeable after millions or billions of executions, so just write it whichever way you think looks better and is more readable. And know that whichever choice you make, someone will tell you the other way would have been better, so be prepared to ignore him/her (or more likely, _them_). – Kevin Anderson May 10 '18 at 09:17
  • Thanks for answers :) I think I will go with switch since it's more readable and clear in my case. Can I use polymorphism in this case (just curious) ? – Sameera.San May 10 '18 at 09:23
  • 1
    Yeah. For example your statusCheck method can return different status object all sharing the same interface - doStuffForThatStatus(). And your code will become something like status=statusCheck(); status.doStuffForThatStatus(); and the logic will be in the different statuses – Veselin Davidov May 10 '18 at 09:30
  • @Veselin Thanks! – Sameera.San May 11 '18 at 08:17

0 Answers0