Boundaries

A nice talk I watched: Boundaries by Gary Bernhardt, on values, isolation and testing.

A nice talk I watched: Boundaries

I recently watched Gary Bernhardt's talk Boundaries (YouTube) and it was epic. Here's what stuck with me.

Isolation is key

In a unit test, only the code you actually want to test should run. Everything else is a mock or a stub.

The catch: if you don't mock or stub correctly, production behaves differently from your test doubles. Your tests pass, prod breaks, and the tests never caught it. Sad.

Integration tests are a scam

The natural answer is integration tests, but with N conditionals you get 2^N paths to cover. Every bit of new production code adds paths, and every path needs a test, so the suite gets slower and slower.

One alternative is to enforce external contracts by writing mock implementations of your interfaces. It works, but it's a lot of ceremony.

Values as boundaries

This was the big idea of the talk: values are the boundaries.

If the pieces of your system talk to each other by passing plain values, you get isolation for free. No mocks, no stubs. Just data in, data out.

Shell and core

  • Core: the business logic and decisions, for example "what does an expired user look like?"
  • Shell/glue: the code that touches the nasty outside world (network, disk, state storage).

We want to hide that outside world from our critical business code. This lines up with Ports and Adapters, and with the idea from Clean Architecture that the UI and the database are plugins to the business rules.

The example in the talk:

Sweeper <-user value-> ExpiredUsers <-user value-> Mailer

ExpiredUsers holds the decision logic and only ever sees user values. The sweeper and the mailer handle the dirty work on either side. That means the core can be tested without a database or an SMTP server.

Takeaway

Pure values at the boundaries, side effects at the edges. Fast, isolated tests follow naturally.