law-of-demeter_skill

This skill helps you write safer TypeScript code by avoiding deep property access and encouraging explicit data access patterns.
  • TypeScript

6

GitHub Stars

1

Bundled Files

2 months ago

Catalog Refreshed

4 months ago

First Indexed

Readme & install

Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.

Installation

Preview and clipboard use veilstrat where the catalogue uses aiagentskills.

npx veilstrat add skill yanko-belov/code-craft --skill law-of-demeter

  • SKILL.md5.5 KB

Overview

This skill enforces the Law of Demeter (don’t talk to strangers) for TypeScript code that accesses nested object properties or chains method calls. It helps detect and prevent chained property or method access that creates tight coupling, fragile tests, and null-safety risks. Use it to encourage objects to expose needed data via their own methods rather than letting callers reach through internals.

How this skill works

The skill inspects code for long dot-chains, chained getters, optional-chaining over multiple levels, and patterns that reach through objects to get data (e.g., obj.a.b.c or obj.getA().getB()). It flags violations and suggests refactor alternatives: add explicit accessor methods on the owning object, ask an object for the data instead of navigating its internals, or return higher-level values from public APIs. It highlights null-safety and testability issues caused by chains.

When to use it

  • When you see multiple dots in an expression (a.b.c or a?.b?.c).
  • When code chains method calls like obj.getA().getB().getC().
  • When callers access another object’s internals instead of asking it.
  • During code reviews to prevent hidden dependencies and fragile callers.
  • When unit tests must mock long object chains to validate behavior.

Best practices

  • Prefer asking the owning object for data via a clear method (e.g., order.getPaymentLast4()).
  • Limit method m in class C to call only this, its parameters, objects it creates, direct components, or globals.
  • Refactor short chains immediately — two dots still indicate coupling.
  • Expose small, intent-revealing accessors instead of exposing structure.
  • Avoid optional chains that hide structural coupling; return undefined explicitly from owner methods when appropriate.

Example use cases

  • Replace company.employees.find(...).address.city with company.getEmployeeCity(id).
  • Convert user.getProfile().getAddress().getZipCode() into user.getZipCode().
  • Refactor order.customer.paymentMethod.last4 into order.getPaymentLast4().
  • Add methods on domain objects so callers don’t need to know nested layouts.
  • During refactor sprints, remove chained accesses to improve testability and reduce breakage.

FAQ

Yes. Even short chains create coupling to multiple objects and can break when internal structures change.

Won’t adding many getters bloat classes?

Add only intent-focused accessors that reflect use cases. They isolate callers from structure and reduce long-term maintenance cost.

Built by
VeilStrat
AI signals for GTM teams
© 2026 VeilStrat. All rights reserved.All systems operational
law-of-demeter skill by yanko-belov/code-craft | VeilStrat