---
title: "Talk only to your direct neighbours · Agent Wranglers"
url: https://agentwranglers.se/playbook/architecture/coupling/law-of-demeter
updated: 2026-10-20
licence: "All rights reserved"
---

# Talk only to your direct neighbours

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Coupling](https://agentwranglers.se/playbook/architecture/coupling)
- Rule: A 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()`.
- How: When you need something deep inside an object, ask that object to do the work. Add a method on the nearest object that hides the path. Data objects with public fields are fine to read, because they have no behaviour to hide. Chains on one object are fine too. Examples are fluent builders such as `z.string().trim()` and collection pipelines such as `items.map().filter()`.
- Check: A lint rule or a grep fails a method call on an object returned by another object's getter, such as `order.getCustomer().getAddress()`. Reviewers fail any that slip past. Builders and pipelines on one value pass. Example tools are PMD's `LawOfDemeter` rule for Java, Exakat for PHP, or a custom ESLint rule on long member chains.

Example:

```js
// Bad: the checkout knows how a customer stores their wallet
const card = order.getCustomer().getWallet().getDefaultCard();
card.charge(order.total());

// Good: the order asks its customer, who asks its wallet
order.collectPayment();
```

```python
# Bad
card = order.customer.wallet.default_card
card.charge(order.total)

# Good
order.collect_payment()
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers architecture playbook: https://agentwranglers.se/playbook/architecture/coupling/law-of-demeter`.

- Next: [Tell objects what to do](https://agentwranglers.se/playbook/architecture/coupling/tell-dont-ask)
- [Coupling](https://agentwranglers.se/playbook/architecture/coupling)
