moonbitlang/moonbit-agent-guide/moonbit-refactoring
Claude Code and agent skill by moonbitlang/moonbit-agent-guide
- community
Checking that the SKILL.md file is still in the repository.
Refactor MoonBit code to be idiomatic: shrink public APIs, convert functions to methods, use pattern matching with views, add loop invariants, and ensure test coverage without regressions. Use when updating MoonBit packages or refactoring MoonBit APIs, modules, or tests.
Install
npx vskill@latest install moonbitlang/moonbit-agent-guide/moonbit-refactoringSKILL.md
MoonBit Refactoring Skill
Intent
- Preserve behavior and public contracts unless explicitly changed.
- Minimize the public API to what callers require.
- Prefer declarative style and pattern matching over incidental mutation.
- Use view types (ArrayView/StringView/BytesView) to avoid copies.
- Add tests and docs alongside refactors.
Workflow
Start broad, then refine locally:
- Architecture first: Review package structure, dependencies, and API boundaries.
- Inventory public APIs and call sites (
moon ide doc,moon ide find-references). - Pick one refactor theme (API minimization, package splits, pattern matching, loop style).
- Apply the smallest safe change.
- Update docs/tests in the same patch.
- Run `moon check`, then `moon test`.
- Use coverage to target missing branches.
Avoid local cleanups (renaming, pattern matching) until the high-level structure is sound.
Improve Package Architecture
- Keep packages focused: aim for <10k lines per package.
- Keep files manageable: aim for <2k lines per file.
- Keep functions focused: aim for <200 lines per function.
Splitting Files
Treat files in MoonBit as organizational units; move code freely within a package as long as each file stays focused on one concept.
Splitting Packages
When spinning off package A into A and B:
- Create the new package and re-export temporarily:
``mbt // In package B using @A { ... } // re-export A's APIs ` Ensure moon check` passes before proceeding.
- Find and update all call sites:
``bash moon ide find-references <symbol> ` Replace bare f with @B.f`.
- Remove the
usestatement once all call sites are updated.
- Audit and remove newly-unused
pubAPIs from both packages.
Guidelines
- Prefer acyclic dependencies: lower-level packages should not import higher-level ones.
- Only expose what downstream packages actually need.
- Consider an
internal/package for helpers that shouldn't leak.
Minimize Public API and Modularize
- Remove
pubfrom helpers; keep only required exports. - Move helpers into
internal/packages to block external imports. - Split large files by feature; files do not define modules in MoonBit.
Local refactoring
Convert Free Functions to Methods + Chaining
- Move behavior onto the owning type for discoverability.
- Use
..for fluent, mutating chains when it reads clearly.
Example:
To make the transition smooth, place #as_free_fn(old_name, ...) on the method; it emits a deprecated free function old_name that forwards to the method. Then you can check call sites and update them gradually by looking at warnings. Example (chaining):
Prefer Explicit Qualification
- Use
@pkg.fninstead ofusingwhen clarity matters. - Keep call sites explicit during wide refactors.
Example:
Simplify Enum Constructors When Type Is Known
When the expected type is known from context, you can omit the full package path for enum constructors:
- Pattern matching: Annotate the matched value; constructors need no path.
- Nested constructors: Only the outermost needs the full path.
- Return values: The return type provides context for constructors in the body.
- Collections: Type-annotate the collection; elements inherit the type.
Examples:
Pattern Matching and Views
- Pattern match arrays directly; the compiler inserts ArrayView implicitly.
- Use
..in the middle to match prefix and suffix at once. - Pattern match strings directly; avoid converting to
Array[Char]. String/StringViewindexing yieldsUInt16code units. Usefor ch in sfor Unicode-aware iteration.
We Prefer Pattern Matching Over Small Functions
For example,
We can pattern match directly; it is often clearer and equally readable:
MoonBit pattern matching is pretty expressive, here are some more examples:
Char literal matching
Use char literal overloading for Char, UInt16, and Int; the examples below rely on it. This is handy when matching String indexing results (UInt16) against a char range.
Use Nested Patterns and is
- Use
ispatterns insideif/guardto keep branches concise.
Example:
Prefer Functional Loops to Mutation When Possible
- Use functional state update
Example:
- Functional loops also accept range loops, so they can be simplified:
Prefer Range Loops to Simple Indexing
- Use
for i in start..<end { ... },for i in start..<=end { ... },for i in large>..small, orfor i in large>=..smallfor simple index loops. - Use
for x in xs {...}if xs is an iterator(e.g, Array) orfor i,x in xs {...}if you also want to use the index - Keep functional-state
forloops for algorithms that update state.
Example:
Loop Specs (Dafny-Style Comments)
- Add specs for functional-state loops.
- Skip invariants for simple
for x in xsloops. - Add TODO when a decreases clause is unclear (possible bug).
Example:
Tests and Docs
- Prefer black-box tests in
*_test.mbtor*.mbt.md. - Add docstring tests using
mbt checkfenced blocks for public APIs, and verify withmoon check && moon test.
Example:
Coverage-Driven Refactors
- Use coverage to target missing branches through public APIs.
- Prefer small, focused tests over white-box checks.
Commands:
Moon IDE Commands
Use these commands for reliable refactoring.
Example: spinning off package_b from package_a.
Temporary import in package_b:
Steps:
- Use
moon ide find-references <symbol>to find all call sites ofaandB. - Replace them with
@package_a.aand@package_a.B. - Remove the
usingstatement and runmoon check.
Security
- Security scanPassed, 90 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingmoonbit-refactoring/SKILL.md
Popularity
- Repository stars
- 54
- Repository forks
- 7
- 7-day trend score
- 20
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 20 Mar 2026, 7 months ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/moonbitlang/moonbit-agent-guide/moonbit-refactoring)