Constructors Considered Harmful

Panu Viljamaa
Sep 3, 2018 · 3 min read

This article is about JavaScript/ES6 custom constructors. Using static methods to create instances is better than defining ES6 custom constructors, in most cases.

Why? Using class-methods to create instances allows you to have multiple different methods for creating instances, from any number of different argument-types. And because it allows you to create a subclass which is not dependent on the data/state in the super-class. You’ll get less coupling between supers and the subs. Less coupling means less fragility for your program.

There can be only one constructor per class, which is also tightly coupled to the super-class. Why? Because it must call the super-constructor if it has a super-class, with the type of data that expects. That is how subclass-constructors in ES6 work.

Creating a custom constructor makes the implementation of the subclass constrained by (and thus dependent on) the implementation of the super-class, the data-structures and helper objects inside it.

Depending on the structure of data inside super-class is bad because data should be private, only accessible via a public method-call interface — so it can be changed later without affecting users of that class.

THEREFORE: NEVER CREATE CONSTRUCTORS! They are not needed. Not since ES6 classes came into being.

You still need to create instances however, somehow. You do it with plain old:

new MyClass();  // No arguments please

System provides the default constructor for you behind the scenes. Or maybe it just behaves LIKE it does. We don’t know. We don’t need to know. All we care is we get a brand new instance, somehow, and above is the syntax for creating one.

This article could have been named “Some (namely Custom- ES6- ) Constructors Considered Harmful”. But I would argue that from the application developer’s point of view there are no other “constructors” than custom ones. And the same concern of avoiding data-dependency from the subclass to superclass should apply to other Object-Oriented programming languages as well. Fragile Baseclass Problem (https://en.wikipedia.org/wiki/Fragile_base_class ) is much caused by incompatible data or state in the superclass.

ES6 may be special in that sub-constructors must call their super-constructor always. In some OO languages, Smalltalk in particular, there is no concept of ‘constructors’ at all. There is only the built-in method #basicNew for creating new instances. This keeps the language simpler and thus programs written in it simpler. And thus less error-prone.

We don’t need no (custom) constructors, we have static methods.

Just say NO to constructors

P.S. What may not be obvious from above is that default constructors are created automatically. Therefore creating static methods to create instances in many cases is no more work than creating a custom constructor would be.

If you use static methods to create instances you must call the default constructor. But if you create a subclass constructor it must call the super-constructor. So about the same amount of code. Using only static methods keeps you program simpler, more uniform, easier to explain.

P.S.2. The other main benefit of using static methods instead of a custom constructors is static methods can return anything. They can return null. They can return a String. They can return 42. Constructors can’t. Less limitations give you more flexibility in program design, more agility!

P.S.3. Is there ever a case where you should create a custom constructor rather than a static method? There may be. I’m not saying there isn’t, because it’s hard to prove the absence of something in an expanding universe. Never say never. So the advise in this article should really be taken as: “Before you create a custom constructor ask whether you really need it, or whether a static method would be a better”. I believe static methods are better in most cases. Constructors bring in complexity and more coupling between sub-classes and their supers.

UPDATE: Never say never. I think I’ve now found one good use-case for custom constructors. I will write about it in my next medium post, stay tuned …

Panu Viljamaa

Written by

Class Cloud LLC

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade