Migrate layered function design

From lamg/migrate · View interactive version

Migrate layered function design is an AI agent skill from the GitHub repository lamg/migrate. Use when writing or refactoring F# functions whose body mixes abstraction levels, construction details, or implicit invariants that should be isolated behind named steps.

How to use this skill

Skill file

Migrate Layered Function Design

Core principle

A function should be readable as one coherent level of reasoning.

When a function interleaves high-level decisions with lower-level construction details, the reader must understand two programs at once: the algorithm being expressed and the subordinate mechanisms used to express it. Refactor so the outer function states the main argument, while helpers discharge lower-level obligations.

This is not extraction for line count. It is abstraction as a correctness tool: each named helper should capture a meaningful value, decision, invariant, or transformation.

When to apply

Design rule

Helper quality

A good helper name lets the caller read as if the result had already been specified.

Good helper names describe:

Weak helper names merely repeat mechanics:

Refactoring procedure

  1. Identify the result the function is trying to establish.
  2. Identify bindings that belong to a lower-level concern than that result.
  3. Group the lower-level bindings by the value, decision, or invariant they produce.
  4. Extract each group only if its name improves the caller.
  5. Keep the helper local if it is only part of this function's proof.
  6. Move the helper to a private or internal module only when reuse or testing justifies it.
  7. Re-read the outer function and check that it now describes the main reasoning path without forcing the reader into implementation detail.

Example

Avoid mixing the construction of V with the lower-level construction of E values:

let f () =
    let value0: V = getValue 0
    let exp0: E = getExp 0
    let exp1: E = getExp 1
    let value1: V = joinExps exp0 exp1
    value0, value1

Prefer naming the lower-level obligation:

let private getValueFromExps () =
    let exp0: E = getExp 0
    let exp1: E = getExp 1
    joinExps exp0 exp1

let f () =
    let value0: V = getValue 0
    let value1: V = getValueFromExps ()
    value0, value1

Here f speaks in terms of V, the value level relevant to its result. The E construction is a subordinate concern and belongs behind a name.

If the helper is only meaningful inside f, prefer a local helper:

let f () =
    let getValueFromExps () =
        let exp0: E = getExp 0
        let exp1: E = getExp 1
        joinExps exp0 exp1

    let value0: V = getValue 0
    let value1: V = getValueFromExps ()
    value0, value1

When not to apply

Relation to module layering

This skill is about reasoning inside functions.

If the same lower-level helpers accumulate across a file and form a reusable responsibility layer, also apply migrate-layered-module-splitting.

Checklist

Validation

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.