Looking for performance :)

Discussion in 'Plugin Development' started by Goty_Metal, Jan 18, 2013.

Thread Status:
Not open for further replies.
  1. Well i was thinking about what's the best way for the server to make checks like those:

    Wathever event{
    if(! bla bla ) return;
    if(! bla bla bla ) return;
    if(! bla ble bli ) return;
    do something:
    }

    or


    Wathever event{
    if( bla bla ){
    if( bla bla bla ){
    if ( bla ble bli ){
    do something:
    }
    }
    }
    }

    What do you think it's better for the server to proccess?? thanks guys :)
     
  2. Offline

    LaxWasHere

    Your blah is null.
     
    Phinary, tommycake50 and Bo98 like this.
  3. Yea... that really helped alot...
     
  4. Offline

    Lolmewn

    I'd say it's exactly the same. The performance difference between those two is pretty close to 0.
     
  5. So should i change the question for... what's you favourite one? :D
     
  6. Offline

    danthonywalker

    You can also use else if statements if you only want 1 if to ever take on at a time. So if(blah) is not true then it skips to the else if(blah blah) if that's not true it wll keep skipping until neither are true or one is. So if(blah) was true it would ignore all the other else if statements.

    Although the difference in performance is nearly 0 if you are doing this kind of thing. Certain code can be structurally changed to be much more compact, while others can't, it all depends on the application really.
     
  7. Offline

    ImTheFool

    I'd just use
    Code:
    if (blah()==1 && blah2()==2 && blah3()==3) {
        //do stuff
    }
    
     
  8. Yea also do the same when i can get the variables together :)
     
  9. Offline

    Phinary

    Use whatever you like best. Simple checks like this wont really affect performance at all.
     
  10. Goty_Metal
    As everyone has said the performance between the two is negligible.

    Personally i use the ones without the return statements, i find it's much easier to look at.
     
  11. Agree, i like the no return ones :)
     
  12. I for myself like the return approach if it is used in the following sense:
    Code:
    Object method(args...) {
       if (!precondition1 || !precondition2 || ...)
          return null;
    
       Object result;
       doSomeStuff();
       if (conditionToDoAorB) {
          result = doA();
       }
       else {
          result = doB();
       }
    
       return result;
    }
    So first there are the checks for the preconditions. Of course it can be splitted to more then one if, if it's not readable in a single one. After that, the method starts doing it's job and at the end, the calculated value is returned. So there are no returns in your code but at the very beginning and the very end of the method.

    Three only thought about performance you should have here, is the order of your condition checks of course. If a condition can be checked very fast or will fail very often you should put it as near to the beginning of the method as possible, to avoid unneeded checks. But that should be clear regardless of how you split the if statements.
     
  13. Yea i always check first the most important thing to avoid using useless code, like "if is not a player return" cause you don't need / can't gate the name for example etc
     
Thread Status:
Not open for further replies.

Share This Page