aspose file tools*
The moose likes Java in General and the fly likes Maximum Size of Java Class and Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Maximum Size of Java Class and Method" Watch "Maximum Size of Java Class and Method" New topic
Author

Maximum Size of Java Class and Method

Selvakumar Kumar
Ranch Hand

Joined: Feb 18, 2007
Posts: 36

Hi,

I am trying to maintain a legacy application. Already the number of lines in a particular method is over 10000 lines. 10418 to be precise.
We ended up with this many lines after a code merge activity from Parent branch.

So what is the maximum number of lines that Java can accommodate in a method and fro my information in a class.

Thanks,
Selvakumar

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

There is a limit on the size of a method, but it isn't measured by the number of lines. It's something more like the number of bytes in the compiled version. Or something like that... why do you ask? Were you planning on making the method even larger? I would suggest that your goal should actually be to reduce the size of the method.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14429
    
  23

I would really advise you to refactor that method. More than 10,000 lines in one method? I've never seen anything like that. Code like that is totally unmaintainable and untestable.

At the place where I'm currently working, we are striving to have no methods longer than 20 lines, and no classes longer than 200 lines.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 1231
    
  67

So what is the maximum number of lines that Java can accommodate in a method and fro my information in a class.

Far more than any human can accommodate. Take Paul and Jesper's advice and refactor towards significantly reducing the amount of code in that method/class.

Follow the advice of Uncle Bob: "Functions should do one thing. They should do it well. They should do it only." -- Clean Code (Robert C Martin)


Tim Driven Development
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Java Virtual Machine Specification §4.11 (I think).
And a 10000‑line method is beyond the bounds of “beginning” so I shall move this discussion.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8415
    
  23

Jesper de Jong wrote:At the place where I'm currently working, we are striving to have no methods longer than 20 lines, and no classes longer than 200 lines.

I don't want to deraill the discussion but, while I can certainly see the point of the first target, I'm not so sure about the second. Indeed, I think you might be very hard-pressed to implement a List in only 200 lines - especially if you intend to document it properly - simply because of the number of methods you're obliged to implement.

@Selvakumar - Just to give you another yardstick: My usual rule of thumb for a method is one "page", or screenful, of code (ie, not including docs) - or, if you prefer, as much as your eye can scan without scrolling - which generally means an upper limit of 40-50 lines.

A couple more for you:
1. No more than two major levels of indentation.
2. No more than 3 parameters.

None of the above is hard and fast, but if you find yourself exceeding them, it's probably worth looking to see if you can break the logic up further.

And finally, I'd say that Tim's "Uncle Bob" quote is the most important one of all to keep in mind.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1836
    
    7

I'm currently staring at a generated .java file that contains OR mapping information, which clocks in at just over 44000 lines of code.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Generated code is exempt from all style rules.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8415
    
  23

Jelle Klap wrote:I'm currently staring at a generated .java file that contains OR mapping information, which clocks in at just over 44000 lines of code.

Ooof. Sounds like the multi-thousand-page financial reports we used to produce back when block printers were the norm and trees were more plentiful.

I guess you have to presume that your generator knows what it's doing...

Winston
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

some times it cannot be helped, but if i find one of my methods exceeding 50 lines or so i take a closer look at it. same thing if a class gets over about 300 lines. 10000 in one method! there must be a way to refactor that.


SCJP
Visit my download page
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8415
    
  23

Randall Twede wrote:10000 in one method! there must be a way to refactor that.

Nope. It depends entirely on what it's doing.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
If you have automatically‑generated code which works, why refactor it?
Selvakumar Kumar
Ranch Hand

Joined: Feb 18, 2007
Posts: 36
Thanks for all the suggestions and answers guys.

This method has a series of SWITCH/CASE statement. There are over 200 CASE statements. I need to add a new CASE for my condition.

But when I try to compile the code after I added my case, I am getting a compilation error.
As many of you have suggested I need to create a new method and create my CASE condition there.

I was just looking to get to know how many lines of code the method can accommodate.

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
And what is the compiler error?
 
Consider Paul's rocket mass heater.
 
subject: Maximum Size of Java Class and Method