Mistake proof software design

It’s The System’s Fault

Developer Mistakes Point To System Design Issue

Faulty design

Game game = new Game();
game.moveO();
game.moveO();
game.moveO();

class Game{
    Game(Player playerX, Player playerO);
    move();
    ...
}

void moveX(){
    if(currentPlayer != Player.X){
        throw new NotTheTurnOfThePlayerException();
    }
}

Eliminate Exceptions

class Game{
    Game(Player playerX, 
         Player playerO);
    move();
    ...
}

Pass Mandatory Arguments In Constructor

Game game = 
new Game(firstPlayer, 
         secondPlayer); 


game.addPlayer(thirdPlayer);
game.addPlayer(fourthPlayer); 
game.move();

Avoid Primitive Obsession

game.move("A1");
game.move(0, 0);

Place place = 
new Place(Coordinate.One, 
          Coordinate.One);
game.move(place);

Recap

  • it’s typically the system’s fault
  • design your software with fault tolerance 
    • Eliminate Exceptions
    • Pass Mandatory Arguments In Constructor
    • Avoid Primitive Obsession

Further reading

  • CHECKS pattern http://c2.com/ppr/checks.html
  • Design by contract
Made with Slides.com