WEBVTT

00:00.000 --> 00:19.180
Hello and welcome to episode 68 of the Structure and Interpretation of Computer Programmers

00:19.180 --> 00:24.760
podcast. I'm Graham Lee. The episode is sponsored by you, the software engineering community.

00:25.420 --> 00:28.460
If you enjoy this podcast, please share the link with your friends and colleagues.

00:28.460 --> 00:34.200
You can give feedback on this episode by commenting on the post at sicpers.info slash podcast,

00:34.700 --> 00:42.440
s-i-c-p-e-r-s dot info slash podcast, or by sending me an email, grahamlee at acm.org.

00:43.320 --> 00:48.880
Back in the days when you could safely assume that many computerists hung out on twitter.com,

00:49.460 --> 00:54.640
Gary Bernhardt made an observation that the history of computing looks better if you run it in reverse.

00:54.640 --> 00:59.540
A lot of kids are thrashing about in JavaScript without really knowing what they're doing in the

00:59.540 --> 01:04.560
beginning. Then gradually a load of sagacious academics show up and uncover the logic and

01:04.560 --> 01:12.020
principles behind their activities. Modularity is one of those topics that supports this wibbly-wobbly,

01:12.180 --> 01:19.380
timey-wimey interpretation of software engineering, sort of. Taking it in reverse order, people now use

01:19.380 --> 01:24.800
data flow as their guide for creating software modules. This gets fixed in the early 2000s by

01:24.800 --> 01:30.980
Bob Martin's The Single Responsibility Principle, then fixed once and for all in 1972 by the publication

01:30.980 --> 01:37.160
of David Parnas's paper, the subject of this episode. Then people go back to using data flow as their guide

01:37.160 --> 01:44.100
until finally electronic computers are uninvented during World War II. Of course, this wouldn't be an

01:44.100 --> 01:48.540
episode of the Structure and Interpretation of Computer Programmers podcast without an unhealthy

01:48.540 --> 01:55.380
level of digression. Bob Martin just popped up as an NPC, so I'm accepting his side quest to dig out

01:55.380 --> 02:01.940
some juice on the SOLID principles. You've probably heard of five of the SOLID principles, the ones that

02:01.940 --> 02:07.520
make up the SOLID acronym. The rest of this episode deals with the Single Responsibility Principle.

02:07.520 --> 02:12.920
The Open Closed Principle comes from Bertrand Meyer, and really deserves an episode in its own right,

02:13.260 --> 02:17.340
but for the moment, make do with the link in the show notes to my 2013 blog post,

02:17.800 --> 02:24.260
which I'm not entirely sure I agree with anymore. The Liskov Substitution Principle is, as the name

02:24.260 --> 02:31.100
suggests, from Barbara Liskov, and it will get its own episode someday. The Interface Segregation Principle

02:31.100 --> 02:36.600
is an application of the Gang of Four's Adapter pattern to reducing the dependency of one class

02:36.600 --> 02:43.280
on the whole API surface of another class. The Dependency Inversion Principle is the natural

02:43.280 --> 02:48.840
consequence of applying the Liskov Substitution and Open Closed Principles, discovered through

02:48.840 --> 02:54.540
applying the Gang of Four's Template Method pattern. It requires that concrete implementations depend on

02:54.540 --> 03:02.280
abstract interfaces. Now, if you take a look at Martin's article, The Principles of OOP, link also in

03:02.280 --> 03:08.720
show notes, you find six other principles. These all relate to software packages, which are typically

03:08.720 --> 03:14.240
physical realizations of logical composition, so they tell us something useful about modularity too.

03:15.360 --> 03:20.860
Three of these principles are about cohesion. That is, they say what things belong together in

03:20.860 --> 03:28.080
packages. These are the Release-Reuse Equivalency principle. The way that you reuse something should

03:28.080 --> 03:33.500
be through a deliberately defined package. The Common Closure principle. If you change things

03:33.500 --> 03:39.580
together, you package them together. And the Common Reuse principle. If someone uses things together,

03:39.820 --> 03:46.580
you package them together. Three of these principles are about coupling. That is, they say how packages

03:46.580 --> 03:52.440
can rely on things that aren't in the packages. They are the Acyclic Dependencies principle.

03:52.440 --> 03:59.100
No dependencies may be transitively their own dependence. The Stable Dependencies principle.

03:59.780 --> 04:04.540
Dependencies shouldn't change more frequently than their dependence. And the Stable Abstractions

04:04.540 --> 04:11.740
principle. More abstraction implies more stability. Many of the packaging principles are very similar to

04:11.740 --> 04:18.040
the solid principles, which are about designing classes, to the point where you could get there by

04:18.040 --> 04:23.340
applying the SOLID principles at the package level. There are two reasons that this makes sense.

04:23.900 --> 04:29.400
Firstly, both the class and the packaging principles have the same underlying set of values,

04:29.900 --> 04:34.760
which aim to increase cohesion, putting related things together and unrelated things apart,

04:35.220 --> 04:44.300
and decrease coupling, unrelated things not only apart but separated. Secondly, classes and packages

04:44.300 --> 04:50.280
are both forms of modules. So the class principles and the packaging principles are both modularity

04:50.280 --> 04:57.300
principles. Which all brings us back nicely to the Single Responsibility Principle and to Parnas's paper.

04:57.300 --> 05:12.660
This is the advert break. It starts now. This episode is brought to you by me, Graham Lee. But really, by you.

05:13.600 --> 05:20.660
Chiron Codex is a community of people who are learning how to become better software engineers by adopting AI augmentation

05:20.660 --> 05:27.620
in a thoughtful way. We aren't outsourcing our understanding to coding assistants, like Claude or Codex,

05:27.820 --> 05:35.580
but becoming software engineering centaurs by using AI tools to improve our knowledge and the quality of our work.

05:36.180 --> 05:42.980
Join the community over on Patreon to find out about interaction patterns that improve your work with AI coding tools,

05:43.400 --> 05:49.900
running LLMs for software development locally, discussions of recent research in the field, and more.

05:50.660 --> 05:57.320
If you're a software engineer who's interested in the promise of AI tools, but sceptical about handing your

05:57.320 --> 06:04.380
skills over to the computer, this is the community for you. Go to patreon.com slash Chiron Codex,

06:04.620 --> 06:14.040
that's C-H-I-R-O-N-C-O-D-E-X, now for more information and to join. Use the gift link in the show notes

06:14.040 --> 06:20.880
to get your first month of Insider access completely free. Alternatively, you can show your appreciation

06:20.880 --> 06:31.900
by donating at Ko-fi, that's ko-fi.com slash Chiron Codex, K-O-F-I dot com. Direct support by my audience

06:31.900 --> 06:37.800
is the only revenue I get for my work as a software engineer and communicator, so your support really

06:37.800 --> 06:43.300
means a lot to me, and makes it possible for me to produce this podcast. Thank you so much.

06:44.460 --> 06:47.440
That was the advert break. It's over now.

06:47.440 --> 06:58.960
Why do we care about coupling and cohesion? Is there a grand unifying theory of modularity that,

06:59.360 --> 07:05.460
using an analogy from particle physics, introduces the modulon field that explains both the coupling

07:05.460 --> 07:11.660
force and the cohesive force? There is, and it's actually Martin's Single Responsibility Principle.

07:11.660 --> 07:17.120
As I argued on my blog, again link in show notes, and this time I do still agree with what I wrote

07:17.120 --> 07:23.200
in the post, the Single Responsibility Principle is usually erroneously taken to mean every class

07:23.200 --> 07:28.100
should have a single responsibility, the phrase Michael Feathers uses in Working Effectively

07:28.100 --> 07:35.260
with Legacy Code. Martin's actual wording was, "when you write a software module, you want to make sure

07:35.260 --> 07:40.720
that when changes are requested, those changes can only originate from a single person, or rather,

07:40.720 --> 07:45.540
a single tightly coupled group of people, representing a single narrowly defined business

07:45.540 --> 07:51.480
function"". These maybe actually say the same thing. Martin's statement is more powerful than

07:51.480 --> 07:58.980
Feathers', because it explicitly shows that software is a collaborative activity. The developer isn't the

07:58.980 --> 08:04.480
only person choosing what software gets made and how. Other people also make choices. If you don't

08:04.480 --> 08:08.160
remember that other people make choices, you might create a design that has hidden coupling.

08:08.160 --> 08:14.960
Someone else can make a decision that changes multiple modules. And hidden incohesion, multiple

08:14.960 --> 08:20.760
decisions, potentially from multiple people, can change a single module. So the underlying value,

08:20.920 --> 08:27.580
the modulon field, is decision-location equivalence. A decision should be in a single place, which

08:27.580 --> 08:32.640
minimises coupling, and a place should contain a single decision, which maximises cohesion.

08:32.640 --> 08:40.120
And that is what Parnas describes in the Criteria to be Used in Decomposing Systems into Modules.

08:40.720 --> 08:44.620
With the above discussion in place, I can jump directly to his conclusion, quote,

08:44.620 --> 08:51.100
"We propose instead that one begins with a list of difficult design decisions, or design decisions

08:51.100 --> 08:56.740
which are likely to change. Each module is then designed to hide such a decision from the others."

08:57.000 --> 09:02.880
End quote. He's given us decision-location equivalence, now known as the modulon field.

09:04.280 --> 09:09.220
Unfortunately, the answer to why he's given us that is the one I gave at the top of the episode.

09:09.220 --> 09:14.640
The thing that people were doing before he wrote his paper, and still are, even though both he and

09:14.640 --> 09:20.420
Martin have demonstrated the better way. They're thinking in flowcharts. When he said we propose

09:20.420 --> 09:26.940
instead that one begins with a list of design decisions, that is, instead of beginning with

09:26.940 --> 09:33.380
the data flow through the system. First the user does this, then the system does this, then it does

09:33.380 --> 09:38.380
that, then it replies. So I need three to four modules, depending on whether the user action and the

09:38.380 --> 09:44.080
response are in different modules. They almost never are, by the way. A typical layer cake

09:44.080 --> 09:49.480
architecture treats the whole system as if it's a single model-view-controller triad, with three

09:49.480 --> 09:56.620
modules called, unsurprisingly, models, views and controllers. The user interacts with the view, which

09:56.620 --> 10:02.660
isn't how MVC as described actually works, but that's another episode. The view messages the controller

10:02.660 --> 10:09.780
which…okay, there's a debate here. Should you have skinny controllers and fat models, and I'm sorry

10:09.780 --> 10:14.840
for the terminology, but that's what they use. They haven't updated to universal models like Apple did

10:14.840 --> 10:19.800
with universal binaries. Should you have skinny controllers and fat models, in which case the

10:19.800 --> 10:24.740
controller passes the message onto the model, or fat controllers and skinny models, in which case the

10:24.740 --> 10:30.520
controller does some work and updates the model. Again, in MVC as described, there's no question to answer

10:30.520 --> 10:37.460
here. And again, that's another episode. Anyway, the model changes for some reason, the view observes the

10:37.460 --> 10:45.520
changes, and shows the updated state to the user. In that world, whichever side of the model controller

10:45.520 --> 10:52.500
skinniness fence you sit, almost all decisions are incohesive, because almost all decisions affect a view,

10:52.580 --> 10:57.720
a model, and a controller. Almost all modules are coupled, because the view depends on the controller

10:57.720 --> 11:04.920
to know what messages to send, and on the model to know what fields to display. The controller depends

11:04.920 --> 11:10.440
on the view, uh-oh, we've just violated the cyclic dependencies principle, to short-circuit some state

11:10.440 --> 11:16.100
changes, and the controller depends on the model to tell it how to update, and the model maybe depends

11:16.100 --> 11:23.100
on the view, uh-oh, to tell it about changes. In practice, you don't have to stop at three layers.

11:23.100 --> 11:28.420
I've even seen microservice architectures, where each microservice implements a step in a flowchart,

11:28.820 --> 11:33.200
with the effect that all services are tightly coupled, and all decisions are incohesive,

11:33.600 --> 11:38.200
and the application response time is restricted by the rate at which Kubernetes clusters can play

11:38.200 --> 11:44.980
ping-pong within themselves. Layers themselves aren't bad. Dijkstra's string-of-pearls metaphor,

11:45.360 --> 11:51.480
recalling episode 63, has modules that sit at one layer, and call down to the modules in another layer,

11:51.480 --> 11:57.800
but never up or across. His layers are levels of abstraction, starting with the most specific of

11:57.800 --> 12:02.580
hardware facilities at the bottom layer, moving up to the most specific of solutions to problems,

12:03.140 --> 12:07.880
with the middle layers being transitions through abstract computational requirements to abstract

12:07.880 --> 12:14.320
solution components. The layers are only a problem when they ignore the modulon field,

12:14.320 --> 12:22.380
and violate decision-locality equivalents. As Parnas says, "we must conclude that hierarchical structure

12:22.380 --> 12:28.400
and clean decomposition are two desirable but independent properties of a system structure".

12:29.460 --> 12:34.360
As ever, share your thoughts on this episode in the comments, on the sicpers.info website,

12:34.440 --> 12:41.640
that's S-I-C-P-E-R-S dot info, or email grahamlee at acm.org. Please recommend the podcast to your

12:41.640 --> 12:46.780
friends or review the show at your podcast service. You can find more info at the Patreon page linked

12:46.780 --> 12:53.120
in the show notes and the reading list at se100.chironcodex.com. Let's talk soon.

