Reputation: 21
I want to know that many developers do the same thing as to decrease the size of long method , they create so many small sized methods from it for the same task. I want to know that does it affects the performance of an application or not?
Upvotes: 0
Views: 1865
Reputation: 608
Functions should normally be short, between 5-15 lines is my personal "rule of thumb" when coding in Java or C#. This is a good size for several reasons:
But I don't think it is helpful to set an absolute rule, as there will always be valid exceptions / reasons to diverge from the rule:
So basically, use common sense, stick to small function sizes in most instances but don't be dogmatic about it if you have a genuinely good reason to make an unusually big function.
Taken from here.
Upvotes: 5
Reputation: 1074989
It's impossible to say in the general case, but no, probably not.
For one thing: Method calls are really, really fast.
For another, the JVM monitors the work done by your code and when it sees a "hot spot" (a part of the code that gets run a lot), it aggressively optimizes that part of the code. One of the ways it does that is by inlining methods where possible — that is, by relocating the code from the method into the method that's calling it. At which point, the performance is the same as if you had put the code there yourself. So you get the maintenance and testability benefits of small methods, but the benefit of inlined code if it's useful to inline it.
Again, though, it cannot be answered in the general case. If you have a specific performance problem, benchmark to see whether it matters in that specific situation.
Upvotes: 2