Skip to content

Latest commit

 

History

History
126 lines (81 loc) · 4.17 KB

File metadata and controls

126 lines (81 loc) · 4.17 KB

Code Smell 23 - Instance Type Checking

Code Smell 23 - Instance Type Checking

Do you check who are you talking to?

TL;DR: Trust your collaborators. Don't check who they are. Ask them to do instead.

Problems

Solutions

  1. Avoid kind, isKindOf, instance, getClass(), typeOf, etc..

  2. Don't use Reflection and Metaprogramming for Domain Objects.

  3. Replace IFs with polymorphism.

  4. Avoid checking for 'undefined'. Use complete objects, avoid nulls and setters, favor immutability and you will never have undefined and ifs.

Sample Code

Wrong

if (typeof(x) === 'undefined') {
    console.log('variable x is not defined');   
}

function isNumber(data) {
  return (typeof data === 'number');
}

function move(animal) {
  if (animal instanceof Rabbit) {
      animal.run()
  }
  if (animal instanceof Seagull) {
      animal.fly()
  } 
}
 
class Rabbit {
  run() {
    console.log("I'm running");
  }  
}

class Seagull {
  fly() {
    console.log("I'm flying");
  }  
}

let bunny = new Rabbit();
let livingstone = new Seagull();

move(bunny);
move(livingstone);

Right

class Animal { } 

class Rabbit extends Animal {
  move() {
    console.log("I'm running");
  }  
}

class Seagull extends Animal {
  move() {
    console.log("I'm flying");
  }  
}

let bunny = new Rabbit();
let livingstone = new Seagull();

bunny.move();
livingstone.move();

Detection

Since type checking methods are well known it is very easy to set up a code policy checking the uses.

Tags

  • Metaprogramming

Conclusion

Testing for a class type couples the objects with accidental decisions and violates bijection since no such control exists on real-world. It is a smell our models are not good enough.

Relations

Code Smell 12 - Null

More Info

How to Get Rid of Annoying IFs Forever

Laziness I - Metaprogramming

Credits

Photo by Remy Gieling on Unsplash


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code