Mission Impossible / How to create datatypes which cannot contain invalid state

adrianimboden
22.6K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

This example implements a serial port protocol which is request/response based. It assumes a that:

  • the send and receive is long running
  • the current status can be obtained for debugging reasons
  • calls to send_request are synchronized by the caller
Example

The problem here is, that we have three bool variables which would result in 2×2×2=8 possible state combinations, of which only 4 are used:

is_connectedis_sendingis_receiving
falseignoredignored
truefalsefalse
truefalsetrue
truetruefalse

This leads to the following results:

  • A person which reads the code has to look up all the various asserts in order to understand the existing invariants
  • A person which debuggs the code has to keep values of supposedly relevant variables in his head, which are actually not used
  • It is very easy for a different programmer to change the code in such a way that the previous invariant is no longer given

Now try to refactor the SerialPort example to use an enum instead.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content