user656523
user656523

Reputation: 3361

Java - performance consideration when using multiple methods

I have a pretty complex java class with over 60 small helper methods used for easy readability and understanding.Just wondering, do you think having these many methods in a class could affect performance in any way?

class MyClass() { 

    //Some static final variables 

    MyInnerClass m = loadAllVariables(); 

    private void update(){ 
    } 
    ... 
    //All 60 methods come here 
    .... 
    private MyInnerClass loadAllVariables() { 
        MyInnerClass m = new MyInnerClass();  
        //load all variables with pre-initialized values 
    } 

    private MyInnerClass() { 
        int a, b, c; //In reality, I have over 20 variables 
    }  
} 

Upvotes: 1

Views: 429

Answers (5)

cruftex
cruftex

Reputation: 5723

Sorry folks, the answers are wrong or not addressing the really important point.

All methods in the class above are private. This means there is no polymorphism going on and the JVM can invoke the method directly and not via invoke dynamic. "private" implies "final".

Further more, all the tiny methods get inlined by the JIT. So there is no performance difference.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533520

No. The number of methods in a method doesn't matter much. Only the methods used are really loaded. If you have thousands of methods this is more likely to be a problem for you (the developer)

Keeping your code simple and clear is more likely to improve performance.

Upvotes: 4

Kiril Kirilov
Kiril Kirilov

Reputation: 11257

May be you need to use Extract Class. It is common methodology for solving Feature Envy code smell (these are both taken from Martin Fowler's Refactoring).

About the many methods in one class - I reacall that I've read somewhere that the JVM will optimize additionaly your code, will inline methods, etc.

Upvotes: 1

hqt
hqt

Reputation: 30276

It depend !!!

First, It likes Peter Lawrey has said, just the methods you use in your answer will take some time. for example:

public void update(){
 if (sth) methodA();
 else if(sth other) methodB();
 else if(sth other) methodC();
}

in above example, always one method will be called.

Seconly, It depends on your platform. If you are developing for desktop, 60 methods make nonsense, unless you use thousand and thousand methods. But if you are developing on Android, 60 methods for one update() function really a big problem. That why many expert say you shouldn't use Getter/Setter on Mobile Platform. And of course, this work will anti again Design pattern :)) That the reason why when you develop on Mobile Platform, you will have hard choice between performance and Maintenance. (It means your code will be clear and easily to read )

Hope it's clear for you :)

Upvotes: 0

Nikola Yovchev
Nikola Yovchev

Reputation: 10226

Performance wouldn't be really affected that much. Just make sure you are not implementing the God-anti-pattern.

From an engineering perspective, it might be confusing for other developers to navigate through your complex hierarchy of inner classes.

Upvotes: 2

Related Questions