Code Tips #8: Null Object Pattern

Many times functions accept object references that can be null, and we tend to add if statements to treat the special case where null is passed to a function, and either provide a default response or do nothing. In the following example our calculateSpeed function expects an object that has a getSpeed function. In Javascript we will have to do something like:

class Car {
    return 100;

const calculateSpeed = function(vehicle){
  if(vehicle && typeof vehicle.getSpeed === 'function'){
    return vehicle.getSpeed();
  } else {
    return 50;

const car1 = new Car();

console.log(calculateSpeed(car1)); // 100
console.log(calculateSpeed()); // 50

But there is a better way to achieve that. Using the Null Object Pattern we can Continue reading Code Tips #8: Null Object Pattern

Code Tip #2: Isolate References to external classes

Imagine we have the following Vehicle class that has a dependency on the speed property of an Engine class.

class Engine {
    this.speed = speed * 1.2;

The Engine class accept a speed value and uses a factor to define the final speed. Its internals is not so important. Continue reading Code Tip #2: Isolate References to external classes

Rage against flexibility

Often while I am reviewing code I stumble upon pieces that are trying to do way more things than the requested feature demands. We all have been there, either reading or writing code that does more things than the ticket/issue we are working on requires. Many times we try to predict the future, because we guess that touching this specific part of the system now is easier than doing it later when a new requirement comes up. In my opinion this mentality is completely wrong and can lead to slowing down a team instead of moving things faster.

Let’s image that your boss asks you to create a component that accepts an array and renders a list of items, and you guess Continue reading Rage against flexibility

Abstract class in Javascript

Many object orientated language offer the notion of an abstract class.

An abstract class is, conceptually, a class that cannot be instantiated and is usually implemented as a class that has one or more pure virtual (abstract) functions. (source: wikipedia)


In C++ for example:


Java has also the concept of Continue reading Abstract class in Javascript