Definition

Most object-oriented languages (some more than others) have some kind of type system. This principle simply says that we should use the type system of the language, instead of inventing our own.

What does it mean “to invent your own type system”?

Whenever you see a class with a field akin to “type”, you're not using the type system of the language, but you're attempting to create your own “type system” at runtime.

Problems and solutions

Let's consider the following pseudo-code:

How not to model types, details skipped for brevity

class ChessPiece
    public TYPE = "white";
 
    public firstMove(position)
        if TYPE == "white"
            this.move(position)
        else
            throw new Exception("black is not allowed to move first")

In the example above, the red flag is the field TYPE.

What you should do instead is create two classes, one called WhitePiece which has a method firstMove, and one for BlackPiece, which doesn't – because the domain, the rules of the chess, are dictating it.

Some of the advantages of doing so:

  • you're less likely to create bugs
  • the autocomplete feature of the IDE is helping you to type more correct code