Class size to make the most of OOP

This might seem like a stupid question, Im trying to dicsuss now with you how you see on the subject and im not after a real answear. What i want is to build my own view on the following subject.

EDIT: For you who didn’t know already OOP stands for Object Oriented Programming

When is a java class becoming too big?
How many lines / methods do you try to make per class?
How do you think the above question effecrts your programming?
Do you prefer smaller or bigger classes?
Do you consider OOP be few big classes or many small classes?
Does having many small classes affect you negative?
Does having many big classes affect you negative?

Hoping to start a discussion with spread oppinions :slight_smile:

You are thinking about this entirely wrong. It’s not about the size of classes, it’s about making the correct sized classes to have working, efficient and managable code.

[quote] Im trying to dicsuss now with you how you see on the subject and im not after a real answear.
[/quote]
Thats what i said. I want your oppinions on the topic. Im not thinking of that classes have to be this or that size…

For me, it depends completely on the purpose of the class, generally I will have a couple “big” classes (this is all relative btw) then the bulk of the code in several “medium” classes and then a scattering of small classes serving as data structures. But all of this is just how the code manifests itself. I do not plan anything at all on a large scale (at least in so far as what classes is concerned, often I have quite a problem coming up with a good name for a class). A class should serve a purpose or represent a single thing. You should make it as long as is required to serve the purpose and no longer.
Einstein: [quote]We should make things as simple as possible but no simpler.
[/quote]

I have found myself moving towards classes with fewer but longer methods. The main reason for this is thinking about objects in terms of domain behavior rather than data + functions.

  • I no longer reflexively create getters and setters for all of my fields. I only create one if it is absolutely necessary.
  • I no longer write methods “just in case”. I don’t write a method until there is behavior that I actually need that doesn’t currently exist.
  • I no longer preemptively break methods into small pieces. I only pull code out of a method if that code is going to be called from multiple places, or if it’s something that needs to vary between subclasses.
  • I take “Tell don’t ask” more seriously.

I found that doing this has made my code easier to understand and the interactions between objects are simplified.

  • do not create God classes
  • methods should be comprehendible at sight
  • but do not split up algorithms for no reason
  • clearly assign tasks and responsibilities to classes and methods, one for each class or method preferably
  • look up separation of concerns
  • as few dependencies as possible