• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is a 350 line class too big?

 
Bernhard Woditschka
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
while fideling around with the flight reservation gui I ended up having the folowing set of classes on the frontend:
The Client:
TravelAgent implements the busines logic for looking up airports, flights and to make reservations.
The GUI has some model classes:
OriginModel
DestinationModel
FlightsModel
and a custumized JTextField:
IntegerRangeTextField
I think they are of reasonable size and well encapsulated.
The lase class sets up the GUI components layout and "glues" together the gui classes with the help of anonymous inner classes (ActionListener, FocusListener ...)
It ended up to be about 350 lines of code wich suggests to me it's too big.
What's your opinion?
Did I follow the "common" patterns?
Thanks for your help,
cheers, Bernhard
 
Aruna Raghavan
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, too big. If you are used to using java code review tools JLINT/PMD, they tell you not to make classes ( or even methods for that matter) too big. It makes it hard to follow the code.
 
Samual Harvey
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it may be big in the review world but its very hard to keep the number of lines under 350 for some classes. I mean the Data class provided by SUN is more then that and if you choose to modify it by adding a function or two, its even more. In the practical world sometimes even the interface class contaning the constants gets bigger then this. I think once you review the code you can know if the code lines can be reduced or not.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The lase class sets up the GUI components layout and "glues" together the gui classes with the help of anonymous inner classes (ActionListener, FocusListener ...)
It ended up to be about 350 lines of code wich suggests to me it's too big.

Don't take the "if the class is too big, it's bad" recommendation too literally. Normally, this recommendation applies to incoherent classes that put together functionality that belongs to different classes. But if your class defines a narrow set of responsibilities (such as your class that sets layout of components and instantiates these components), let it be 10,000 lines long, it is still good coding.
The key here is not the size of a class, but how well it is encapsuletd and decoupled from everything else.
Just for reference, my mainframe class in the form of "public class MainFrame extends JFrame implements FlightsModelListener" was 425 lines long.
If you need more convincing arguments, look at the source code of Sun java classes. The "String" class, for example, is 2,355 lines long, and JTable is 5,868 lines of code. If it takes that long to describe a good class, so be it!
Eugene.
[ December 29, 2002: Message edited by: Eugene Kononov ]
 
Bernhard Woditschka
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies,
I've now got a first working version of the GUI, but will refactor it in the next cycle keeping a focus MVC and encapsulation.
cheers, Bern
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic