law-of-demeter

Detect chained object access and replace it with explicit accessor methods.

13|2|Updated Jan 21, 2026
One-click install
npx skills add https://github.com/yanko-belov/code-craft --skill law-of-demeter
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: law-of-demeter
Source: https://github.com/yanko-belov/code-craft/tree/main/skills/law-of-demeter
Command: npx skills add https://github.com/yanko-belov/code-craft --skill law-of-demeter

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps developers avoid deep object access and chained method calls by enforcing the Law of Demeter, reducing coupling and fragility in codebases.

Core Features & Use Cases

  • Promotes explicit data access: Replaces nested property chaining with well-defined accessors.
  • Refactoring guidance: Provides patterns to move logic from chained calls into owner methods.
  • Use Case: When you see code like user.getProfile().getAddress().getCity(), apply this skill to introduce a getCity() method on User or related objects.

Quick Start

Detect chained access patterns and replace them with explicit accessor methods. Example: replace order.customer.paymentMethod.last4 with order.getPaymentLast4() by adding a getter on Order or Customer as appropriate.

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 deep nested property access in object-oriented code?

Deep nested property access is fixed by enforcing the Law of Demeter, which replaces long method chains with explicit, well-named accessor methods on the direct object.

What is the Law of Demeter in clean code?

The Law of Demeter in clean code is a design principle that prevents objects from fetching transitive data, requiring you to ask objects directly rather than chaining through nested properties.

How do I refactor chained method calls to reduce coupling?

Refactor chained method calls by moving the logic into the owner object and exposing it through a specific accessor, replacing patterns like user.getProfile().getAddress().getCity() with a direct getCity() method.

Does the Law of Demeter require external tools to apply?

Applying the Law of Demeter requires no external tools, relying entirely on refactoring existing object-oriented codebases to introduce small accessors and reduce structural fragility.

When should I avoid chained method calls in my codebase?

You should avoid chained method calls when they fetch transitive data across multiple objects, as this creates tight coupling and fragility, making the codebase harder to maintain.

What is the best way to replace transitive data fetches in object-oriented design?

The best way to replace transitive data fetches is to introduce explicit accessor methods on immediate objects, ensuring you ask objects directly for data instead of navigating through deep chains.