Poka-yoke in software design

Ürgo Ringo
Wise Engineering
Published in
2 min readJan 8, 2019
Minimalistic/excellent murder mystery El cuerpo (2012) by Oriol Paulo

The idea of poka-yoke or mistake proofness even though originating from industrial processes and mass production is universal to any kind of design. It can be used equally well as a guiding principle in software design.

Good design is something that is easy to use correctly but hard to misuse.

We can reason about many things in software design using the idea of mistake proofness. One example is primitive obsession — using small value objects instead of primitives makes it harder to inadvertently use incorrect values. Another example is composing objects around invariants to guarantee that we can never take system into an invalid state.

Example

Lets say we want to implement currency conversion. The simplest solution could be something like this:

And here is implementation of Rate:

There are many problems with this design. One issue is that we have amount and currency that should be connected but there is nothing enforcing this. It is easy to swap USD and EUR as input parameters and even easier to forget the currency of the amount returned from conversion. To solve this we can introduce Money:

However, now client code needs to make sure that it uses this Rate only for converting the right pair of currencies. One way how to avoid that is to make Rate aware of the Route that it applies to.

Since Rate knows about its Route then there is no need to pass anything to convert except the money being converted.

We can make this even more misuse proof by enforcing this precondition in convert:

Now we still need to make sure that the Route used to create the Rate is the correct one — USD to EUR in this case. We can change this by getting Rate inside Money:

Of course this makes Money slightly more complicated but client code is now simpler. This may seem as just moving the complexity around. However, the difference is that now we can introduce bugs only once — while implementing Money as opposed to every time when doing the conversion between currencies in our application.

Summary

Good design is something that makes it hard to make mistakes. By using tools like intention revealing types, clear naming, enforced preconditions, easy to understand exceptions we can bring the idea of poka-yoke to software and make it more bug proof.

P.S. Interested to join us? We’re hiring. Check out our open Engineering roles.

--

--

Ürgo Ringo
Wise Engineering

Have been creating software for 20 years. Cofounded a software consultancy, worked as an IC and team lead at Wise. Currently working at Ibank.