This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Maximum Size of Java Class and Method

 
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

 
Sheriff
Posts: 24654
58
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
Sheriff
Posts: 4673
308
IntelliJ IDE Clojure 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)
 
Marshal
Posts: 65787
250
  • 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.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
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
Marshal
Posts: 65787
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generated code is exempt from all style rules.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
Ranch Hand
Posts: 4702
9
Scala Java
  • 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
Posts: 10777
71
Hibernate Eclipse IDE 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
Marshal
Posts: 65787
250
  • 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
Marshal
Posts: 65787
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what is the compiler error?
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!