Better way to do this?

Discussion in 'Plugin Development' started by DrAgonmoray, Jun 13, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    DrAgonmoray

    I'm making some commands, and it's annoying me having to do so many argument length checks.
    My code basically looks like this:
    Code:java
    1. if (args.length >= 1) {
    2. if (args[0].equalsIgnoreCase("cool")) {
    3. if (args.length >= 2) {
    4. if (args[1].equalsIgnoreCase("awesome")) {
    5. //actual command code..
    6. } else if (args[1].equalsIgnoreCase("command")) {
    7. if (args.length >= 4) {
    8. if (args[2].equalsIgnoreCase("win")) {
    9. //actual command code..
    10. }
    11. }
    12. }
    13. }
    14. }
    15. }


    There's just a bunch of sterpid check everywhere. Is there a better way to make the code flow?
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    EDIT: you can snip out the command logic for each argument into it's own method in the same executor to clean it up slightly so your method is not very long

    OR

    You can use a whole series of CommandExecutors then use a map to determine which commandExecutor to delegate to. Constructing the original command map itself however is not the most pretty thing.

    BUT

    You will need to have some piece of code that determines what what the string is, be it if statements or some sort of switch statement, or leveraging a map and its get function.
     
  3. Offline

    DrAgonmoray

    lol maybe i'll just continue this way. it's not very pretty, but it works
     
  4. Offline

    Ne0nx3r0

    I hear you, but I don't know that there is a more effective way to do it, other than optimizing the layout of conditionals to whatever method suits you.

    One notable thing is that you could combine your arg length and arg equals checks, as long as the length check comes first.

    Realistically args comparison in a command executor doesn't really matter for efficiency, so it's just about what looks nice and what's maintainable.

    To be fair, normally there would be more involved, a whole slew of else's that handle the optional parameters.
     
  5. Offline

    ZachBora

    I did something like this on my latest plugin. Kept things clean.

    PHP:
    if(args.length == 0)
    {
         
    showhelp(p1);
         return 
    true;
    }
    else
    {
              
    String a0 args[0].toString();
              
    int ipage = -1;
         
              try  
              {  
                    if(
    args.length 0)
                    
    ipage Integer.parseInta0 );  
              }  
              catch( 
    Exception e) {}
             
              if(
    ipage != -1)
              {
                    
    showhelp(pipage);
                    return 
    true;
              }else{
                    if (
    a0.equalsIgnoreCase("claim")) { claim(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("auto")) { auto(pargs); return true; }
                    if (
    a0.startsWith("home") || a0.startsWith("h")) { home(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("info") || a0.equalsIgnoreCase("i")) { info(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("comment")) { comment(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("comments") || a0.equalsIgnoreCase("c")) { comments(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("biome") || a0.equalsIgnoreCase("b")) { biome(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("biomelist")) { biomelist(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("id")) { id(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("tp")) { tp(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("clear")) { clear(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("reset")) { reset(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("add") || a0.equalsIgnoreCase("+")) { add(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("remove") || a0.equalsIgnoreCase("-")) { remove(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("setowner") || a0.equalsIgnoreCase("o")) { setowner(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("move") || a0.equalsIgnoreCase("m")) { move(pargs); return true;}
                    if (
    a0.equalsIgnoreCase("reload")) { reload(sargs); return true;}
              }
    }
     
Thread Status:
Not open for further replies.

Share This Page