“Construction site” image by Hugh Dutton AssociĆ©s

In a Pluralsight course by John Papa covering Angular 2, he made an offhand comment about how constructors should be kept as simple as possible, and though it was the first time I had heard this philosophy, it made a lot of sense. Brendan Enrick on his blog post from back in 2009 goes into more detail that really reinforces the idea, but it boils down to making your objects easy to use in testing and to avoid hidden complexity.

Objects get instantiated in a lot of places, especially in frameworks, and stack traces become a lot less intelligible when logic inside the constructor blows up while connecting to a database or constructing an elaborate object graph. Constructors should be limited to setting fields. Anything more complicated should be calculated outside of the object and provided as an argument (perhaps through a factory), or the logic should be performed in a separate method to be called after object instantiation. Keep your constructors dumb to keep your objects happy, and put the smarts somewhere else.

(image credit: Hugh Dutton AssociƩs)