2

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.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
TAAPSogeking
  • 350
  • 4
  • 14
  • 1
    What do you mean by efficient? Runtime performance? Time taken to write / maintain the code? – Stephen C Aug 24 '13 at 03:25
  • you might want to check out [guava functions](https://code.google.com/p/guava-libraries/wiki/FunctionalExplained) – AdamSpurgin Aug 24 '13 at 03:27
  • @Stephen C Runtime performance – TAAPSogeking Aug 24 '13 at 04:00
  • According to [this](http://stackoverflow.com/questions/13897784/how-is-physical-memory-organized-between-base-and-derived-class-instances-in-jav), 1 is the best answer. – TAAPSogeking Aug 24 '13 at 23:21
  • Nothing in that question or its answers says any such thing. That's just a conclusion you have drawn on unstated grounds. It sees,to me you need to learn a lot more about language design and code generation before you commit yourself too far on this project. – user207421 Aug 25 '13 at 00:15
  • @EJP What I got from it is that extending a class uses "one piece of memory." To me that means that the memory usage would be about the same as the way I originally had it. If I am wrong, what is the most efficient way then? – TAAPSogeking Aug 25 '13 at 00:21
  • 1
    I'd first devise some sort of table-driven approach, then work at improving that. Any time you deal with a large `if` ladder maintenance becomes a nightmare (not to mention comprehension). – Hot Licks Aug 25 '13 at 01:15
  • @HotL Are you saying to make an array of the method names and use a for loop to search through them? I don't understand how that would help in this case. My info about the table-driven approach was gotten [here](http://www.roboprogs.com/devel/) – TAAPSogeking Aug 25 '13 at 01:24
  • If there is no saving, this not on any grounds the 'most efficient way' or the 'best answer'. Just an answer, and it seems to me a most inconvenient one. – user207421 Aug 25 '13 at 01:34
  • If you make an array of method names you can use a hash table or some such to do the lookup -- much faster than `if` statements and easier to maintain. – Hot Licks Aug 25 '13 at 02:29
  • @HotL But wouldn't I still need the if-else statements to find the right method? I don't think there is way to call a method from a string. – TAAPSogeking Aug 25 '13 at 04:16
  • 1
    Look up [hash table](http://en.wikipedia.org/wiki/Hash_table). It's a concept every software developer should be familiar with. And understand that Java has [several to choose from](http://docs.oracle.com/javase/7/docs/api/java/util/Map.html). And look up "reflection" for one way to invoke a method by string name. (You can also use simple wrapper methods and lots of individual classes.) – Hot Licks Aug 25 '13 at 12:43
  • (At the very least, consider converting your string name to an index (via hash table) and using a `switch` statement. And there are rumors that the latest Java versions will let you `switch` directly with a string, though I've never tried that.) – Hot Licks Aug 25 '13 at 12:48

1 Answers1

10

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • So none of them will really slow down my run time performance. For the record, the bulk of the code is already written. – TAAPSogeking Aug 24 '13 at 15:08
  • 4
    @TAAPSogeking - *"So none of them will really slow down my run time performance."*. I didn't say that. What I said that you should spend your time on **more important things** ... for now. Get something implemented before you worry about performance. – Stephen C Aug 24 '13 at 16:23
  • @TAAPSogeking - Profile it. – Stephen C Aug 25 '13 at 00:31
  • 1
    @TAAPSogeking - I mean this: http://stackoverflow.com/questions/619898/what-is-profiling – Stephen C Aug 25 '13 at 00:36