AlgoViz

Separation of concerns

medium 32 min

Give each question a program must answer its own place, and let the places meet through small openings.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:04

Separation of concerns

1:03Reel

Theory

Read or listen first. About 1 min.

What it is

A concern is a question you can think about alone: how is this shown, when is it allowed, where is it kept, who may ask. Separation of concerns gives each one its own place in the code.

Separation is only half. Places must also meet, through something small: a method, a message, a file format. A wall with a narrow hatch is a good design. A wall full of holes is decoration.

Why it exists

Concerns change at different times. A new page layout should not put the rule about seats at risk.

You can study, test and replace one part while the others stand still. People with different skills can work at the same time.

A short history

  1. 1974

    Edsger Dijkstra uses the phrase in EWD447, dated 30 August 1974. They mean a way to order thoughts: study one aspect in depth, and remember that it is one aspect.

  2. 1997

    Kiczales and colleagues present aspect-oriented programming at ECOOP: some concerns cut across every class, and need a home of their own.

  3. 2015

    Martin Fowler describes presentation, domain and data layers, and advises splitting by business area at the top once a layer grows big.

Words to know

Concern
One question that can be thought about alone.
Layer
A group of code with one concern, which calls only the layer below or an interface.
Cross-cutting concern
One that every feature needs, such as logging or access.
Scattering
One concern spread over many places.
Tangling
Many concerns in one place.
Wrapper
An object or function with the same shape as what it surrounds. Also middleware or decorator.
Vertical slice
All layers of one feature, kept together.

The solution, twice

Reach the regular design first. Then go where a FAANG interviewer digs.

How it works

Step through the idea, or press Play.

Step 1 of 4

List the questions. Show, decide, keep, guard, watch

Understand it

Start here

Three rooms and two hatches

The laundry

The smell

Tangled and scattered

FaultLooks likeCostsRepair
TanglingOne function: JSON, rule, SQL, mail, logNo test without everythingSplit by question
ScatteringForty handlers, each with its own token checkThe forty-first forgetsOne wrapper

Before: one class for every question

SignUpHandler+ handle(request)- checkToken(request)- countSeats(sql)- html(message)

1/2Four questions, one class

Who is asking, is there a seat, how is it kept, how does the page look.

uses

The split

Edge, rules, storage

After: each question has a home

SignUp+ join(id, person)Workshop- seats: int+ join(person)

1/3Rules first

Workshop knows when it is full. SignUp runs the steps of one sign-up.

usesimplements

Why the interface belongs to the rules: see dependency inversion.

The direction of the last arrow is explained in dependency inversion. How to size one class is single responsibility. Separation of concerns works on every scale: functions, classes, modules, systems.

In motion

One request crosses the hatches

What crosses each boundary

JSON never reaches the rules. Rows never reach the page.

Cross-cutting

What every request needs

  • Wrapper

    Same shape as the handler, surrounds it. Plain code, visible order. Start here.

  • Middleware

    The framework keeps a list of wrappers. Global, so watch what not all routes need.

  • Aspect or annotation

    @Transactional. Short, but the behaviour is far from the line that shows it.

  • Sidecar

    Another process beside yours does retries or encryption. Any language, one more thing to run.

  • Base class

    Every handler extends BaseHandler. One chain for all, hard to leave out a piece.

In through the wrappers, out again

Judgement

Where should this line of code go?

Place a line by its question

Place it

LinePlace
person must be a textEdge
A workshop has 12 seatsRules
insert into peopleStorage
Each request needs a tokenWrapper
Only the owner may cancelRules: it needs the booking
Dates shown as 5 MayEdge

Split by layer or by feature

Features outside, layers inside. A change of one feature stays in one folder.

Logging is needed in forty handlers. Where does it go?

BreaksCopy it into each
How
Two lines at the top and bottom of each handler.
Limit
Formats drift, and an early return skips the last line.
Choose when
Never for more than a few.
WorksA base class
How
BaseHandler.handle logs and calls doHandle.
Limit
All share one chain. A job without HTTP cannot use it.
Choose when
A framework already demands the base class.
StrongOne wrapper
How
logged(handler) returns a handler, put together in the root.
Limit
Order must be chosen. Business events still need a log line inside the rules.
Choose when
The need is the same for all.

Limits

When the wall is wrong

A useful wall

  • Two sides change at different times
  • You can test one side alone
  • Little crosses it

Decoration

  • Each change edits both sides
  • Classes that only pass calls on
  • The same object, copied four times

Separation is never perfect, as Dijkstra already wrote. Speed, for example, belongs to no layer: a slow page may need one query that knows what the page shows. Make the exception visible, and keep it in one named place.

Summary

The whole topic in one minute.

Name the concerns, give each one a place, and make the opening between places small. Tangling is many concerns in one place. Scattering is one concern in many.

Layers hold what differs from step to step. Wrappers hold what is the same for every request, and their order matters.

Too many places is also a fault. If one small change always walks through six files, the line is in the wrong place.

  • A concern is a question you can think about alone.
  • Tangling: many here. Scattering: one everywhere.
  • Edge translates, rules decide, storage keeps.
  • Only small, plain data crosses a boundary.
  • Wrap what is equal for all, and choose the order.
  • If every change crosses every wall, move the wall.

Sources