hexagonal soup

So if we can completely remove all duplicate conditionals, what will our code be like? Well the only remaining conditionals (if statements, switch statements etc) will be those that test something that’s set or defined outside of the system, ie. something that’s part of the runtime environment. The two or more code branches selected by such a conditional will tend to consist entirely of the creation/execution of different sets of objects. Each of these sets of objects must by definition ‘know’ what event or state occurred, probably because it was passed parameters that embody the environment’s state or prior decisions. And so within those invoked object sets there need be no conditional code at all.

This situation now dovetails neatly with Alistair Cockburn’s hexagonal architecture pattern…

The conditional statements themselves, because they form part of the response of the system to it’s environment, should be in the outer hexagon, in what Alistair calls the ‘transformers’ (and what I tend to call ‘adapters’). And if there really is no duplication of conditional statements, the inner hexagon (ie. the application and domain objects in Alistair’s model) will contain no conditional logic whatever! The inner hexagon is purely a soup of objects waiting to be invoked and parameterised in response to external stimuli.

So here’s a very simple rule you can apply to any system: Look at every conditional statement and check that it’s sitting in one of the adaptors connecting to the environment. If it isn’t it must be duplicating some knowledge that the system already holds elsewhere…


4 thoughts on “hexagonal soup

  1. Pingback: avoid boolean parameters « silk and spinach

  2. Pingback: Conditionals on the edge « silk and spinach

  3. Pingback: the anti-if campaign | silk and spinach

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s