law-of-demeter

Detects chained method calls violating the Law of Demeter and refactors them into delegation-based designs.

Updated Jun 23, 2026
One-click install
npx skills add https://github.com/j5ik2o/marp-ai-base --skill law-of-demeter-j5ik2o
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: law-of-demeter
Source: https://github.com/j5ik2o/marp-ai-base/tree/main/.agents/skills/law-of-demeter
Command: npx skills add https://github.com/j5ik2o/marp-ai-base --skill law-of-demeter-j5ik2o

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Deep object coupling through chained method calls (Train Wrecks like order.getCustomer().getAddress().getCity()) makes code fragile and hard to change. This Skill reviews code for Law of Demeter violations and guides refactoring toward low-coupling designs where objects only talk to their direct friends. ## Core Features & Use Cases - Violation Detection: Identifies Train Wreck patterns, getter chains, and calls on returned objects using the four allowed-call rules (self, parameters, locally created objects, instance fields). - Refactoring Patterns: Provides transformation recipes including delegation methods, purpose-driven naming, parameter passing, and moving behavior into objects, with per-language examples for Java, Kotlin, Scala, TypeScript, Python, Ruby, Go, and Rust. - Exception Awareness: Distinguishes real violations from acceptable chains such as fluent APIs, builders, DTOs, stream operations, and standard library calls, and warns against over-application (delegation method explosion). - Use Case: During a code review of a Java service, you find order.getCustomer().getAddress().getCountry().isShippable(). The Skill flags it as a friend-of-a-friend violation and shows how to add order.isShippable() delegation methods across each level. ## Quick Start Review this class for Law of Demeter violations and refactor any chained method calls into delegation methods.

Frequently Asked Questions about law-of-demeter

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix Law of Demeter violations in my code?▼

Identify the chain's purpose, then add a purpose-named delegation method on the outermost object that delegates through each intermediate level. For example, replace `order.getCustomer().getAddress().getCity()` with `order.getShippingCity()`, adding delegation methods on Order and Customer.

What is a Train Wreck pattern in object-oriented code?▼

A Train Wreck is a chained call like `a.getB().getC().doX()` that traverses an object's internal structure. It violates the Law of Demeter because the caller depends on friends of friends, making the code brittle to structural changes.

Does the Law of Demeter apply to fluent APIs and builders?▼

No, fluent APIs and builder patterns are explicit exceptions because each method returns the same object, so you keep talking to the same friend. Stream operations, DTO field access, and same-type standard library chains like `text.strip().lower()` are also allowed.

Which languages does Law of Demeter refactoring support?▼

The refactoring patterns cover Java, Kotlin, Scala, TypeScript, Python, Ruby, Go, and Rust. Each language section shows delegation methods, plus idiomatic tools like Rails delegate, Go interfaces, Rust traits, and Scala type classes.

When should I not apply the Law of Demeter?▼

Avoid over-application when it causes a delegation method explosion, typically more than three delegation methods per class. That signals a design problem better solved by rethinking responsibility allocation rather than wrapping every field.

Why is my optional chaining still a Demeter violation?▼

Optional chaining like `user?.address?.city` prevents null errors but still exposes internal structure, so the coupling violation remains. Hide the traversal behind a delegation method such as `user.getCity()` that handles the nullability internally.