It happens that having to call methods of a class in a particular order—unless there’s an obvious, hard dependency like “don’t invoke
Email::send()
until all the headers and body have been added”—leads to fragility. There’s no hint from the IDE about the dependencies or ordering.Worse, if a class has a lot of methods that must be called after setting some crucial data, every one of those methods has to check that the necessary data is set. It’s better to pass that into the constructor, and have the created instance always contain that data.
Given a class that wants to maintain a valid state for all its methods at any given time, how do setters fit into that class? It needs to provide not individual setters, but higher-level methods that change “all” relevant state at once.
So, I’m starting to see
get
and set
methods as an anti-pattern.