law-of-demeter

Detect Law of Demeter violations and suggest refactoring patterns for chained method calls.

Updated Jun 18, 2023
One-click install
npx skills add https://github.com/kuroweb/dotfiles --skill law-of-demeter-kuroweb
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: law-of-demeter
Source: https://github.com/kuroweb/dotfiles/tree/main/agents/.cursor/skills/law-of-demeter
Command: npx skills add https://github.com/kuroweb/dotfiles --skill law-of-demeter-kuroweb

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps enforce the Law of Demeter, preventing overly complex object chains and promoting loosely coupled, maintainable code.

Core Features & Use Cases

  • Detects "Train Wreck" patterns: Identifies sequences like a.getB().getC().doX().
  • Guides refactoring: Provides strategies to transform chained calls into direct interactions or delegated methods.
  • Use Case: When reviewing code, if you see a method making many chained calls to access deeply nested data, use this Skill to identify the violation and suggest refactoring patterns.

Quick Start

Review the current code for violations of the Law of Demeter and suggest refactoring.

Frequently Asked Questions about law-of-demeter

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

FAQPage Schema
What is the Law of Demeter in object-oriented programming?

The Law of Demeter is a design guideline for object-oriented programming that restricts object interactions, ensuring a method only talks to immediate friends rather than deeply nested objects, promoting loose coupling and maintainability.

How do I refactor a train wreck anti-pattern from excessive method chaining?

To refactor a train wreck anti-pattern, you should transform chained calls like `a.getB().getC().doX()` into direct interactions by introducing delegated methods on the immediate object, reducing excessive coupling.

Does this Law of Demeter refactoring guidance work with Python and Go?

Yes, this refactoring guidance supports multiple languages including Python, Go, Java, Kotlin, Scala, TypeScript, Ruby, and Rust to detect and eliminate train wreck patterns across diverse object-oriented codebases.

How do I detect Law of Demeter violations during code review?

To detect Law of Demeter violations during code review, look for methods making many chained calls to access deeply nested data, which indicates a train wreck pattern that requires refactoring into delegated methods.

When should I avoid applying the Law of Demeter?

You should avoid strictly applying the Law of Demeter when dealing with data transfer objects, fluent interfaces, or method chaining in functional pipelines, as enforcing it in these contexts can create unnecessary wrapper methods and reduce readability.