Know less, break less
The fewer internal details a caller knows, the easier it is to refactor the callee safely.
The Law of Least Knowledge, often treated as a deeper form of the Law of Demeter, encourages designs where objects talk mainly to their close collaborators rather than reaching through layers of internal structure. It is about reducing unnecessary knowledge and dependency spread across the system.
In Java business systems, train-wreck call chains often appear around nested DTOs, domain graphs, or framework objects. Those call chains make refactoring dangerous because one internal structure change ripples across many callers. Least knowledge keeps object interaction narrower and more stable.
The Law of Least Knowledge is about collaboration shape. Callers should not need deep awareness of the internal object network behind a request. When they do, the design becomes tightly coupled to structure rather than to meaning.
In OOAD terms, the principle often leads to better delegation, more meaningful façade methods, and cleaner responsibility boundaries. It also works well with Tell, Don’t Ask and encapsulation because all three principles reduce leakage of internal detail.
The goal is not to prevent all navigation. The goal is to prevent unnecessary knowledge from spreading.
The fewer internal details a caller knows, the easier it is to refactor the callee safely.
Expose domain-language methods instead of making callers walk object graphs.
This principle helps packages, modules, and services remain less brittle over time.
package org.javaomnibus.ecommerce.principles;
public final class DeliveryPresenter {
public String label(Order order) {
return "Deliver to " + order.deliveryAddressSummary();
}
}
final class Order {
private final Shipment shipment;
Order(Shipment shipment) {
this.shipment = shipment;
}
String deliveryAddressSummary() {
return shipment.addressSummary();
}
}
record Shipment(String city, String country) {
String addressSummary() {
return city + ", " + country;
}
}
Over-applying the principle can create too many thin pass-through methods. The key is meaningful delegation where it protects the design, not ceremony for every single field.