Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maximum Size of Java Class and Method

 
Selvakumar Kumar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Cooke
Sheriff
Pie
Posts: 3146
136
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently staring at a generated .java file that contains OR mapping information, which clocks in at just over 44000 lines of code.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generated code is exempt from all style rules.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have automatically‑generated code which works, why refactor it?
 
Selvakumar Kumar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what is the compiler error?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic