Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the most efficient way to organize a lot of methods?

I am making a mock-java langue, which has a lot of methods. I would group similar methods together and put them in their own class, but all of the methods need to extend a class that deals with how to interrupt data. What is the most efficient way, in terms of runtime performance, to organize them?

Right now it is just a bunch of methods in one class (ListOfMethods) called with a special method with if-else statements to find the right method, like:

public void methods(String name, String param) throws NumberFormatException, InterruptedException {
    if (name.equals("systemexit")) SystemExit(param);
    else if (name.equals("sleep")) sleep(param);
}
public void SystemExit(String param)
{
    int exit=(int)Double.parseDouble(param);
    System.exit(exit);
}
public void sleep(String param) throws NumberFormatException, InterruptedException
{
    Thread.sleep((long)Double.parseDouble(param));
}

I am not trying to change this format, just how the methods are organized. I want to group them into classes, like math and strings, but I am wondering the best way to do this?

  1. ListOfMethods could extend each class. The problem is that Java does not support multiple inheritance, so class 1 would extend class 2 which would extend class 4, etc.
  2. ListOfMethods could import each class. The problem with this is that they all need to extend a super class. Is that inefficient?
  3. Placing them into different classes and making the methods static (very similar to the previous one).
  4. Simply leaving it as is.

I am open to other suggestions - I want to make it as efficient as possible. Thanks in advance.

Note: there will be hundreds of instances of this class running at once - this is just a short example of the actual problem - so I would really not want to waste memory.

like image 475
TAAPSogeking Avatar asked Feb 04 '26 17:02

TAAPSogeking


1 Answers

My advice would be to ignore runtime efficiency for now and concentrating on getting your language working. When it is working, profile it and use that to decide what needs to be made (more) efficient. Right now, it is impossible to predict what the actual efficiency concerns will be. Effort you spend on this issue now will most likely be wasted.

So my suggestion is option 4.

Or option 5 - in the first instance, pick the approach that you think is going to make writing and maintaining the code simplest.

like image 180
Stephen C Avatar answered Feb 06 '26 07:02

Stephen C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!