A product manager asks for one small change: if an order has already shipped, a person has to approve the refund. Sounds like one extra if. Then you open the code. Part of the refund logic lives in the API controller, part in a SQL query, part in a shared helper, and one condition hides in a test fixture nobody wants to touch. The review drags through the afternoon. By the end, the business decision is the easiest part of the PR.
Most of that pain has one cause: the refund rule knows too much. It knows which web framework received the request, which ORM loaded the row and which payment SDK the team picked this year. So when any of those change, the rule gets dragged along, and when the rule changes, it drags all of them along too.
Dependency direction is the habit that fixes this. In plain words: code that changes for business reasons shouldn't depend on code that changes for tooling and infrastructure reasons. Below: the rule itself, what it looks like in code, where you feel the difference, how to keep it from drifting, and when it isn't worth the ceremony.
The rule in one sentence
Robert C. Martin's version, from his 2012 post on Clean Architecture, is short: "source code dependencies can only point inwards." He adds that "nothing in an inner circle can know anything at all about something in an outer circle" (The Clean Architecture). His older list of package principles says the same thing from another angle: "Depend in the direction of stability" (Principles of OOD).
Stable here doesn't mean bug-free. It means the code changes rarely, and only when the business changes its mind. Your refund policy (what's refundable, for how long, when a person must approve) changes when the company's policy changes. The HTTP framework, the database driver and the payment provider's SDK change on their own schedule: a major version bump, a vendor switch, a new auth header. The policy belongs on the stable side, and the arrows should point at it.
A quick way to check: read the import lines at the top of a file. If refunds/policy.ts imports Express, your ORM or the Stripe SDK, the arrow points the wrong way.
What it looks like in code
Here's the refund policy with nothing but refund language in it:
// refunds/policy.ts: speaks refunds, imports nothing from the outside world
export type Order = { status: 'paid' | 'shipped' | 'refunded'; paidAt: Date }
export type Decision = 'approve' | 'needs-review' | 'reject'
const REFUND_WINDOW_DAYS = 30
const DAY_MS = 24 * 60 * 60 * 1000
export function decideRefund(order: Order, now: Date): Decision {
const ageDays = (now.getTime() - order.paidAt.getTime()) / DAY_MS
if (order.status === 'refunded' || ageDays > REFUND_WINDOW_DAYS) return 'reject'
if (order.status === 'shipped') return 'needs-review'
return 'approve'
}
No framework, no database, no vendor. It takes plain data and returns a decision. The outside world lives in the web handler, and the handler depends on the policy:
// http/refund-route.ts: knows Express and the repository, then asks the policy
import { decideRefund } from '../refunds/policy'
app.post('/orders/:id/refund', async (req, res) => {
const order = await orders.findById(req.params.id)
if (!order) return res.status(404).end()
res.json({ decision: decideRefund(order, new Date()) })
})
The handler knows about HTTP and the order repository. The policy knows neither. Move from Express to Fastify next year and the policy file stays the same. Shrink the refund window from 30 days to 14 and the handler stays the same.
What if the policy needs to reach outward, say to ask the payment provider to reverse a charge? Don't import the SDK. Define a small interface on the policy side, named in refund words (reverse(orderId, amount)), and let an adapter implement it with the real SDK. In Martin's post, that's how you cross a boundary using the Dependency Inversion Principle: the inner code owns the interface, the outer code implements it, and the source dependency still points inward. Dependency injection and IoC covers how that adapter gets wired in.
Where you feel the difference
Tests first. When the refund rule depends on a real database and a real payment SDK, every test has to set both up, and people start writing fewer tests. A policy that takes plain data runs in milliseconds for every case the PM would name: inside the window, outside it, already shipped, already refunded. Alistair Cockburn wrote this into the goal when he described hexagonal architecture in 2005: "Allow an application to equally be driven by users, programs, automated test or batch scripts" (Hexagonal architecture). The adapters still need their own tests, and those can focus on plumbing.
Review next. With the rule in one place, a PR can carry two separate questions. "Is this the policy we agreed on?" goes to the PM and whoever knows the domain. "Does this adapter handle the provider safely?" goes to whoever knows the integration. When both live in one long controller method, nobody's sure which question they're answering, and a policy change can slip through looking like a plumbing fix.
Then the conversations. Clean Architecture, ports and adapters, onion: teams can argue about names for weeks. Day to day you can skip the vocabulary and ask one question: does the stable rule know about the detail that changes often, or does the detail point at the rule? If you do want the differences between the named styles, clean vs onion vs hexagonal lays them side by side, and ports and adapters goes deeper on the adapter side.
Let a tool watch the arrows
Arrows drift. Someone in a hurry imports the ORM into the policy folder "just this once", and six months later there's no boundary left. A lint rule catches that in CI. In JavaScript or TypeScript, ESLint's built-in no-restricted-imports rule can ban packages and paths for one folder (ESLint docs):
// eslint.config.js
export default [
{
files: ['src/refunds/**/*.ts'],
rules: {
'no-restricted-imports': ['error', {
patterns: [{
group: ['express', 'stripe', '**/db/**', '**/http/**'],
message: 'Refund policy must not import frameworks, SDKs or adapters.',
}],
}],
},
},
]
Java teams can write the same check as a unit test with ArchUnit. Whichever tool you use, write the rule down where a machine checks it, so reviewers don't have to remember it.
When to skip it
A one-off script, a prototype you'll delete next month or a CRUD screen with no real policy doesn't need a formal boundary. Wrapping every line in an interface turns a small change into paperwork, and that's a cost too. The signal to add one is repetition: the same business rule showing up in a second screen, job or vendor integration, or the team having the same confused review conversation for the third time. At that point the rule has earned a home of its own, with nothing leaking in.
Try this week
- Pick the feature that always feels harder to change than its business rule. Open its files and read the imports at the top of each one.
- Find one business rule that imports a framework, ORM or SDK, and move it into a function that takes plain data.
- Write a test for each case your PM would name for that rule.
- Add one import restriction for that folder so the arrow can't flip back unnoticed.
None of this needs a big refactor. It grows one PR at a time, each time someone stops and asks which way a new import should point.