oop-principles
ACTIVATE when designing classes, and the value objects or collections modelled as classes, or when the user asks about object design, encapsulation, or 'Tell Don't Ask' across languages. Covers cross-language OOP principles: Tell Don't Ask, collection over named properties, Whole Object pattern, iterable collections, self-describing value objects. Language-specific examples and constructs (IteratorAggregate, Symbol.iterator, branded types) live in companion skills (php-oop, ts-oop). DO NOT use for: a value object or an aggregate modelled as an immutable record with functions over it rather than as a class (see craft:ddd-fp-principles).
- License 许可
- MIT
- 最近更新
- 2026-10-13
- Stars
- ★ 9
- Forks
- 2
01 前言 Front Matter
- name
- oop-principles
- description
- ACTIVATE when designing classes, and the value objects or collections modelled as classes, or when the user asks about object design, encapsulation, or 'Tell Don't Ask' across languages. Covers cross-language OOP principles: Tell Don't Ask, collection over named properties, Whole Object pattern, iterable collections, self-describing value objects. Language-specific examples and constructs (IteratorAggregate, Symbol.iterator, branded types) live in companion skills (php-oop, ts-oop). DO NOT use for: a value object or an aggregate modelled as an immutable record with functions over it rather than as a class (see craft:ddd-fp-principles).
02 资源文件 1
- 参考文档index.json689 B
03 SKILL.md 正文
OOP Design — Cross-Language Principles
The rules below are language-agnostic. Concrete syntax-specific examples live in:
php-oop(PHP —IteratorAggregate, readonly properties)ts-oop(TypeScript —Symbol.iterator, branded types)
These principles focus on patterns where Claude tends to produce "ask" code instead of "tell" code.
1. Tell Don't Ask
The object that owns the data exposes the behavior. Do not extract data to make decisions externally.
Smell: caller queries obj.getX(), applies a condition, then calls back into the object.
Fix: push the condition into the object — expose a method that already encodes the decision (obj.getMissingX(), obj.canDoY()).
Criterion: if the same if/match on object state appears in multiple call sites, the object should own it.
2. Collection Over Separate Named Properties
When elements share the same type and undergo the same processing, use an indexed collection — even if the count is known and fixed.
Smell: $frontFile, $backFile, $passportFile (or frontFile, backFile, passportFile) with parallel handling code.
Fix: a single keyed collection (Map<string, File>, array<string, File>) with one loop.
Criterion: same nature + same treatment (upload, validation, display) = collection.
3. Whole Object — Pass the Object, Not Its Primitives
When multiple parameters come from the same object, pass the object. Extracting primitives on the caller side is feature envy.
Smell: add(documentType: doc.type, documentName: doc.originalFileName, downloadUrl: url) — three properties of doc destructured.
Fix: addFromDocument(doc, url) — let the receiver extract what it needs.
Criterion: if a call site reads ≥ 2 properties of the same object to pass them along, pass the object.
4. Iterable Collections with Private Internals
When a collection will be iterated, expose iteration without exposing the underlying array. The language provides a protocol for this (PHP: IteratorAggregate, TS: Symbol.iterator, Python: __iter__, …).
Smell: public property holding the underlying array, allowing coll.files.push(...).
Fix: keep the array private, implement the language's iteration protocol so for ... of coll (or equivalent) works.
Criterion: if a caller iterates the collection, it must not be able to mutate it directly.
5. Self-Describing Value Objects
Include the type / identity in the value object so consumers don't need an external mapping to interpret it.
Smell: { content, originalFileName } + a separate fieldName → fileType mapping kept by the consumer.
Fix: add the type (or other discriminator) inside the value object — { type, content, originalFileName }.
Criterion: if a consumer needs an external lookup to know what kind of value object it received, the type is missing from the object.
Quick Reference
| Rule | Principle |
|---|---|
| Tell Don't Ask | The object exposes behavior, not data to interpret |
| Collection > named properties | Same nature + same processing = indexed collection |
| Whole Object | Pass the entire object, not its extracted primitives |
| Iterable + private | Use the language's iteration protocol, keep internals private |
| Self-describing VO | Include type/identity in the object — no external mapping |
04 同仓库更多 Skills
vitest-test-conventions
ACTIVATE when writing Vitest tests, creating test files, using vi.fn()/vi.mock()/vi.spyOn(), or test factories. ACTIVATE for 'Vitest', 'it.each', 'NestJS testing module', 'supertest', 'vi.fn', 'vi.mock', 'spyOn'. Provides Vitest/NestJS-specific testing patterns; cross-language testing principles (DAMP, AAA, spy over mock, what NOT to test) live in craft:testing-principles. DO NOT use for: node:test files (a file importing node:test) or a project whose tests run under node --test (see node-test:node-test-conventions), TDD workflow/iterations (see vitest:vitest-tdd-workflow), PHP/PHPUnit tests (see phpunit:php-test-conventions).
vitest-tdd-workflow
ACTIVATE when building new features, endpoints, or user stories using TDD with Vitest in a TypeScript/NestJS context. ACTIVATE for '/feature-tdd-dev', 'TDD', 'red-green-refactor' in TS context. Provides Vitest/NestJS-specific TDD examples and commands; cross-language TDD workflow (cross-layer iterations, RED-GREEN-REFACTOR, working app, bug-fix-first-test, mocks-hiding-bugs) lives in craft:tdd-workflow-principles. DO NOT use for: node:test files (a file importing node:test) or a project whose tests run under node --test (see node-test:node-test-conventions), test writing conventions (see vitest:vitest-test-conventions), PHP TDD (see phpunit:php-tdd-workflow).
ts-refactoring
ACTIVATE when refactoring TypeScript code. ACTIVATE for 'refactor', 'extract', 'redesign', 'simplify', or 'clean up' in a TypeScript / Node / NestJS context. This skill provides TS-specific examples for the cross-language refactoring rules defined in craft:refactoring-principles. DO NOT use for: writing new features from scratch, general OOP patterns (see ts-oop).
ts-ports-adapters
ACTIVATE when a TypeScript codebase with no dependency-injection container has to declare a port and wire its adapter — a repository, a clock, an id generator, an event dispatcher the domain depends on. ACTIVATE for 'port', 'adapter', 'SPI', 'dependency inversion' or 'composition root' in a codebase that wires its dependencies by hand. Covers: the port as a type of functions in the domain layer, the adapter as an object literal annotated with it, the handler that takes its dependencies first and its data second, the composition root in the infrastructure layer, and the in-memory stub that substitutes for the adapter in a unit test. DO NOT use for: NestJS, or any container-based DI with decorators and tokens (see nest-ddd-conventions), aggregate operations and smart constructors (see ddd-ts-fp), the Result type itself (see ts-functional).
ts-oop
ACTIVATE when designing TypeScript classes, and the value objects or collections modelled as classes, or when the user asks about object design, encapsulation, Tell Don't Ask, or Symbol.iterator. Provides TS-specific examples for the cross-language OOP rules defined in craft:oop-principles, plus TS-specific patterns (Symbol.iterator, branded types for primitive identifiers). DO NOT use for: a value object or an aggregate modelled as an immutable record rather than a class (see ddd-ts-fp), refactoring methodology (see ts-refactoring).
ts-layer-boundaries
ACTIVATE when wiring a Worker entry point, a Router, a controller, or a domain event consumer in a TypeScript CQRS codebase, or when deciding where a port belongs. ACTIVATE for 'composition root', 'layer boundary', 'ESLint zones', 'domain must not import infrastructure'. Covers: the *Worker.ts composition root, the curried consumer, Router wiring of command endpoints, the five-gesture controller, the domain-to-infrastructure import boundary enforced by ESLint, infrastructure-only ports, in-memory doubles for every port, the read-can-reach-write asymmetry, and read endpoints going through a domain handler. DO NOT use for: the port's own shape and adapter binding (see ts-ports-adapters), the Result type (see ts-functional), domain event structure (see ts-ddd-events).