All code can be classified into two distinct roles; code that does work (algorithms) and code that coordinates work (coordinators).
The real complexity that gets introduced into a code bases is usually directly related to the creation of classes that group together both of these roles under one roof.
I’m guilty of it myself. I would say that 90% of the code I have written does not nicely divide my classes into algorithms and coordinators.
Defining things a bit more clearly
Before I dive into why we should be dividing our code into clear algorithmic or coordinating classes, I want to take a moment to better define what I mean by algorithms and coordinators.
Most of us are familiar with common algorithms in Computer Science like a Bubble Sort or a Binary Search, but what we don’t often realize is that all of our code that does something useful contains within it an algorithm.
What I mean by this is that there is a clear distinct set of instructions or steps by which some problem is solved or some work is done. That set of steps does not require external dependencies, it works solely on data, just like a Bubble Sort does not care what it is sorting.
Take a moment to wrap your head around this. I had to double check myself a couple of times to make sure this conclusion was right, because it is so profound.
It is profound because it means that all the code we write is essentially just as testable, as provable and potentially as dependency free as a common sorting algorithm if only we can find the way to express it so.
What is left over in our program (if we extract out the algorithms) is just glue.
Think of it like a computer. Computer electronics have two roles: doing work and binding together the stuff that does the work. If you take out the CPU, the memory and all the other components that actually do some sort of work, you’ll be left with coordinators. Wires and busses that bind together the components in the system.
Why dividing code into algorithms and coordinators is important.
So now that we understand that code could potentially be divided into two broad categories, the next question of course is why? And can we even do it?
Let’s address why first.
The biggest benefit to pulling algorithmic code into separate classes from any coordinating code is that it allows the algorithmic code to be free of dependencies. (Practically all dependencies.)
Once you free this algorithmic code of dependencies you’ll find 3 things immediately happen to that code:
- It becomes easier to unit test
- It becomes more reusable
- Its complexity is reduced
A long time ago before mocks were widely used and IoC containers were rarely used, TDD was hard. It was really hard!
I remember when I was first standing on the street corners proclaiming that all code should be TDD with 100% code coverage. I was thought pretty crazy at the time, because there really weren’t any mocking frameworks and no IoC containers, so if you wanted to write all your code using TDD approaches, you’d actually have to separate out your algorithms. You’d have to write classes that had minimal dependencies if you wanted to be able to truly unit test them.
Then things got easier by getting harder. Many developers started to realize that the reason why TDD was so hard was because in the real world we usually write code that has many dependencies. The problem with dependencies is that we need a way to create fake versions of them. The idea of mocking dependencies became so popular that entire architectures were based on the idea and IoC containers were brought forth.
We, as a development community, essentially swept the crumbs of difficult unit testing under the rug. TDD and unit testing in general became ubiquitous with writing good code, but one of the most important values of TDD was left behind, the forced separation of algorithmic code from coordinating code.
TDD got easier, but only because we found a way to solve the problems of dependencies interfering with our class isolation by making it less painful to mock out and fake the dependencies rather than getting rid of them.
There is a better way!
We can still fix this problem, but we have to make a concerted effort to do so. The current path of least resistance is to just use an IoC container and write unit tests full of mocks that break every time you do all but the most trivial refactoring on a piece of code.
Let me show you a pretty simple example, but one that I think clearly illustrates how code can be refactored to remove dependencies and clearly separate out logic.
Take a look at this simplified calculator class:
This class does simple add calculations and stores the results in a storage service while keeping track of the adding session.
It’s not extremely complicated code, but it is more than just an algorithm. The Calculator class here is requiring a dependency on a storage service.
But this code can be rewritten to extract out the logic into another calculator class that has no dependencies and a coordinator class that really has no logic.
Now you can see that the BasicCalculator class has no external dependencies and thus can be easily unit tested. It is also much easier to tell what it is doing because it contains all of the real logic, while the Calculator class has now become just a coordinator, coordinating calls between the two classes.
This is of course a very basic example, but it was not contrived. What I mean by this is that even though this example is very simple, I didn’t purposely create this code so that I could easily extract out the logic into an algorithm class.
I’ve found that if you focus on eliminating mocks or even just having the mindset that you will not use mocks in your code, you can produce code from the get go that clearly separates algorithm from coordination.
I’m still working on mastering this skill myself, because it is quite difficult to do, but I believe the rewards are very high for those that can do it. In code where I have been able to separate out algorithm from coordination, I have seen much better designs that were more maintainable and easier to understand.
I’ll be talking about and showing some more ways to do this in my talk at the Warm Crocodile conference next year.