We’ve Been Teaching Object-Oriented Programming All Wrong (Part 1)

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism


The traditional way that we think about OOP is simply too rigid. There’s a term for this called being an object bigot. Quite frankly, it’s a very impractical way of thinking about writing code and helping junior developers adopt object-oriented practices which exist on a spectrum and should be applied or layered as the problem domain demands. In other words, it’s not always necessary to build solutions in a strict and rigid object-oriented way.

Two utility functions for working with strings in a utils.ts file.
A very loose “encapsulation” of the functions with a wrapper class that does nothing else aside from providing a context.
Note how compact the auto-completion list is once I have the object instance.
The usage of a purely functional approach makes it hard to find what you need.
We can reuse this pattern within the scope of the class.

Avoiding Divergent Behavior

You’ll note that I used the term “encapsulation” in quotes above. This is because we’re really just scratching the surface.

A very typical pattern to see in TypeScript.
Courtesy of CeDAR: Center for Dependency, Addiction and Rehabilitation. With Transaction Scripts, the end result is often an anemic domain model where the specific transaction modifies the state of the entity — possibly in a different way than another transaction. This is an external locus of control as the entity’s state is mutated by external interactions.
Because we’re using our objects a simple maps, we would need to externalize the logic for validating the formatting of the hours to make sure it’s set correctly.
Note how the check for a valid user is now owned by the object itself.
We use the constructor to manage normalization of the data (and add more data validation!). This can be a very powerful pattern when working with document oriented databases to help isolate schema changes by encapsulating the data normalization behavior.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Charles Chen

Charles Chen


I Make Software ▪ Currently obsessed with C#, .NET 6, TypeScript, Vue.js, NoSQL, and serverless!