> For the complete documentation index, see [llms.txt](https://gpcoder.gitbook.io/clean-code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gpcoder.gitbook.io/clean-code/bonus/lod-principle.md).

# LoD Principle

**The Law of Demeter (LoD) or principle of least knowledge says a module should not know about the internals of the objects it manipulates.**&#x20;

An object should not expose its internal structure through accessors because to do so is to expose, rather than to hide, its internal structure.

The advantage of following the Law of Demeter is that the resulting software tends to be more maintainable and adaptable. Since objects are less dependent on the internal structure of other objects, object containers can be changed without reworking their callers.

## LoD Rules

The Law of Demeter says that a method ***f*** of a class ***C*** should only call the methods of these:

1. *C*
2. An object created by *f*
3. An object held in an instance variable of *C*
4. An object passed as an argument to *f*

{% hint style="success" %}

<pre class="language-java"><code class="lang-java"><strong>public class UserService {
</strong>
    private UserRepository userRepository;

    public List&#x3C;User> getUsers(UserFilter filter) {
        QueryBuilder queryBuilder = new QueryBuilder();
        Query query = queryBuilder.build(filter); // rule 2

        List&#x3C;User> users = userRepository.getUsers(query); // rule 3
        
        String direction = filter.getSort(); // rule 4
        
        this.sort(users, direction); // rule 1
    }

    private void sort(List&#x3C;User> users, String direction) {}
}
</code></pre>

{% endhint %}

## Examples

**Example 1:**

{% hint style="danger" %}

<pre class="language-java"><code class="lang-java"><strong>private OrderService orderService;
</strong>
public void getOrderDetail() {
    Oder order = orderService.getOrder();
    Customer customer = orderService.getCustomerService().getCustomer(customerId); // Violate LoD
}
</code></pre>

{% endhint %}

{% hint style="success" %}

```java
private OrderService orderService;
private CustomerService customerService;

public void getOrderDetail() {
    Oder order = orderService.getOrder();
    Customer customer = customerService.getCustomer(customerId);
}
```

{% endhint %}

**Example 2:**

{% hint style="danger" %}

```java
class Company {

    private List<Department> departments;

    Map<DepartmentCode, BigDecimal> costPerDepartment() {
        return departments.stream()
                          .filter(this::costCenter)
                          .collect(Collectors.toMap(Department::getCode, Department::cost));
    }

    private boolean costCentre(Department department) {
        return department.getType() instanceof CostCentre; // Violate LoD
    }
}
```

{% endhint %}

Instead of digging in Department details, it is better to ask, if a department is a cost centre.

{% hint style="success" %}

```java
class Company {

    private List<Department> departments;

    Map<DepartmentCode, BigDecimal> costPerDepartment() {
        return departments.stream()
                          .filter(this::costCentre)
                          .collect(Collectors.toMap(Department::getCode, Department::cost));
    }

    private boolean costCentre(Department department) {
        return department.isCostCentre();
    }
}
```

{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gpcoder.gitbook.io/clean-code/bonus/lod-principle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
