Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • How could I make my code cleaner?


    una_maquina

    Recommended Posts

    Hi, so as you know it's all about readability of code that separates noobs from pros in programming. I think I'm not a complete noob anymore, but I still think that my way of writing code can be improved.

     

    Anyways, the way I write my code is I use a node-based system; but here's where I'm asking for help (here's a code example):

    private static Area geArea;
        
        
        public static boolean isInGe() {
            
            if(!geArea.contains(Client.getLocalPlayer())) {
                //logic to be made
                return false;
            }
            
            return true;
        }
    
        @Override
        public boolean validate() {
            return some condition that triggers the node;
        }
    
        @Override
        public void execute() {
            
            
            // Go to G.E. if not there
            if(!isInGe()) {
                return;
            }
            
            // Continue here
        }
    
    }

     

    This is how I usually structure my code, just making methods in node classes. I have to write a lot of returns, and can easily mess up by forgetting to put it there.

    Is this any good and how could I improve writing node classes? I'm still missing a lot of Java principles, so hopefully you can enlighten me. Thank you!

    Link to comment
    Share on other sites

    Abstracting your code can work wonders toward improving readability (though there is such a thing as too much abstraction). I'd recommend using something like a Tree-Branch Framework to organize your code. Moving from normal task scripts to the aforementioned framework has significantly helped me structure my own code and make it more readable. In the case of the code you've provided above, it can help pull the validation away from the task/node/leaf, and allows you to create general conditions (like your isInGe() method) at a higher level in your script's structure.

    Though I would still consider myself a learner as well, so please take that with a grain of salt and do find what works for you. Best of luck 🙂

    Link to comment
    Share on other sites

    1 hour ago, una_maquina said:

    Hi, so as you know it's all about readability of code that separates noobs from pros in programming. I think I'm not a complete noob anymore, but I still think that my way of writing code can be improved.

     

    Anyways, the way I write my code is I use a node-based system; but here's where I'm asking for help (here's a code example):

    
    private static Area geArea;
        
        
        public static boolean isInGe() {
            
            if(!geArea.contains(Client.getLocalPlayer())) {
                //logic to be made
                return false;
            }
            
            return true;
        }
    
        @Override
        public boolean validate() {
            return some condition that triggers the node;
        }
    
        @Override
        public void execute() {
            
            
            // Go to G.E. if not there
            if(!isInGe()) {
                return;
            }
            
            // Continue here
        }
    
    }

     

    This is how I usually structure my code, just making methods in node classes. I have to write a lot of returns, and can easily mess up by forgetting to put it there.

    Is this any good and how could I improve writing node classes? I'm still missing a lot of Java principles, so hopefully you can enlighten me. Thank you!

    I would totally use the tree-branch thing as Bon said, although it doesn't mesh with my weird coding style it will probably suit you. For nodes, I put all my utils methods in a single Utils class, which is where this GE checking thing would go. I don't like using number priorities, that's just asking for trouble and the validate() is almost always determined by a global String variable to avoid annoying complexities. Also I tend not to use areas unless they're really needed, I just find them yucky but as I said my coding style is weird :)

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.