Road with clear line between gras and tarmac

The integration operation segregation principle

Posted Mon Oct 08 2018

A few weeks ago I attended a DDDBelgium meetup where I was lucky to participate in a refactor workshop lead by Pim and Joop. After the incredible workshop Pim, Dries and I were discussing some code that we refactored earlier. Not so long in the conversation the words "Integration Operation Segregation Principle" casually got dropped by Pim.

Now I'm going, to be honest with you (as I was with them), I had no idea what the hell he was talking about. And maybe neither do you.

A simple price calculator 

Let's take a look at some code:

As you can see, we have a small piece of code that calculates the price for a few days of rental.

The code itself is pretty straightforward. We gather the start date, the end date. We calculate the number of days and later on multiply those days by the price + the km's driven. A small piece of code, that does a lot.

No code is complete without a test:

That's a big test.

It mocks a few things, then calls the calculator and asserts the results. All in all a very simple unit test, but not a fun one to write.

Turning code into a lot more code 

If we take a look at that PriceCalculator class you can see it structurally does 2 things: it fetches data and performs actions on that data. Or in other words: it integrates and operates.

At this point, you probably already figured out when the Integration Operation Segregation Principle is all about: splitting these up. Let me show what the previous class looks like with this principle applied:

"Frederick you devious hack!" you might shout, You've turned a small method into a 4 class method that more than doubles the lines of code.

Well, you're not wrong. It's way more code. And on first glance, it looks way more complex. But let's take a step back and go over it.

Extracting complexity 

if you strip away the dependency injection etc, you're left with

There is nothing complex going on here. Everything perfectly describes what is happening. That $dayPrice? Oh, it's been calculated, how is it being calculated? Who cares. No need to worry about it. Someone with almost no programming knowledge can read this method and fully understand what's happening here.

But what about the logic.

I would argue that the logic in there is even more simple.

In the future, there might be some business logic changes (we might need to do something with tax?). We now know where we can do that, everything is contained in this little class.

This is also a nice illustration of the Open–closed principle.

So yes, we do have a lot more code. But it's all very simple code, easy to read, easy to debug and the coolest of all: Easy to test.

Now that is a nice unit test. No mocks, easy to write, easy to read. (compare this to the unit test at the top of this article).

Conclusion 

As you can see the Integration Operation Segregation Principle is just a long and complicated term to describe something very simple. This all might seem like a lot of work, but it's worth it. Your code and especially your tests will thank you later

If you want to read more about this, do check out this incredible blog post: The Incremental Architect´s Napkin - #7 - Nest flows to scale functional design

Hey thanks for reading!

Hope you enjoyed this post. There is no comments section here, if you've ever seen the YouTube comments section you'll probably understand why. If you have any remarks or comments on this topic, you can find links below to social media discussions

Famous car from time travel movie

The y2k38 bug: the biggest news craze of the year 2038

Posted Sat Jul 01 2017

In 2038 a bug that will apear that might trigger a strong feeling of déjàvu to the Y2k craze. The Y2k38 bug seems to come at a very random moment, but it all makes sense if we apply a bit of computer science to it.

Continue reading
Laptop with code

How we improved our PWA score by 53 points in 4 hours

Posted Sun Mar 12 2017

After a recent burst of inspiration at PHPUK Sam and I ran an experiment to see how much we could improve our company site in just 4 hours. Turns out it was far easier than we expected

Continue reading
Bird's eye view of a landscape

A bird's eye view on API development

Posted Sun Nov 15 2015

So you want to build a web API. You have to start somewhere, why not here

Continue reading