k1595981 Posted June 22, 2018 Share Posted June 22, 2018 (edited) I am really stuck and would like some advice. If I am to make a custom function that interacts with an item.. Is it better to code two functions, With just one parameter (String item) that will get a random x and y positions of the item and click With three variables (String item, int x, int y) if I want to to click in a specific position or just having one function which is the second one, and if I pass -1,-1 it generates a random position of the item rectangle. I know that both ways will work but imagine a function with 100 lines, then another with 100 or just 1 function with 100 and an extra check for the parameters. So how is such thing done in programming properly? Edited June 22, 2018 by k1595981 Quote Link to comment Share on other sites More sharing options...
Alek Posted June 22, 2018 Share Posted June 22, 2018 Use item.interact(), it chooses a random point in the item bounds anyways. If you're asking this question, then you probably should be using the API methods. To specifically answer your question, 1 method. Each method is an extra jump in memory, which is more expensive. It's not really worth writing two separate methods for this. 2 Quote Link to comment Share on other sites More sharing options...
Athylus Posted June 22, 2018 Share Posted June 22, 2018 (edited) If I understand correctly you want to make a function that grabs the item by name and then call another function inside of there to click on a random spot. In that case I'd go for single responsibility. So you make two functions, each is responsible for only one thing. First off your code is more readable and second you can reuse the random click on any rectangle. Edit: Alek makes a fair point. If you look at it from a lower level then it is more expensive. Correct me if I'm wrong, but your program would have to jump to the other function by accessing the pointer that refers to memory where your method is placed. How the inner workings of Java handle that is still a mystery to me, but looking at it from an assembly perspective there are definitely some more instructions happening. The question is, will it make or break your program on a multi-threaded 3 GHz processor? Edited June 22, 2018 by Athylus Quote Link to comment Share on other sites More sharing options...
Alek Posted June 22, 2018 Share Posted June 22, 2018 18 minutes ago, Athylus said: If I understand correctly you want to make a function that grabs the item by name and then call another function inside of there to click on a random spot. In that case I'd go for single responsibility. So you make two functions, each is responsible for only one thing. First off your code is more readable and second you can reuse the random click on any rectangle. Edit: Alek makes a fair point. If you look at it from a lower level then it is more expensive. Correct me if I'm wrong, but your program would have to jump to the other function by accessing the pointer that refers to memory where your method is placed. How the inner workings of Java handle that is still a mystery to me, but looking at it from an assembly perspective there are definitely some more instructions happening. The question is, will it make or break your program on a multi-threaded 3 GHz processor? You're absolutely right, there will be absolutely no noticeable difference in the end. However more methods doesn't really equate to cleaner code. If he only uses that method once (maybe even twice), then it probably shouldn't be a method. 1 Quote Link to comment Share on other sites More sharing options...
Athylus Posted June 22, 2018 Share Posted June 22, 2018 I agree Alek. Well said. Quote Link to comment Share on other sites More sharing options...
k1595981 Posted June 22, 2018 Author Share Posted June 22, 2018 (edited) 43 minutes ago, Alek said: Use item.interact(), it chooses a random point in the item bounds anyways. If you're asking this question, then you probably should be using the API methods. To specifically answer your question, 1 method. Each method is an extra jump in memory, which is more expensive. It's not really worth writing two separate methods for this. For sure I use the API for this, only because you coded them and I hope I do have such experience as you. I am just asking this question in general, and eventually for such problems, I will be using the 3 variables method with a statement inside to check for the variables being given or -1. Thanks so much again! Edit: As of your reply to Athylus. I am wondering if it won't be a method then what would it be? Edited June 22, 2018 by k1595981 1 Quote Link to comment Share on other sites More sharing options...
Explv Posted June 23, 2018 Share Posted June 23, 2018 16 hours ago, Athylus said: If you look at it from a lower level then it is more expensive. Correct me if I'm wrong, but your program would have to jump to the other function by accessing the pointer that refers to memory where your method is placed. How the inner workings of Java handle that is still a mystery to me, but looking at it from an assembly perspective there are definitely some more instructions happening. The question is, will it make or break your program on a multi-threaded 3 GHz processor? The performance impact is insignificant. Especially as we're talking about OSBot scripts here. 17 hours ago, k1595981 said: Is it better to code two functions, With just one parameter (String item) that will get a random x and y positions of the item and click With three variables (String item, int x, int y) if I want to to click in a specific position or just having one function which is the second one, and if I pass -1,-1 it generates a random position of the item rectangle. I know that both ways will work but imagine a function with 100 lines, then another with 100 or just 1 function with 100 and an extra check for the parameters. So how is such thing done in programming properly? In my opinion two functions is much better for readability and usability. Performance should not be a consideration whatsoever here. And you wouldn't have two functions with 100 lines of code, the functionality shouldn't be duplicated, it would look like: public boolean interact(String itemName, String action) { // Generate x, y point interact(itemName, action, generatedX, generatedY); } public boolean interact(String itemName, String action, int x, int y) { // Do interaction stuff } Generally you want to keep functions as simple as possible, a good rule to follow is that a function should do just one thing. If you only had a single function, it would be doing two things, generating a random point, and then interacting at that point. By defining separate functions it communicates more clearly what the intention is when you are making a call. For example: interact("Knife", "Use", -1, -1); It's not clear what the intention is here, you would need to go to the API docs, or look at the function definition to figure out what -1, -1 does. If you saw: interact("Knife", "Use"); The intention is clear, the "Use" interaction will be performed on the knife. 2 Quote Link to comment Share on other sites More sharing options...
k1595981 Posted June 23, 2018 Author Share Posted June 23, 2018 (edited) @Explv You are smart, nice one! Edited June 23, 2018 by k1595981 Quote Link to comment Share on other sites More sharing options...