Emergent Architecture

Hexagonal Architecture sounds mysterious and cool but isn’t really anything magical. I see it as as the natural consequence of applying the Single Responsibility Principle.

If your objects have a single reason to change then the impact of those changes on collaborators should also be minimized. Hexagonal (emergent) architecture is one of the means to that end.

By exposing a well defined set of interfaces  and clearly defining the messaging patterns between  objects we get increased cohesion across the system.

Because hexagonal architecture is a consequence of SRP, I’d argue that it should be a result of refactoring towards SOLID rather than a pre-planned approach.

In my view, it’s more beneficial  first build  a cohesive monolith and slowly introduce ports and adapters as the patterns of communication between objects  emerge  and crystalize.

Too often I’d find myself trying to introduce ports and adapters prematurely, which led to BDUF and decreased productivity. While we definitely should strive to code towards abstraction and not concretion , it may take a few concretions to arrive at the proper abstraction.  BDD can help us there.

If we think about of our specs as the interface to which our objects must adhere than the jump between concrete and abstract is not so daunting.

In his excellent talk “Barely Enough Design” the amazing Marcello Duarte explains this  so well  https://www.youtube.com/watch?v=NQN72Big8ww

Once we build our abstractions and define our ports, it’s important to remember that any of the adapters plugged in should still pass the specs unchanged.

I believe this is a  tip of the hat to Barbara Liskov and the L in SOLID. Here of course we’re talking about behavior and not direct inheritance, however, the idea of re-use stands: “Any adapter plugged into a port should communicate the same way”

It’s amazing to me how all the the basic principles of OOP scale upwards towards component and system design as a whole. Of course as we enter different levels of abstraction there are a couple unique twists but in general  the core principles apply.

I’ve been thinking about microservices as well and how they fit in the big picture. I’m also working on a tutorial series on refactoring towards hexagonal.

How do you structure your apps, do you employ hexagonal architecture right away? Let me know in the comments below


Update: Given awesome feedback from Marcello Duarte (@_md), i’ve decided to rename the post to emergent architecture vs hexagonal. Thank you so much!



Share This: