Factory Functions vs Classes: JavaScript

Recently while trying to understand clean code in JS, I ran upon the differences between factory functions and classes and how they affect the code readability, refactoring and the general advantages and disadvantages of using each.

Factory Functions:

Let’s start with factory functions, quite simply functions in JS can return objects, when they are neither constructor functions nor classes, it’s called factory functions.

An example of a factory function would be:

The above code creates an object named dog1 with a function that logs something in the console.

Benefits of Factory Functions:

Factory functions are very flexible and they don’t lead us into the dark road of inheritance, deep inheritance hierarchies specifically. The other benefits include:

  1. Fewer problems with refactoring issues: For starters, we will never have to convert factory functions to classes or constructor functions so automatically refactoring is much less of an issue.

  2. No ‘new’ keyword: No ambiguity in using the ‘new’ keyword.

  3. this’ behaves in a standard manner:this’ behaves as it normally would, so you can use it to access the parent object. Also, inside any function call of an object of the factory function, ‘this’ will work as it normally should.

Drawbacks of Factory Functions:

It may perform slower than a constructor function in micro-optimization benchmarks. The slow path is still very fast — millions of ops/sec on an old computer. This is more likely to be a concern in library or framework code than in application code.

Classes:

Classes are prototypes of something and they usually have member variables and member functions, to create an object and execute its behaviour.

An example of class would be:

The above example works similarly to the above factory function but obviously this has a different syntax and how the object is created, especially the use of the “new” keyword.

But the code works the same i.e., we create an object dog2 that executes the function bark when called and prints something in the console.

Benefits of Classes:

  1. ’this’ refers to the new object.

  2. There may be a micro-optimization performance benefit, but we should not worry about that unless we have profiled your code and proven that it’s an issue for us.

  3. Convenient and self-contained syntax.

  4. More familiar to people with a class-based language background.

Drawbacks of using Classes:

  1. The temptation for users to create problematic class hierarchies using inheritance.

  2. Required ‘new’ and the ambiguity it brings with it.

  3. Classes break the Open / Closed principle of clean coding.

Conclusion

In my opinion and experience, factory function and functional programming as a whole are far more convenient, easy to maintain, and easy to refactor and debug, with much fewer downside or performance issues.

For larger codes, these problems are even more evident and it can be seen when inheritance especially can cause a much bigger problem for programmers.