安装
npx skillscat add tomevault-io/tomes/masm-named-literals 通过 SkillsCat registry 安装。
技能简介
在MASM和Rust中将内联数字常量替换为命名常量,提高代码可读性、可维护性并实现单一来源重构。
SKILL.md
Replace Magic Numbers with Named Constants
Rule
Numeric literals embedded inline in MASM and Rust code must be promoted to named constants when they represent:
- Memory offsets, slot indices, or layout sizes
- Protocol/tag/type/version discriminants
- Domain values reused in more than one place
Define each constant exactly once. In MASM, declare it in the file's CONSTANTS section (see masm-constants skill). In Rust, define it as an associated constant on the type it describes (Type::CAPACITY, not a free-floating const CAPACITY).
Why
A bare 47 or 0x1234 is invisible to grep, indistinguishable from coincidentally-equal numbers, and drifts when one occurrence is updated and another missed. A named constant documents intent and gives refactors a single source of truth.
Examples
# Good
const ACCOUNT_DATA_PTR = 4
mem_load.ACCOUNT_DATA_PTR
# Bad
mem_load.4 # what is at offset 4?// Good
impl AccountStorage {
pub const MAX_NUM_STORAGE_SLOTS: usize = 255;
}
// Bad
if slots.len() > 255 { ... } // 255 also appears unrelated elsewhereSource: 0xMiden/protocol — distributed by TomeVault.