[Tut] How to manage 'If-Else' Blocks Correctly using Boolean Algebra

Results 1 to 9 of 9
  1. #1
    Member meiscooldude is offline
    MemberRank
    Nov 2006 Join Date
    79Posts

    [Tut] How to manage 'If-Else' Blocks Correctly using Boolean Algebra

    How to Manage 'If-Else' Blocks


    I remember taking some Boolean algebra last year, And i was recently looking at the commands in most WinterLove servers...
    I realized there was some major refactoring to do there...

    What we are going to do is divide up our commands to make them be checked as quickly and efficiently as possible. The first way to divide that I would take is to divide them up by player Rights...


    Code:
    if(command.equalsIgnoreCase("coins") && playerRights >= 2)
    {
      // what ever it does
    }
    if(command.equalsIgnoreCase("ace") && playerRights >= 2)
    {
      // what ever it does
    }
    if(command.equalsIgnoreCase("barrows") && playerRights >= 2)
    {
      // what ever it does
    }
    if(command.equalsIgnoreCase("Armor") && playerRights >= 2)
    {
      // what ever it does
    }
    if(command.equalsIgnoreCase("kick") && playerRights >= 1)
    {
      // what ever it does
    }
    if(command.equalsIgnoreCase("warn") && playerRights >= 1)
    {
      // what ever it does
    }
    if(command.equalsIgnoreCase("starter"))
    {
      //what ever it does
    }
    if(command.equalsIgnoreCase("home"))
    {
      //what ever it does
    }
    if(command.equalsIgnoreCase("woodcutting"))
    {
      //what ever it does
    }
    Here is a Flow diagram of the code above:

    While it looks simple, and efficient, its not, if the command entered happens to be the one at the top, it checks every other command after it even though we know that it will be false... (computers are not that smart)

    Below i have re-writen the code to be a little more efficient.

    Code:
    if(playerRights >= 2)
    {
    
       if(command.equalsIgnoreCase("coins"))
       {
         // what ever it does
       }
       if(command.equalsIgnoreCase("ace"))
       {
         // what ever it does
       }
       if(command.equalsIgnoreCase("barrows"))
       {
         // what ever it does
       }
       if(command.equalsIgnoreCase("Armor"))
       {
         // what ever it does
       }
    }
    if(playerRights >= 1)
    {
    	if(command.equalsIgnoreCase("kick"))
    	{
      		// what ever it does
    	}
    	if(command.equalsIgnoreCase("warn"))
    	{
      	// what ever it does
    	}
    }
    if(playerRights >= 0)
    {
    	if(command.equalsIgnoreCase("home"))
    	{
      		// what ever it does
    	}
    	if(command.equalsIgnoreCase("woodcutting"))
    	{
      	// what ever it does
    	}
    }
    Here is a Flowchart of the code above:

    So, now instead of checking every possible command, It will only check the commands that require your player rights... So if your just a regular player, it won't even look at the admin or the mod commands. (so, if you are just a regular player, it will only check the bottom 3 commands, not all 9)


    Now, that is better, but we can still make them even better... By adding the 'else' keyword


    Code:
    if(playerRights >= 2)
    {
       if(command.equalsIgnoreCase("coins"))
       {
         // what ever it does
       }
       else if(command.equalsIgnoreCase("ace"))
       {
         // what ever it does
       }
       else if(command.equalsIgnoreCase("barrows"))
       {
         // what ever it does
       }
       else if(command.equalsIgnoreCase("Armor"))
       {
         // what ever it does
       }
    }
    if(playerRights >= 1)
    {
    	if(command.equalsIgnoreCase("kick"))
    	{
      		// what ever it does
    	}
    	else if(command.equalsIgnoreCase("warn"))
    	{
      	// what ever it does
    	}
    }
    if(playerRights >= 0)
    {
    	if(command.equalsIgnoreCase("home"))
    	{
      		// what ever it does
    	}
    	else if(command.equalsIgnoreCase("woodcutting"))
    	{
      	// what ever it does
    	}
    }
    Here is a Flowchart of the code above:

    The 'else' keyword will make it so that it will only check the conditions of the 'if' following the 'else', if the previous conditions where false... But why add it?, because, lets say if we the command was already found, then is there really any point in checking if all the other command equal it? no... there is not... It is just a waist of time and resources. (so, now lets say your an admin, and you chose to do the command that was second in the admin's command list, Now, once it found that commad, it won't check to see if the other commands where it)

    Now, this is lacking just one more optimization,

    Somewhere declare this:
    Code:
    private boolean cmdNotFound = false;
    // or either
    static boolean cmdNotFound = false;
    This boolean is going to help tell us if our command has already been found. (preventing us from looking for the already found command in other player rights)


    Code:
    if(playerRights >= 2)
    {
    
       cmdNotFound = false;
    
       if(command.equalsIgnoreCase("coins"))
       {
         // what ever it does
       }
       else if(command.equalsIgnoreCase("ace"))
       {
         // what ever it does
       }
       else if(command.equalsIgnoreCase("barrows"))
       {
         // what ever it does
       }
       else if(command.equalsIgnoreCase("Armor"))
       {
         // what ever it does
       }
       else
          cmdNotFound = true; //since all of the if's failed, commandNotFound equals true
    }
    if(cmdNotFound && playerRights >= 1)
    {
    
    	cmNotFound = false;
    
    	if(command.equalsIgnoreCase("kick"))
    	{
      		// what ever it does
    	}
    	else if(command.equalsIgnoreCase("warn"))
    	{
      	// what ever it does
    	}
    	else
    	   cmNotFound = true;
    }
    if(cmdNotFound) //there is no more PlayerRights >= 0  here, because since everyone would apply, it can be assumed that everyone will pass
    {
    	if(command.equalsIgnoreCase("home"))
    	{
      		// what ever it does
    	}
    	else if(command.equalsIgnoreCase("woodcutting"))
    	{
      	// what ever it does
    	}
    }
    Here is a Flowchart of the code above:

    Now, you see that if you pick the second command for an Admin, it would no longer check the other admins commands, Then it would no longer check the mod commands, or the player commands Making this a very efficient way to organize your large 'if-else' blocks,


    The only other way (that i know), how to optimize it would be to put normal more frequently used commands more towards the top of the 'if else' block.

    I hope you enjoyed this little tutorial

    Credits: Me
    Last edited by meiscooldude; 27-06-08 at 04:27 PM. Reason: little error .. :P


  2. #2
    right + down + X GhostSnyper is offline
    MemberRank
    May 2006 Join Date
    AZ, USALocation
    2,818Posts

    Re: [Tut] How to write Commands Correctly using Boolean Algebra (and reduce some lag)

    Wow, Good job on that! I implemented this, But it slipped my mind to explain this here. Props to the CoolDude! Yea, commands used to be bad; People used to always ask why the else was in there, and I'd have to tell them this.

    Thread = epic knowledge base material!

  3. #3
    Member meiscooldude is offline
    MemberRank
    Nov 2006 Join Date
    79Posts

    Re: [Tut] How to write Commands Correctly using Boolean Algebra (and reduce some lag)

    Quote Originally Posted by ghostsnyper View Post
    Wow, Good job on that! I implemented this, But it slipped my mind to explain this here. Props to the CoolDude! Yea, commands used to be bad; People used to always ask why the else was in there, and I'd have to tell them this.

    Thread = epic knowledge base material!

    Thanks, I hope some people actually start thinking like this

  4. #4
    Apprentice Lucy1 is offline
    MemberRank
    Jun 2008 Join Date
    6Posts

    Re: [Tut] How to manage 'If-Else' Blocks Correctly using Boolean Algebra

    thanks this was nice/

  5. #5
    right + down + X GhostSnyper is offline
    MemberRank
    May 2006 Join Date
    AZ, USALocation
    2,818Posts

    Re: [Tut] How to manage 'If-Else' Blocks Correctly using Boolean Algebra

    Nice addition with the pseudo diagrams. very good material

  6. #6
    There's no RL just AFK -fedexer- is offline
    MemberRank
    May 2006 Join Date
    ScotlandLocation
    1,632Posts

    Re: [Tut] How to manage 'If-Else' Blocks Correctly using Boolean Algebra

    Great tutorial, and now command code will hopefully be optimized for better preformance.

    Only problem i noticed in the tutorial was that you were not using the same variable name for the boolean you set up, you were declaring commandNotFound as true, when this should have been cmdNotFound.

    This will throw the users who do not fully understand variables and when they get a lovely display of errors they will be like "WTF !!!".

    So yeah, just go back through it and check those variable names and then the tutorial will be perfect :winky:

    Unfortunately (maybe even fortunately xD) i have a keen eye for mistakes in code.

    Edit: This i reckon is KB material.

  7. #7
    Member meiscooldude is offline
    MemberRank
    Nov 2006 Join Date
    79Posts

    Re: [Tut] How to manage 'If-Else' Blocks Correctly using Boolean Algebra

    Quote Originally Posted by -fedexer- View Post
    Great tutorial, and now command code will hopefully be optimized for better preformance.

    Only problem i noticed in the tutorial was that you were not using the same variable name for the boolean you set up, you were declaring commandNotFound as true, when this should have been cmdNotFound.

    This will throw the users who do not fully understand variables and when they get a lovely display of errors they will be like "WTF !!!".

    So yeah, just go back through it and check those variable names and then the tutorial will be perfect :winky:

    Unfortunately (maybe even fortunately xD) i have a keen eye for mistakes in code.

    Edit: This i reckon is KB material.
    Thanks, And i fixed that little error... And ya, its good you have a keen eye for mistakes,

    Also, Fedexer, Are you the fedexer from almost forever ago?... You had a tutorial about Global Objects I remember... And it mad the server lag like CRAZY, lol

  8. #8
    There's no RL just AFK -fedexer- is offline
    MemberRank
    May 2006 Join Date
    ScotlandLocation
    1,632Posts

    Re: [Tut] How to manage 'If-Else' Blocks Correctly using Boolean Algebra

    Yes i am that fedexer from ages ago, and yeah... i reckon it would not have made servers lag if a way of implementing them into the server properly was to be discovered.... mainly it seems it was called in the boolean process, and well.. we all know that was a bad idea lol.

  9. #9
    right + down + X GhostSnyper is offline
    MemberRank
    May 2006 Join Date
    AZ, USALocation
    2,818Posts

    Re: [Tut] How to manage 'If-Else' Blocks Correctly using Boolean Algebra

    you don't need it to be called from the Boolean process, really. The global objects only needed to be placed once... When I used your global tut, I just had a void that used the actionTimer after the server started, set it to about 300ms, then when it fired, spawned the globals. Then rechecked if they were there every 10 seconds, and replaced if not there. Good way to have the objects placed without stressing the process. A lot of my code was run that way, however I got lazy after a while, since there was way too many lines to refactor



Advertisement