Modularity
Modularity is defined as “the degree to which a system’s components may be separated and recombined, often with the benefit of flexibility and variety in use.” 1
I have been writing code for a long time, and from the beginning of my learning, even when writing simple video games in assembly language, modularity was lauded as important in the design of our code.
Yet, much of the code that I have seen—in fact most of the code that I have seen and maybe even some of the code that I have written—has been far from modular. At some point this changed for me. My code is always modular now; it has become an ingrained part of my style.
Modularity is of vital importance in managing the complexity of the systems that we create. Modern software systems are vast, complicated, and often genuinely complex things. Most modern systems exceed the capacity of any human being to hold all of the details in their heads.
To cope with this complexity, we must divide the systems that we build into smaller, more understandable pieces—pieces that we can focus on without worrying too much about what is going on elsewhere in the system.
This is always true, and again this is a kind of fractal idea that operates at different granularities.
As an industry, we have made progress. When I began my career, computers, and their software, were simpler, but we had to work harder to get things done. Operating systems did little beyond providing access to files and allowing us to show text on the screen. Anything else we needed to do had to be written from scratch for every program. Want to print something out? You need to understand and write the low-level interactions with your specific printer.
- Source: Dictionary definition from Merriam-Webster. Used with Permission. Wikipedia, https://en.wikipedia.org/wiki/Modularity .
We have certainly moved on by improving the abstraction and modularity of our operating systems and other software.
Yet, lots of systems don’t look modular themselves. That is because it is hard work to design modular systems. If software development is about learning, then, as we learn, our understanding evolves and changes. So it is possible, probable even, that our views on which modules make sense and which don’t will change over time, too.
For me, this is the real skill of software development. This is the property that most differentiates the code written by experts, masters of their craft, from that written by neophytes. While it takes skill to achieve good modularity in our designs, what I perceive in a lot of the code that I see is that people don’t just “do modularity badly,” but, rather, they “don’t attempt it at all.” A lot of code is written as though it is a recipe, meaning a linear sequence of steps collected together in methods and functions spanning hundreds or even thousands of lines of code.
Imagine for a moment if we turned on a feature, in your codebase, that rejected any code that contained a method longer than 30 lines of code or 50 or 100? Would your code pass such a test? I know that most of the code that I have seen in the wild would not.
These days when I begin a software project, I will establish a check in the continuous delivery deployment pipeline, in the “commit stage,” that does exactly this kind of test and rejects any commit that contains a method longer that 20 or 30 lines of code. I also reject method signatures with more than five or six parameters. These are arbitrary values, based on my experience and preferences with the teams that I have worked on. My point is not to recommend these specific values; rather, it is that “guiderails” like these are important to keep us honest in our design. Whatever the time pressure, writing bad code is never a time-saver!
Hallmarks of Modularity
How can you tell if your system is modular? There is a simplistic level in the sense that a module is a collection of instructions and data that can be included in a program. That captures the “physical” representation of the bits and bytes that form the module.
More practically, though, what we are looking for is something that divides our code into little compartments. Each compartment can be reused multiple times, perhaps in a variety of contexts.
The code in a module is short enough to be readily understood as a stand-alone thing, outside the context of other parts of the system, even if it needs other parts of the system to be in place to do useful work.
There is some control over the scope of variables and functions that limit access to them so that there is a notion in some sense of there being an “outside” and an “inside” to the module. There is an interface of some kind that controls access, manages communication with other code, and deals with other modules.
Undervaluing the Importance of Good Design
There are a few reasons why many software developers don’t pay attention to ideas like these. As an industry, we have undervalued the importance of software design. We obsess over languages and frameworks. We have arguments over IDEs versus text editors or object-oriented programming versus functional programming. Yet none of these things comes close to being as important, as foundational, as ideas like modularity or separation of concerns to the quality of our output.
If you have code with good modularity and good separation of concerns, whatever the programming paradigm, language, or tools, it will be better, easier to work on, easier to test, and easier to modify as you learn more about the problem that you are trying to solve. It will also be more flexible in use than code that does not have these properties.
My impression is that either we don’t teach these skills at all or there is something inherent in programming (or programmers) that makes us dismiss their importance.
Clearly, designing for modularity is a different kind of skill to knowing the syntax of a programming language. It is a skill that we need to work on if we hope to develop some degree of mastery, and we can spend a lifetime and probably never perfect it.
This, though, is to me what software development is really about. How can we create code and systems that will grow and evolve over time but that are appropriately compartmentalized to limit damage if we make a mistake? How do we create systems that are appropriately abstracted so that we can treat the boundaries between our modules as opportunities to enhance our systems rather than liabilities that prevent us from changing them?
This is an important point in the thesis of this book.
I once taught a class on test-driven development (TDD). I was attempting to demonstrate how TDD could help us reduce the complexity in our designs when one of the course attendees (I won’t call them a programmer ) asked why it mattered if the code was less complex. I confess that I was shocked. If this person didn’t see the differences in impact and value between obtuse, complex code and clear, simple code, then they have a different view of our job than I do. I did my best to answer his question, talking about the importance of maintainability and the advantages in terms of efficiency, but I am not convinced that my arguments made much impression.
Fundamentally, complexity increases the cost of ownership for software. This has a direct economic impact as well as a more subjective one: complex code isn’t as nice to work on!
The real issue here, though, is that complex code is, kind of by definition, more difficult to change. That means you have one chance to get it right when you write it the first time. Also, if my code is complex, then I probably don’t really understand it as well as I think that I do; there are more places for mistakes to hide.
If we work to limit the complexity of the code that we write, we can make mistakes and have a better chance of correcting them. So either we can bet on our own genius and assume that we will get everything perfectly correct at the start, or we can proceed more cautiously. We start by assuming that there will be things that we didn’t think of, misunderstandings, and changes in the world that mean that we will likely need to revisit our code one day. Complexity costs!
It is important that we are open to new ideas. It is important that we continually question our assumptions. However, that doesn’t mean that all ideas have equal merit. There are dumb ideas, and they should be dismissed; there are great ideas, and they should be valued.
Knowing the syntax of a language is not enough to be a “programmer,” let alone to be a good programmer. Being “idiomatic in language X” is less valuable and less important than high—quality in design. Knowing the abstruse details of “API Y” does not make you a better software developer; you can always look up the answer to that kind of question!
The real skills—the things that really differentiate great programmers from poor programmers—are not language-specific or framework-specific. They lie elsewhere.
Any programming language is only a tool. I have been privileged to work with a few world-class programmers. These people will write good code in a programming language that they have never used before. They will write nice code in HTML and CSS or Unix shell scripts or YAML. One of my friends even writes readable Perl!
There are ideas that are deeper and more profound than the language used to express them. Modularity is one of these ideas; if your code is not modular, it is almost certainly not as good as code that is!
The Importance of Testability
I was an early adopter of TDD, taking my first tentative steps in that direction in response to Kent Beck’s book Extreme Programming Explained published in 1999. My team experimented with Kent’s intriguing ideas and got them wrong that same year. Nevertheless, we got great benefit from the approach.
TDD is one of the most significant steps that software development practice has taken during my career. The confusing thing is that the reason I value it so highly has little to do with “testing” as we would usually conceive it. In fact, I now think that Kent Beck made a mistake including “test” in the name of the practice, at least from a marketing perspective. And no, I don’t know what he should have called it instead!
Chapter 5 described how we can get fast, accurate feedback on the quality of our designs from our tests and how making our code testable enhances its quality. This is an enormously important idea.
There are a few things, beyond the good taste of an experienced, skilled programmer, that can give us early feedback on the quality of our design. We may learn weeks, months, or years later that our design is good or bad when we attempt to change it, but short of that, there are no objective measures that indicate quality, unless, that is, we attempt to drive our design from tests.
If our tests are difficult to write, it means that our design is poor. We get a signal, immediately. We get feedback on the quality of our design as we attempt to refine it for the next increment in behavior. These lessons are delivered automatically to us if we follow the Red, Green, Refactor discipline of TDD. When our tests are hard to write, our design is worse than it should be. If our tests are easy to write our code, the stuff that we are testing inevitably exhibits the properties that we value as hallmarks of high quality in code.
Now, this does not imply that a test-driven approach to design will automatically create great design. It is not a magic wand. It still relies upon the skill and experience of the designer. A great software developer will still create a better outcome than a poor one. What driving our designs from tests does is encourage us to create testable code and systems and so, given the limits of our experience and talent, enhances the outcome.
We don’t have any other techniques I can think of that really do that to a similar extent! This talent amplifier then is an important tool if we are to move from craft to engineering.
If we aspire to be engineers, advising people to “do better” is not enough. We need tools that will guide us and help us achieve a better outcome. Striving for testability in our systems is one such tool.
Designing for Testability Improves Modularity
Let’s get back on topic and think about this specifically in the context of modularity. How does designing to achieve testability encourage greater modularity?
If I want to test the effectiveness of the airfoil of a wing on an airplane, I can build the airplane and go flying. This is a terrible idea that even the Wright Brothers, who built the first powered, controlled airplane, realized wouldn’t work.
If you take this rather naive approach, then you have to do all of the work first before you learn anything. When you try to learn this way, how will you measure the effectiveness of this airfoil versus another? Build another airplane?
Even then, how do you compare the results? Maybe the wind was gustier when you flew the first prototype versus the second. Maybe your pilot had a bigger breakfast on the first flight than the second. Perhaps the air pressure or the temperature varied, so the wings delivered different amounts of lift because of that. Maybe the fuel batch was different between the two, so the engine was producing different power levels. How can you manage all these variables?
If you take this whole-system, waterfall approach to solving this problem, the complexity of the system is now expanded to encompass the entire environment in which the airfoil operates.
The way to scientifically measure an airfoil is to take control of these variables and standardize them across your experiments. How can we reduce the complexity so that the signals that we get back from our experiment are clear? Well, we could put the two airplanes into a more controlled environment, maybe something like a big wind tunnel. That would allow more precise control of the airflow over the wings and the wind. Maybe we could do this in a temperature and pressure-controlled environment. Only with this sort of control can we expect to get to more repeatable results.
If we are going to start down this road, we don’t really need an engine or flight controls or the rest of the airplane for that matter. Why not just make two models of the wings with the airfoils that we would like to test and try those in our temperature- and pressure-controlled wind tunnel?
That would certainly be a more accurate experiment than just going flying, but this still requires us to build the whole wing twice. Why not make a small model of each airfoil? Make each model as precisely as possible, using exactly the same materials and techniques, and compare the two. If we are going to go that far, we could do this on a smaller scale, and we’d need a simpler wind tunnel.
These small pieces of airplane are modules. They are parts that certainly add to the behavior of the whole plane, but they are focused on a specific part of the problem. It is true that such experiments will give you only a partially true picture. The aerodynamics of airplanes are more complex than only the wings, but modularity means that we can measure things that we couldn’t measure without it, so the part, the module, is certainly more testable than the whole.
In the real world, this is how you conduct experiments to determine how the shape of wings, and other things, affect lift.
Modularity gives us greater control and greater precision in the things that we can measure. Let’s move this example into the software world. Imagine that you are working on System B, which is downstream from System A and upstream from System C (see Figure 9.1 ).
Figure 9.1
Coupled systems
This is typical of working on big systems in complex organizations. This presents a problem: how do we test our work? Many, maybe even most, organizations faced with this problem jump to the assumption that it is essential to test everything together to be sure that the system is safe to use.
There are many problems with this approach. First, if we measure only at this scale, we face the “test the whole airplane” problem. The whole system is so complex that we lack precision, reproducibility, control, and clear visibility of what any results that we do collect really mean.
We can’t evaluate our part of the system with any degree of precision, because the upstream and downstream parts, System A and System C, get in our way. There are many types of tests that are simply impossible as a result of this decision. What happens to System B if System A sends it malformed messages?
That case is impossible to measure, while the real System A is in place sending well-formed messages. How should System B respond when the communications channel to System C is broken?
Again, we can’t test that scenario while a real System C, with working comms, is in place, getting in the way of us faking a comms error.
The results that we do collect don’t tell us much. If a test fails, is that because there is a problem with our system, or is it one of the others? Maybe the failure means that we have the wrong versions of the upstream or downstream systems. If everything works, is that because we are ready to release? Or is it because the cases we are trying to evaluate are so simplistic, due to this mega-system not being testable that they aren’t really finding the bugs that are really there?
If we are measuring the whole, composite system (see Figure 9.2 ), then our results are vague and confusing. The rather cartoony diagram in Figure 9.2 illustrates an important problem: we need to be clear what it is that we are measuring, and we need to be clear of the value of our measurements. If we are doing the kind of end-to-end tests in this diagram, then what is the objective of our test? What do we hope to demonstrate? If our aim is to demonstrate that all of the pieces work together, well, that may be useful in some contexts, but this style of testing is insufficient to tell us if System B, the one that we are truly responsible for in this scenario, really works. These sorts of tests make sense only as a supplement, a small supplement, to a better, more thorough, more modular testing strategy. They certainly do not replace that more detailed testing is necessary to demonstrate the correct working of our system, System B!
Figure 9.2
Testing coupled systems
So what would it take to achieve that more detailed testing? Well, we would need a point of measurement , which is a place in our whole system where we can insert probes of some kind that will allow us to reliably gather our measurements. I have represented these “points of measurement” in this series of diagrams as fictional calipers. In reality, we are talking about being able to inject test data into our system under test (SUT), invoke behaviors within it, and collect the outputs so that we can interpret the results. I know that the calipers are a bit cheesy, but that is the mental model I have when I think about testing a system. I am going to insert my system into a test rig of some kind so that I can evaluate it. I need a measuring device (my test cases and test infrastructure) that allows me to attach probes to my SUT so that I can see how it behaves.
My calipers in Figure 9.2 are not very helpful for the reasons that we have already explored, but also because the bigger and more complex the system, the more variable the results. We have not controlled the variables enough to get a clear, repeatable result.
If you have a suite of even automated tests that you run to evaluate your software to determine if it is ready to release and those tests don’t produce the same result every time, what do those results really mean?
If our aim is to apply some engineering thinking, we need to take our measurements seriously. We need to be able to depend upon them, which means that we need them to be deterministic. For any given test or evaluation, I should expect, given the same version of the software under test, to see the same results every time I run it, however many times I run it, and whatever else is going on when I run it.
There is enough value in that statement that it is worth doing extra work, if necessary, to achieve that repeatable outcome. It will have an impact on not only the tests that we write and how we write them but also, importantly, the design of our software, and that is where the real value of this engineering approach begins to show.
When we built our financial exchange, the system was completely deterministic to the extent that we could record the production inputs and replay them some time later to get the system into precisely the same state in a test environment. We did not set out with that goal. That was a side effect of the degree of testability, and so determinism, that we achieved.
Complexity and Determinism
As the complexity of the system under test grows, the precision with which we can measure things reduces. For example, if I have a piece of software that is performance critical, I can isolate it and put it into a test rig of some kind that allows me to create a series of controlled test runs. I can do things like discard the early runs to eliminate any of the effects of runtime optimization, and I can run through enough executions to apply statistical techniques to the data that I collect. If I take all of these things seriously enough, my measurements can be accurate and reproducible down to certainly microseconds and sometimes even nanoseconds.
Doing the same things in a whole-system performance test, for a system of any significant size, is practically impossible. If I measure performance for a whole system, the variables will have exploded. What other tasks are underway at the same time on the computers running my code? What about the network? Is that being used for anything else while my measurements are underway?
Even if I control for these things, locking down the network and access to my performance test environment, modern operating systems are complex things. What if the OS decides to do some housekeeping while my test is running? That will surely skew the results, won’t it?
Determinism is more difficult to achieve as the complexity and scope of the system grow.
The real root cause of a lack of determinism in computer systems is concurrency. This can take various forms. The clock ticking away incrementing the system time is one form of concurrency; the OS re-organizing your disk when it thinks it has some spare time is another. In the absence of concurrency, though, digital systems are deterministic. For the same sequence of bytes and instructions, we get the same result every time.
One useful driver of modularity is to isolate the concurrency so that each module is deterministic and reliably testable. Architect systems so that entry into a module is sequenced and its outcomes are more predictable. Systems written this way are very nice to work on.
This may seem a fairly esoteric point, but in a system in which every behavior that its users observe is deterministic, in the way that I have described, it would be eminently predictable and testable with no unexpected side effects, at least to the limits of our testing.
Most systems are not built like this, but if we take an engineering-led approach to their design, they can be.
If, instead, we could apply our calipers to measure only our component (see Figure 9.3 ), we could measure with much greater accuracy and precision and with much more reliability. Stretching my analogy to breaking point, we could measure other dimensions of the problem, too.
Figure 9.3
Testing modules
So, what would it take to measure with this increased precision and specificity? Well, we would like our measuring points to be stable so that we get the same result from our measurement every time, all other things being equal. We would like our evaluations to be deterministic.
We would also like to not have to re-create our measurement points from scratch each time that the system changes.
To be clear, what I am describing here is a stable, modular interface to the part of the system that we would like to test. We compose larger systems of smaller modules, modules with clearly defined interfaces, for inputs and outputs. This architectural approach allows us to measure the system at those interfaces.
Reading this here, I hope that this seems obvious. The problem is that few real-world computer systems look like this.
If we make it a fundamental part of our job to test, with automated testing, the systems that we create, then we are very strongly encouraged by the extra work that we are forced to do if we get it wrong to create more modular systems. This is fractal. This is true at all levels of granularity, from entire enterprise systems down to individual methods, functions, and classes.
It is simply not possible to test a system, in the way that this book assumes, that is not, in some way, modular. We need those “points of measurement.” Our ability to test is supported and enhanced by modularity, and modularity is encouraged by being guided in our designs by tests.
This does not necessarily imply a collection of tiny, independent components. This can equally work for large complex systems. The key here is to understand the scope of measurement that makes sense and work to make those measurements easy to achieve and stable in terms of the results that they generate.
When I was involved in creating a financial exchange, we treated the whole enterprise system as a single system, but we established clear, well-defined integration points for every external interaction and faked those external systems. Now we had control; now we could inject new account registrations and collect data that, in real operation, would have been sent to banks or clearinghouses and so on.
This allowed us, for some of our testing, to treat the whole system as a black box, inject data to get the system into an appropriate state for a test, and collect its outputs to evaluate the system’s response. We treated every point at which our system interacted with a third-party system and every integration point as a point of measurement where we could plug in to our test infrastructure. This was possible only because our entire enterprise system was designed with testability in mind from day one.
Our system was also extremely modular and loosely coupled. So, as well as evaluating the system as a whole, we could do more detailed testing for single, service-level components, too. Inevitably all of the behavior within a service was also developed using fine-grained TDD techniques for pretty much every line of code. We could also test tiny pieces of the behavior of the system in isolation from everything else. As I said, modularity and testability are fractal.
Testability deeply informed the architectural choices that we made and, over time, had a profound impact not just on the obvious measures of quality, like how many, or how few bugs we could find, but more subtly, and perhaps more importantly, on the architectural compartmentalization of our system.
Historically, we have, as an industry, undervalued or even missed the importance of testability and specifically test-first development as a tool to drive good design and to give us early, clear feedback on the quality of our designs.
Services and Modularity
The idea of a service in software terms is rather slippery. There are no mainstream languages, for example, that directly support the idea of services, yet still the idea is pretty pervasive. Software developers will argue about what makes a good service or a bad one and architect their systems to support the concept.
From a purely practical perspective, we can think of a service as code that delivers some “service” to other code and hides the detail of how it delivers that “service.” This is just the idea of “information hiding” and is extremely important if we want to manage the complexity of our systems as they grow (see Chapter 12 ). Identifying “seams” in the design of our systems where the rest of the system doesn’t need to know, and shouldn’t care about, the detail of what is happening on the other side of those “seams” is a very good idea. This is really the essence of design.
Services, then, provide us with an organizing idea of little compartments in our systems that hide detail. This is a useful idea. So a service can certainly, sensibly, be thought of as a module of our system. If that is the case, what about these “seams,” these points where a service or module touches something outside of its boundary? The thing that provides any meaning at all to the concept of a “service” in software terms is that it represents a boundary. There is a difference between what is known and what is exposed, on either side of these boundaries.
One of the commonest problems that I see in larger codebases is a result of ignoring this difference. Often the code that represents these boundaries is indistinguishable from the code on either side. We use the same kinds of method calls, and we even pass the same data structures across such boundaries. There is no validation of inputs at these points or assembly and abstraction of outputs. Such codebases quickly become tangled messes that are difficult to change.
There has been an advance in this respect, but it is a small step, and to some degree, we took that step by accident. That is the move to REST APIs.
My background is, in part, in high-performance computing, so the idea of using text, XML, or HTML as a way of encoding the information that flows between services leaves me a bit cold; it’s way too slow ! However, it does rather strongly encourage the idea of having a translation point at the edges of your service or API. You translate the incoming message into some more tractable form for consumption by your service, and you translate the outputs of your service into some horrible, big, slow, text-based message for output. (Sorry, I let my biases creep in there.)
Software developers still get this wrong, though. Even in systems built along these lines, I still see code that passes the HTML straight through, and the whole service interacts with that HTML—yuck!
The seams or boundaries should be treated with more care. They should be translation and validation points for information. The entry point to a service should be a little defensive barrier that limits the worst abuses of consumers of that service. What I am describing here is a Ports & Adapters kind of model at the level of an individual service. This approach should be just as true for a service that communicates via standard method or function calls as one that uses HTML, XML, or any other form of messaging.
The base idea here is one of modularity! A system is not modular if the internal workings of adjacent modules are exposed. Communication between modules (and services) should be a little more guarded than communication within them.
Deployability and Modularity
In my book Continuous Delivery , Jez Humble and I described a way to organize our work so that our software was always releasable. We advised (and continue to advise) that you work so that your software is always in a releasable state. Part of achieving this repeatable, reliable ability to release software is ensuring that it is easily and simply deployable.
Since writing the Continuous Delivery book, I now believe, even more profoundly, that working so that our software is both testable and deployable has a deep impact on the quality of our work.
One of the core ideas in my earlier book is the idea of the deployment pipeline , a mechanism that takes commits in at one end and produces a “releasable outcome” at the other. This is a key idea. A deployment pipeline is not simply a little workflow of build or test steps; it is a mechanized route from commit to production.
This interpretation has some implications. This means that everything that constitutes “releasability” is within the scope of your deployment pipeline. If the pipeline says everything is good, there should be no more work to do to make you comfortable to release—nothing…no more integration checks, sign-offs, or staging tests. If the pipeline says it is “good,” then it is “good to go!”
This, in turn, has some implications for the sensible scope of a deployment pipeline. If its output is “releasable,” it must also be “independently deployable.” The scope of an effective deployment pipeline is always an “independently deployable unit of software.”
Now this has an impact on modularity. If the output of the deployment pipeline is deployable, it means that the pipeline constitutes a definitive evaluation of our software. It’s definitive at least to the degree that we care, and consider safe and sensible, to establish its readiness for release.
There are only two strategies that make sense if we are to take that idea to its logical conclusion. We can build, test, and deploy everything that constitutes our system together, or we can build, test, and deploy parts of that system separately. There is no halfway solution. If we don’t trust the output of our deployment pipeline sufficiently and feel it necessary to test the results it generates with the output of other deployment pipelines, then that presents problems; the messages that our deployment pipeline is sending to us are now unclear, and since we are trying to be engineers, that isn’t good enough!
Now the scope of our evaluation is compromised. When are we done? Are we finished when our pipeline completes or when every other pipeline that is needed to verify the output for our pipeline has been run? If the latter, then the “cycle time” 2 for our changes includes the cycle times of everyone else’s changes too, so we have a monolithic evaluation system.
The most scalable approach to software development is to distribute it. Reduce the coupling and dependencies between teams and their products to the degree that each team can, independently, create, test, and deploy their work with no reference to another team. This is the approach that has allowed Amazon, with its famous “two-pizza teams,” 3 to grow at an unprecedented rate.
Technically, one way to accomplish this independence is to take the modularity of the system so seriously that each module is, in terms of build, test, and deployment, independent from every other module. This is what microservices are. They are so modular that we don’t need to test them with other services prior to release. If you are testing your microservices together, they aren’t really microservices. Part of the definition of a microservice is that they are “independently deployable.”
Deployability can up the stakes on modularity. As we have seen, deployability defines the effective scope of a deployment pipeline. Our choices of what really works, if we value high-quality work based on fast, efficient feedback, are really quite limited.
We can make the choice to build, test, and deploy everything that constitutes our system together and eliminate dependency-management problems altogether (everything lives in a single repository), but then we must take on the responsibility to create fast enough feedback to allow developers to do a good job, which may take a big investment in engineering to get the feedback that drives any high-quality process quickly enough.
Alternatively, we can work so that each module is, essentially, independent of every other module. We can build, test, and deploy each of these things separately, without the need to test them together.
This means that the scope of our builds, tests, and deployment is small. Each of them is simpler, so it is easier to achieve fast, high-quality results.
However, this comes at the sometimes very significant cost of a more complex, more distributed architecture in our systems. We are forced, now, to take modularity very seriously indeed.
We must design for it; we must be skilled at the techniques of protocol design so that the interactions between modules, the protocol of information exchange between them, is stable and is not allowed to change in a way that forces change on other modules. We probably need to consider and apply ideas like runtime version management for APIs and so on.
Nearly everyone would like some ideal middle ground between these two extremes, but in reality, it doesn’t exist. The middle ground is a fudge and is often slower and more complex than the monolithic approach that everyone strives, so assiduously, to avoid. The more organizationally distributed approach that is microservices is the best way that we know to scale up software development, but it is not a simple approach and comes at a cost.
-
Cycle time is a measure of the efficiency of your development process. How long does it take to go “from idea to useful software in the hands of users”? In continuous delivery, we use the optimization of cycle time as a tool to direct us toward more efficient approaches to development.
-
Amazon famously reorganized following a memo from CEO Jeff Bezos. In his memo Bezos stated that “...no team should be bigger than can be fed by two pizzas.”
Modularity at Different Scales
Modularity is important at every scale. Deployability is a useful tool when thinking of system-level modules, but this, alone, is not enough to create high-quality code. There is a modern pre-occupation with services.
This is a useful architectural tool and one that has been at the heart of my approach to system design for at least the last three decades. However, if the modularity of your design stops there, you may still have poor, hard-to-work-with systems.
If, as I argue, the importance of modularity is a tool to help us manage complexity, then we need to take that to the point of readable code. Each class, method, or function should be simple and readable and, where appropriate, composed of smaller, independently understandable submodules.
Again, TDD helps to encourage such fine-grained code. For the code to be testable at this resolution, techniques like dependency injection encourage code with a bigger surface area. This heavily influences the modularity of our designs.
At smaller scales, dependency injection is the most effective tool to provide pressure on our code that encourages us to create systems composed of many small pieces. The dependencies are the calipers, the points of measurement, that we can inject into our system to achieve a more thoroughly testable outcome. Again, ensuring that our code is testable encourages designs that are genuinely modular and as a result code that is easier to read.
Some people criticize this style of design. This criticism generally takes the form that it is harder to understand code that has a bigger surface area in this way. It is harder to follow the flow of control through the system. This criticism misses the point. The problem here is that if it is necessary to expose that surface area in order to test that code, then that is the surface area of the code. How much harder is it to understand if it is obscured by poor interface design and a lack of tests? The root of this criticism is really about what constitutes “good design.” I propose that focusing our design on the management of complexity is a valuable benchmark to define what we mean by “high quality” in code.
Testing, when done well, exposes something important and true about the nature of our code, the nature of our designs, and the nature of the problem that we are solving that is not otherwise easily accessible. As a result, it is one of the most important tools in our arsenal to create better, more modular systems and code.
Modularity in Human Systems
I discuss the impact of this engineering thinking in detail in Chapter 15 , but it’s useful to call out the particular importance of modularity in this respect. Much of my professional career has been spent working on large computer systems. In this kind of world, the constant refrain has been “how do we scale?” Sometimes, rarely, that is about the software, but mostly when people in big organizations ask that question, what they really mean is “how can we add more people so that we can produce software faster?”
The real answer is that for any given computer system there are very serious limits to that. As Fred Brooks famously said:
You can’t make a baby in a month with 9 women. 4
However, there are other options; you can make nine babies in nine months with nine women, which averages out at a baby per month. At this point my, or Fred’s, analogy breaks down!
In software terms, if not with babies, the problem is about coupling. As long as the pieces are truly independent of one another, truly decoupled, then we can parallelize all we want. As soon as there is coupling, there are constraints on the degree to which we can parallelize. The cost of integration is the killer!
How do we integrate the work from separate streams of development? If you are as nerdy as I am, this may be ringing bells; this is about something deeply fundamental. This is about information and concurrency. When you have independent streams of information, the cost of bringing them together to form a coherent whole can be extremely high if there is any overlap. The best way to parallelize things is to do it in a way where there is no need to re-integrate (nine babies). Essentially, this is the microservice approach. Microservices are an organizational scalability play; they don’t really have any other advantage, but let’s be clear, this is a big advantage if scalability is your problem!
We know that just adding more people to a team does not make that team go faster. In a lovely metadata study of more than 4,000 software projects that compared the relative performance (time to create 100,000 lines of code) in teams of 5 or fewer and teams of 20 or more, the teams of 5 took only one week longer than the teams of 20 people over a 9-month period. So small teams are nearly 4 times as productive, per person, as larger teams. 5
If we need small teams to efficiently create good, high-quality work, then we need to find ways to seriously limit the coupling between those small teams. This is at least as much an organizational strategy problem as it is a technical one. We need modular organizations as well as modular software .
So if we want our organizations to be able to scale up, the secret is to build teams and systems that need to coordinate to the minimum possible degree, we need to decouple them. Working hard to maintain this organizational modularity is important and one of the real hallmarks of genuinely high-performing, scalable organizations.
-
A quote from Fred Brooks’ influential, and still true, book from the 1970s, The Mythical Man Month
-
In a study Quantitative Software Management (QSM) also found that the larger teams produced 5x more defects in their code. See https://bit.ly/3lI93oe .
Summary
Modularity is a cornerstone of our ability to make progress when we don’t have an omniscient view of how our software should work in the future. In the absence of modularity, we can certainly create naive software that will solve a problem in front of us now. Without working in ways that provide some layers of insulation between the parts of software, though, our ability to continue to add new ideas and to grow our software will rapidly diminish to the point where, in some real-world cases, there is no forward progress at all. This is the first in our collection of tools needed to defend against complexity.
Modularity as a design idea is fractal. It is more than only the “modules,” whatever their form, supported in our programming languages. It is more complex and more useful than that. It is, at its heart, the idea that we must retain our ability to change code and systems in one place, without worrying about the impact of those changes elsewhere.
This starts us thinking about other aspects of this problem, so modularity is intimately connected to the other ideas that we need to consider in managing the complexity of our systems—ideas like abstraction, separation of concerns, coupling, and cohesion.