Information Expert
Information Expert
“Assign responsibility to the class that has the information needed to fulfill it.” – Craig Larman
The most basic principle of responsibility assignment. If you need to calculate a total, assign that method to the class that has the line items.
When to use
When deciding where to put a method. Ask: “Who has the data required to do this work?”
Why it matters
- Encapsulation: Data and behavior stay together.
- Cohesion: Classes focus on their own data.
- Low Coupling: Other classes don’t need to query data just to process it elsewhere.
Signs of Violation
- Feature Envy: A method in Class A accesses the data of Class B excessively to verify or calculate something.
- Logic is spread across “Manager” or “Service” classes while data objects are anemic (just getters/setters).
Explanation
Problem
If you separate data from behavior, you end up with “Anemic Domain Models” where objects are just bags of data, and logic is scattered in procedural service classes. This increases coupling because the services become dependent on the internal structure of the data objects.
Solution
Look at the information required to perform a task. Find the class that owns that information. Add the method there.
Real world analogy
If you need to know a user’s balance, you ask the Bank Account (which has the transaction history), you don’t grab the transaction history yourself and calculate it on a napkin.
Pros and Cons
| Pros | Cons |
|---|---|
Comparison
- Feature Envy (Smell): Violating Information Expert often leads to Feature Envy.
Code example
Typescript
Bad (Violation)
Good (Adherence)
PHP
Bad (Violation)
Good (Adherence)