law-of-demeterlisted
Install: claude install-skill anyulled/my-portfolio-website
# Law of Demeter (Don't Talk to Strangers)
## Overview
**Only talk to your immediate friends, not strangers.**
A method should only call methods on: itself, its parameters, objects it creates, or its direct components. Never reach through an object to access another object's internals.
## When to Use
- Accessing nested properties: `obj.a.b.c`
- Chaining method calls: `obj.getA().getB().getC()`
- Reaching through objects for data
- Long dot chains in your code
## The Iron Rule
```
NEVER chain through objects. Ask, don't reach.
```
**No exceptions:**
- Not for "it's simpler"
- Not for "it's just one chain"
- Not for "the data is there"
- Not for "fewer lines of code"
## Detection: The Chain Smell
If you see multiple dots, you're violating LoD:
```typescript
// ❌ VIOLATION: Reaching through objects
function getEmployeeCity(company: Company, employeeId: string): string {
return company.employees.find((e) => e.id === employeeId)?.address.city; // Reaching into employee, then into address
}
// More violations:
user.getProfile().getAddress().getZipCode();
order.getCustomer().getPaymentMethod().getLast4();
```
## The Correct Pattern: Ask, Don't Reach
Let objects expose what's needed:
```typescript
// ✅ CORRECT: Ask the object directly
class Employee {
constructor(
private name: string,
private address: Address,
) {}
getCity(): string {
return this.address.city; // Employee asks its own address
}
}
class Company {
getEmployeeCities(): Map<stri