law-of-demeter

Detect chained calls and enforce the Law of Demeter across multiple languages.

24|1|Updated Sep 15, 2023
One-click install
npx skills add https://github.com/j5ik2o/event-store-adapter-js --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/event-store-adapter-js/tree/main/.agents/skills/law-of-demeter
Command: npx skills add https://github.com/j5ik2o/event-store-adapter-js --skill law-of-demeter-j5ik2o

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

デメテルの法則(最小知識の原則)に基づくコードレビューと設計支援。オブジェクトの連鎖呼び出し(Train Wreck)を検出し、直接の友人とのみ会話する設計へ変換する。結合度の低減と変更容易性の向上を促進する。コードレビュー・新規実装・リファクタリング時にオブジェクト間の結合が深い場合に使用。

Core Features & Use Cases

  • Train Wreckの検出と回避をガイド
  • 委譲メソッドの導入による直接的な友人だけと会話する設計への移行
  • Java/Kotlin/Scala/TypeScript/Python/Ruby/Go/Rust など複数言語に適用可能なパターン集
  • レビューやリファクタリング時の設計判断をサポートする実践的ガイド

Quick Start

Identify the deepest chain of calls and replace it with a delegation method on the outer object.

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 train wreck code patterns caused by deep object chaining?

Fix train wreck patterns by identifying the deepest call chains and replacing them with delegation methods on the outer object. This enforces the Law of Demeter, ensuring objects only talk to direct friends to reduce coupling.

What is the Law of Demeter and when should I apply it during code review?

The Law of Demeter is the principle of least knowledge, dictating that objects should only interact with immediate friends. Apply it during code reviews and refactoring when you detect deep object coupling and chained method calls across multiple languages.

Can I apply the Law of Demeter to languages like Kotlin, Go, and Rust?

Yes, you can apply the Law of Demeter across Java, Kotlin, Scala, TypeScript, Python, Ruby, Go, and Rust. Standardized guidelines and delegation patterns help minimize train wreck calls and reduce object coupling in all these languages.

What is the best way to reduce object coupling in a multi-language codebase?

The best way to reduce object coupling is to introduce delegation methods that encapsulate internal object structures. This hides implementation details and promotes direct friend communication, improving maintainability across different programming languages.

Why does the Law of Demeter improve code maintainability during refactoring?

The Law of Demeter improves maintainability by minimizing dependencies between distant objects. Replacing train wreck call chains with proper delegation localizes changes, meaning modifications to an internal object are less likely to break external callers.

When should I avoid using delegation to fix train wreck code?

Avoid adding delegation methods if it results in excessive boilerplate or creates inappropriate objects merely to satisfy the Law of Demeter. If a chain represents a stable data structure or fluent interface, forcing delegation may unnecessarily complicate the design.