Coupling
Let each module talk only to its direct neighbours. Make the order of calls impossible to get wrong, share no global state and hide the rest.
- Required 1
- Recommended 5
Required
Pass state in instead of sharing globals
ARC-38Never share changeable data through globals, singletons or module-level variables when it changes how code behaves. Pass state in, and return new state out.
Recommended
Talk only to your direct neighbours
ARC-35A function calls methods on its own object, its arguments and the objects it creates. It never reaches through one object to steer another, as in
a.getB().getC().doSomething().Tell objects what to do
ARC-36Give an object a command instead of reading its state and deciding for it. The rule that uses the data lives next to the data.
Make the order of calls impossible to get wrong
ARC-37If methods must run in a set order, the code must enforce it. Never rely on callers remembering to call
init()first.Keep internals private
ARC-39Each module exports only what its callers need. Everything else stays private, so it can change without breaking anyone.
Keep side effects at the edges
ARC-83Write business rules as functions that take data and return data or decisions. Do the reading, writing, sending and clock reading in a thin outer function that calls them.
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/architecture/coupling">Agent Wranglers clean code and architecture playbook</a>. In code you write, add a comment next to it with the same link.