Programming Styles as an Architecture Comparison
The same small task solved in dozens of programming styles is the most compact architecture comparison I know – because every style is a deliberately chosen set of constraints.
Programming Styles as an Architecture Comparison
A programming style is not a matter of taste. At least not in the sense that Cristina Videira Lopes gives the word in her book “Exercises in Programming Style” (2014): there, a style is a set of rules that determine which means are allowed and which are forbidden. No global variables. Only functions that leave their input unchanged. No direct calls between components. When you write in a style, you submit to a set of constraints – and it is exactly these constraints that produce the properties of the program, not a personal preference for indentation or naming.
The book takes this idea to the extreme with a device that Lopes borrowed from Raymond Queneau. In “Exercices de style” (1947), Queneau tells the same trivial everyday scene in 99 different ways. Lopes does the same with a program: in the first edition, a single, deliberately mundane task is solved in 33 styles. The task is word frequency counting – term frequency:
- read a text file (in the repo: “Pride and Prejudice”)
- normalize the text, that is, lowercase it and remove punctuation
- filter out stop words such as “the” and “of”
- count the frequency of each word
- print the 25 most frequent words
Each solution is a short Python script, all of them produce the same output, all of them run with the same command. I forked the accompanying repository in early May and worked through it style by style – not reading, but typing, changing, breaking and repairing. In doing so, I noticed something that goes beyond the book: this is not a cabinet of curiosities. It is the most compact architecture comparison I have ever come across. Because the task is trivial and constant, structure remains the only variable. You see architectural decisions in pure form, each on a single screen.
I want to pick out a few of the styles and set them against each other.
Good Old Times: the absence of structure#
The first style in the book simulates the conditions of early computers: barely any memory, no named abstractions, no functions – the program is a single sequence of statements that has to keep intermediate results on a simulated “hard disk”. The result is tedious to read and even more tedious to change. The related monolithic style relaxes the memory limit but still forbids any abstraction: one long procedure, nested loops, flags.
What’s interesting about it is not the nostalgia but the honesty. The monolith is the zero point: this is what code looks like when no constraint shapes it. And it is by no means extinct – in 2014 I took over an organically grown import script in a project that followed exactly this style, only with 1,800 instead of 80 lines. It worked. It was just impossible to test, because there was no place where you could have touched it without touching everything.
Pipeline: data flow instead of a sequence of steps#
The pipeline style allows only functions that take their input and return a result. No shared state, no side effects between the steps. My rebuild looks like this (Python 2.7, as in the book):
import re
import operator
def read_text(path):
with open(path) as f:
return f.read()
def normalize(text):
return re.sub('[^a-z]', ' ', text.lower())
def tokenize(text):
return text.split()
def remove_stop_words(words):
with open('stop_words.txt') as f:
stop_words = set(f.read().split(','))
return [w for w in words if w not in stop_words and len(w) > 1]
def count_frequencies(words):
counts = {}
for w in words:
counts[w] = counts.get(w, 0) + 1
return counts
def top_25(counts):
ordered = sorted(counts.items(), key=operator.itemgetter(1), reverse=True)
return ordered[:25]
for word, count in top_25(count_frequencies(remove_stop_words(
tokenize(normalize(read_text('pride-and-prejudice.txt')))))):
print word, '-', count
The last line reads like a mathematical formula: the program is the composition of its steps. Each function can be tested on its own, each can be swapped out as long as the shape of the data fits. That is the architecture of every batch and ETL pipeline and every Unix pipe – except that there the functions are called processes and the return values are files or streams. The constraint “no shared state” buys testability and parallelizability and pays with the ban on remembering anything along the way.
Things: state behind doors#
The “Things” style is the object-oriented answer: the problem is broken down into things – a DataStorageManager, a StopWordManager, a WordFrequencyManager – and each thing encapsulates its state. From outside, only methods are visible; nobody reaches into another thing’s data. The constraint is: state exists, but only behind a door, and the door has a name.
On 60 lines, this seems almost excessively solemn. But the analogy at the system level is obvious: it is the basic idea of modular systems and clean service boundaries. The question “who owns this state, and who may change it?” is the most productive question of all in every architecture review I have experienced – and this style forces you to answer it for every piece of data.
Hollywood: Don’t call us, we’ll call you#
From here on, the direction of control flips. In the Hollywood style, the program no longer calls its building blocks – a framework calls back whatever registered beforehand:
class WordFrequencyFramework:
def __init__(self):
self._load_handlers = []
self._work_handlers = []
self._end_handlers = []
def register_for_load(self, handler):
self._load_handlers.append(handler)
def register_for_work(self, handler):
self._work_handlers.append(handler)
def register_for_end(self, handler):
self._end_handlers.append(handler)
def run(self, path):
for handler in self._load_handlers:
handler(path)
for handler in self._work_handlers:
handler()
for handler in self._end_handlers:
handler()
The domain parts – reading, filtering, counting – register with the framework and wait. The flow belongs to the framework, no longer to the application. This is Inversion of Control in its barest form, and it is the construction principle of practically every web framework and every plugin architecture: you no longer write a main loop; you fill gaps that someone else has provided.
If you write a lot of JavaScript, you know this feeling from every Node program – there, the callback is not a style exercise but everyday life, with all the consequences for the readability of the control flow. I once wrote down how to tame control flow before async/await. The style exercise makes visible what you trade in the process: flexibility for the framework, traceability for the reader. If you want to know when count_frequencies runs, you have to understand the framework, not the domain logic.
Bulletin Board: the notice board#
The bulletin board style decouples one level further. Components no longer call each other at all, not even through registration with the framework – they publish events to a notice board, and whoever has signed up for an event type gets notified:
class EventManager:
def __init__(self):
self._subscriptions = {}
def subscribe(self, event_type, handler):
self._subscriptions.setdefault(event_type, []).append(handler)
def publish(self, event):
event_type = event[0]
for handler in self._subscriptions.get(event_type, []):
handler(event)
The counter subscribes to word, the filter subscribes to valid_word, the output subscribes to print – and nobody knows anybody. That is publish/subscribe in 15 lines, the pattern behind every message broker and every event-driven integration. The constraint “no direct calls” buys maximum decoupling: you can add participants without changing existing ones. The price is losing the visible flow. While working through the exercises, this style was the first for which I needed the debugger – with 80 lines. You have exactly the same experience later at large scale, when you chase a message through an event-driven system.
The One: the value in a container#
The style with the nicest name is a monad light: a value is put into a container, and the container knows how to apply functions to its content one after another.
class TheOne:
def __init__(self, value):
self._value = value
def bind(self, func):
self._value = func(self._value)
return self
TheOne('pride-and-prejudice.txt') \
.bind(read_text) \
.bind(normalize) \
.bind(tokenize) \
.bind(remove_stop_words) \
.bind(count_frequencies) \
.bind(top_25) \
.bind(print_all)
In terms of content, this is the same chain as in the pipeline style – but the chaining itself is now a building block you can extend: with logging at every step, with error handling, with deferred execution. If you have worked with promise libraries in ES5 JavaScript, you recognize the pattern immediately: then is a bind that also encapsulates asynchrony along the way. The constraint “everything flows through a container” buys a place where cross-cutting concerns are allowed to live.
Six styles, six architectures#
Laid side by side, the exercises yield a mapping that I have kept in my head ever since:
flowchart LR
A[Monolith<br/>no abstraction allowed] --> A2[grown scripts,<br/>legacy procedures]
B[Pipeline<br/>only pure functions] --> B2[batch, ETL,<br/>Unix pipes]
C[Things<br/>state only behind methods] --> C2[modules,<br/>service boundaries]
D[Hollywood<br/>framework calls back] --> D2[frameworks,<br/>plugin systems]
E[Bulletin Board<br/>only events, no calls] --> E2[message brokers,<br/>integration]
F[The One<br/>everything through one container] --> F2[fluent APIs,<br/>promise chains]Each of these architectures at large scale is a programming style at small scale – the same set of constraints, only at the process level instead of the function level. And at both scales, the properties you want come from what you forbid yourself. Testability comes from forbidding shared state. Extensibility comes from forbidding direct calls. Traceability comes from forbidding hidden control flows. None of this comes for free; every constraint has its price, and the exercises make you feel the price on a single screen instead of presenting it only after two years in production.
The unconscious mix of styles#
The most important insight for me, though, didn’t come from any of the styles but from the contrast with my own everyday code. Like most code out there, presumably, it is a mix of styles: a bit object-oriented, because the language suggests it; a few pure functions where it was convenient; a global cache, because it had to be quick; callbacks, because the framework demands them. None of this is wrong in itself. The problem is that the mix arises unconsciously. Nobody decided that this module follows the pipeline idea and that one the notice board – it just happened, commit by commit.
For me, this is exactly the difference between code that has structure and code that only looks structured. Architecture is the art of choosing constraints deliberately – and then sticking to them even where they are inconvenient at the moment. A constraint you give up as soon as it pinches never was one; it was a habit. Since working through the exercises, I occasionally ask a new question in code reviews: which style are we actually in here? Not as a trick question, but because the answer immediately shows whether a rule the team set for itself was broken – or whether the rule never existed and we should make up for it now.
The view also helps in the opposite direction. When a module is hard to test, the question is not “which test tool is missing?” but “which constraint is missing?”. Usually it is the same one that the pipeline style enforces and organically grown code ignores.
What I take away from this#
Counting word frequencies is the most boring task in the world, and that is exactly what makes it the ideal test bench: if the task stays constant, everything that differs between two solutions is structure – and therefore comparable. You can discuss architectural styles by dissecting systems with years of history. Or you compare 33 scripts of 80 lines each that all do the same thing, and see the same forces at work, only without the fog.
There are a few concrete things I take away:
- Judge styles not by sympathy but by the question of which property their constraints buy and what they cost.
- When reading someone else’s code, identify the style first – afterwards, much of what previously looked arbitrary explains itself.
- Occasionally make your own mix of styles explicit: which rules apply in this module, and are they written down anywhere?
If you don’t have the book at hand: the repository alone already gets you far. Fork it, rebuild one style per evening, deliberately violate the constraints along the way and watch what breaks. I have learned more about architecture from these evenings than from many a thick design patterns tome – precisely because not a single UML diagram appeared, only the question: what do you forbid yourself, and what do you get in return?
Further reading#
- My fork with the exercises I worked through: https://github.com/MikeBild/exercises-in-programming-style
- The original repository by Cristina Videira Lopes: https://github.com/crista/exercises-in-programming-style
- Raymond Queneau’s literary model “Exercises in Style”: https://en.wikipedia.org/wiki/Exercises_in_Style
- Python 2 documentation on
collections(includingCounterfor frequency counting): https://docs.python.org/2/library/collections.html
Revised and translated from German with the help of AI.