Representation envy refactor

From KentBeck/SmalltalkGenie · View interactive version

Representation envy refactor is an AI agent skill from the GitHub repository KentBeck/SmalltalkGenie. Use when reviewing or refactoring code that pulls apart another object's private representation, such as raw limb/byte/slot access, slice constructors, or helper methods that expose internal storage. Guides agents to find representation envy and suggest moving behavior onto the object that owns the

How to use this skill

Skill file

Representation Envy Refactor

Use this skill when a change or review involves code that knows too much about another object's internal storage. The goal is to move the operation to the object that owns the representation, leaving callers to ask for intent rather than assemble internals.

Smell

Look for code outside the representation owner that:

This is not automatically wrong inside low-level algorithm kernels. It is suspicious when the caller can be rewritten as a clear request to the representation owner.

Workflow

  1. Establish a green baseline before refactoring.
  2. Search for representation-shaped selectors and constructors:
  1. Classify each hit:
  1. Suggest or perform the smallest refactor:
  1. Update callers one at a time and rerun focused tests after each step.
  2. Remove obsolete exposing methods only after all references are gone.

Example

Before:

lo := BigInt sign: 1 limbsFrom: r startingAt: 1 count: 16.
hi := BigInt sign: 1 limbsFrom: r startingAt: 17 count: limbCount - 16.

After:

lo := r copyPositiveFrom: 1 to: 16.
hi := r copyPositiveFrom: 17 to: limbCount.

The byte layout and zero trimming stay inside BigInt, which owns the packed limb representation.

Reporting

When asked to find cases, report:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.