avoid-arbitrary-stateful-objects
Installation
SKILL.md
Summary
Minimize the use of long-lived, mutable objects (state containers). Prefer passing immutable data through stateless functions. When state is required, use explicit creation patterns like Factory or Builder rather than ad-hoc stateful classes.
Rationale
- Predictability: Stateless functions are easier to test and reason about because their output depends only on their input.
- Debugging: Tracking state mutation across an application is a common source of bugs. Hidden state makes reproduction difficult.
- Concurrency: Immutable data is inherently safe to share across async contexts or threads (in environments that support it).
Guidance
- Immutability: Prefer
readonlyproperties andconstvariables. - Pure Functions: Write logic as functions that take data and return new data, without mutating
this. - Controlled State: If you need state (e.g., a connection pool, a cache), use a Factory function to create it explicitly, rather than hiding it inside a class constructor's side effects.