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...
Here is a Flow diagram of the code above: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 }
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.
Here is a Flowchart of the code above: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 } }
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
Here is a Flowchart of the code above: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 } }
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:
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:private boolean cmdNotFound = false; // or either static boolean cmdNotFound = false;
Here is a Flowchart of the code above: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 } }
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







Reply With Quote![[Tut] How to manage 'If-Else' Blocks Correctly using Boolean Algebra](http://ragezone.com/hyper728.png)


