What do you actually gain from object-oriented design, and at what point does the style start costing more than it returns?
You gain four things, and they all come from one source. The code that uses a thing does not have to know how the thing works, so you can change the inside without breaking the outside.
So here are the four gains.
the gain what it means in practice
------------------------ ---------------------------------------------
the code is easier to one new class instead of an edit in
change ten places
the parts can be tested you can supply a small stand-in
on their own for the slow or expensive part
the state stays valid the class checks its own data
the parts are reused you compose existing classes instead
of copying code
And here is where the style starts costing more than it returns.
the sign it is costing too much
----------------------------------------
a class with one field and one method
a getter that only returns the field
a factory that only calls a constructor
an interface with one method that passes everything through
a hierarchy where the child uses half of the parent
And there are three checks you can do to see whether a class is earning its place.
The first one is to count the real methods. If a class has a constructor, a getter and a setter and nothing else, then it is a bag of data and you could have used a record or a simple class with public fields. The class is not adding anything.
The second one is to look at whether the class has behaviour of its own. If the class only holds values that other code reads and writes, then the rules about those values are somewhere else, and you have not gained the safety that you wanted.
The third one is to count the layers. If you have to open five files to find out what a function does, then the abstraction is costing more than the clarity is giving back.
// A class that is probably too thin.
public class UserId {
private final long value;
public UserId(long value) {
this.value = value;
}
public long value() {
return value;
}
}
And the honest answer to the question is that the style pays off when the code has more than one author, more than one way of doing a job, or a life of more than a few months. Below that line, plain functions and simple data classes are better and they cost less to read.
So the rule to remember is this. Keep a class if it has behaviour, a rule of its own, or more than one implementation.
The last question in this topic lists the advantages and what you give up for them.