Constructors should only assign the given arguments to fields and, where needed, validate these beforehand. This article describes among others the advantages of dependency injection.
The work that our program needs to do does not belong in objects’ constructors, it belongs in their methods. The constructors are only there to inform and enforce what a class needs in order to work.
Construction is an own responsibility
If a dentist wants to set up a new office, he might have a list of rooms that he needs, but he will not build the house himself. That’s a different job, requiring a different skill set.
In the same way, an object should not itself be responsible for creating the data or other objects it needs. That should be done by an own object, or a whole structure thereof – or by a dependency injection framework, which is an whole own tool specialized to do this job.
new is Glue
Every time you use
new in your code, that code is tied down to that specific implementation – you can neither switch the thing out, nor use the same thing just with different parameters. If you repeatedly use
new in constructors, you will create a chain of specific implementations. And because you can’t “have” an instance of a class without constructing it, you can’t separate that chain.
Next Article: Keep Methods Short