encapsulation

Enforce encapsulation by routing state mutations through validated public methods with private fields.

13|2|Updated Jan 21, 2026
One-click install
npx skills add https://github.com/yanko-belov/code-craft --skill encapsulation
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: encapsulation
Source: https://github.com/yanko-belov/code-craft/tree/main/skills/encapsulation
Command: npx skills add https://github.com/yanko-belov/code-craft --skill encapsulation

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Encapsulation helps maintain invariants by preventing external access to internal state and by routing mutations through controlled methods.

Core Features & Use Cases

  • Hides internal data via private fields and exposes behavior through methods.
  • Prevents invariants from being violated by external callers and supports safe evolution.
  • Recommends getters/setters with validation and returns copies for collections to avoid aliasing.

Quick Start

Start by identifying public mutable state in a class, replace it with private fields, add public methods to mutate state with validation, and ensure callers use these methods rather than directly changing fields.

Frequently Asked Questions about encapsulation

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

FAQPage Schema
How do I prevent external mutation of internal object state in object-oriented design?

To prevent external mutation of internal object state, use private fields for data and expose behavior through public methods. This ensures all state changes occur through validated methods, maintaining invariants and supporting safe code evolution.

What is the best way to protect class invariants from being violated by external callers?

The best way to protect class invariants is to hide internal data via private fields and route all mutations through controlled public methods. This prevents external callers from directly changing fields and breaking object integrity.

Why should getters and setters return copies of collections in object-oriented codebases?

Getters and setters should return copies of collections to avoid aliasing, which occurs when external callers hold direct references to internal data. Returning copies or read-only views preserves invariants by preventing unvalidated external state changes.

When do I need to use encapsulation practices in my codebase?

You need to use encapsulation practices in typical object-oriented codebases where invariants must be maintained. It is required when internal object state must be protected from direct external access and all mutations need to occur through validated methods.