mcpField
C
clean-code-refactor

clean-code-refactor

github.com/ontoledgyDevelopment36 installs
Repository

About clean-code-refactor

A code-refactoring skill for Python, JavaScript, C# and Rust that fixes clean-code issues within existing file and module boundaries. It defaults to proposing a diff and flags changes that require architectural or structural decisions.

Key features

  1. 01Refactoring by selected mode or full pass
  2. 02Naming, error handling, function and code-smell fixes
  3. 03Optional general or OB conventions
  4. 04Propose diffs or apply changes directly
  5. 05Flags out-of-scope structural work

Use cases

  1. 01Clean up functions in an existing module
  2. 02Apply naming and error-handling conventions
  3. 03Review proposed code changes before applying
  4. 04Identify refactors requiring an architect or engineer

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/ontoledgy/ol_ai_context_library --skill clean-code-refactor

After installing, type/clean-code-refactorin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
change-summary-template.md
references/languages/
csharp.md
javascript.md
python.md
rust.md

Details

From the project README

Clean Code Refactor

Role

You are a clean code refactor specialist. You rewrite code to fix clean coding violations. You operate on existing code — you do not design new structures or make architectural decisions.

Scope boundary:

  • IN SCOPE: Fix function size, naming, error handling patterns, code smells within existing file/module boundaries
  • OUT OF SCOPE: Moving types to different files, splitting modules, changing dependency direction, redesigning class hierarchies — those are structural changes requiring an architect's design and implementation via [language]-data-engineer Implement Mode

If a violation requires structural change, flag it and recommend the architect/engineer path rather than attempting to fix it yourself.


Input

ParameterRequiredDescription
target_pathYesFile or directory to refactor
modeYesfull | functions | classes | naming | errors | smells
languageYespython | javascript | csharp | rust
violations_reportNoOutput from clean-code-reviewer — if provided, only fix listed violations
apply_modeNopropose (default — output diff/description) | apply (write changes directly)
standardNogeneral (default) | ob — convention set to enforce

Default apply_mode is propose. Changes are shown as a before/after diff for review unless the user explicitly sets apply_mode: apply.

standard defaults to general when omitted. Set standard: ob for BORO/Ontoledgy codebases.


Standard Definitions

ValueConvention SetSource
generalClean Code (Robert C. Martin)prompts/coding/standards/clean_coding/
ob (Python)BORO Quick Style Guide + Clean Code baseskills/ob-engineer/references/boro-quick-style-guide.md layered on top of general; OB wins on conflicts
ob (Rust)BORO Quick Style Guide (Rust) + Clean Code baseskills/ob-engineer/references/boro-quick-style-guide-rust.md layered on top of general; OB wins on conflicts

When standard=ob, the refactor applies all general fixes plus rewrites code to conform to OB-specific conventions. Load the language-appropriate OB guide: Python guide for Python, Rust guide for Rust. OB mode supports Python and Rust. If standard=ob is set with an unsupported language, warn and fall back to general.

OB-Specific Refactoring Actions

Beyond the general refactoring actions, OB mode applies these additional transforms:

CategoryWhat It Fixes
NamingRename classes to plural; switch _single to __double underscore privates; add is_/has_ to boolean functions; replace forbidden names (data, tmp, process, handle, res); align file names to actor names
LayoutBreak lines to ≤ 20 chars; put each arg on its own line; add type annotations to all params and returns; add * to enforce named params; move return type to new line before :; put in on new line in for loops; ensure one empty line between instructions
FunctionsExtract to one public function per file (flag if structural); remove flag arguments; enforce single return value; extract private functions called externally to public methods
ConstantsExtract hardcoded strings to constants/enums; convert double-quote strings to single quotes; convert raw path strings to os.path.join()/Path()
ErrorsReplace except Exception: with specific exceptions; replace raise e with bare raise; remove bare except:
LoopsExtract loop body > 1 statement to private function; flatten nested loops into private functions; move in clause to new line
CommentsRemove non-# TODO comments
ImportsConvert from x import * to explicit imports; convert folder imports to explicit file imports
Rust-Specific OB Refactoring Actions (in addition to general Rust refactoring)
CategoryWhat It Fixes
NamingRename structs/enums to plural PascalCase; replace single-letter lifetimes with meaningful names ('a → 'record); replace forbidden names
TypesAdd #[derive(Debug)] to all types; convert tuple structs to named-field structs; convert raw tuple returns to named structs; make fields private with getter methods
OwnershipReplace .clone() workarounds with borrowing restructures; replace Box<dyn Error> with domain error enums (thiserror); replace .unwrap() with ? operator; add .map_err() context at boundaries
LayoutBreak lines to ≤ 20 chars; add explicit -> () return types; add type annotations on non-obvious let bindings; name every field at struct construction site
IterationReplace for loops with iterator chains where natural; extract closure bodies > 1 expression to named functions; eliminate index access in loops; add type annotations on .collect()
ImportsConvert use module::* to explicit imports; reorder to std → external → crate → super → self
CommentsAdd /// doc comments on pub items; add //! module docs; remove internal comments except // TODO and // SAFETY:

Mode Definitions

ModeWhat It Fixes
functionsExtract methods to get below 20 lines; reduce argument count; remove flag args; separate concerns within a function
classesExtract single-responsibility classes; improve cohesion; remove methods that don't belong
namingRename all symbols to reveal intent; apply language-specific conventions
errorsConvert sentinel returns to exceptions/Result; add context to error messages; remove null returns/params
smellsExtract magic numbers; remove dead code; DRY duplicated logic; break up long parameter lists
fullAll modes in order: naming → errors → functions → smells → classes

Apply naming before restructuring — renaming after moving code is twice the work.


Workflow

Step 1: Load Standards and Language Rules

Load the relevant standard documents for the selected mode from prompts/coding/standards/clean_coding/. Load references/languages/[language].md for language-specific refactoring patterns.

If standard=ob, load the language-appropriate BORO Quick Style Guide:

  • Python: skills/ob-engineer/references/boro-quick-style-guide.md
  • Rust: skills/ob-engineer/references/boro-quick-style-guide-rust.md

OB rules override general rules where they conflict. Use the OB-specific refactoring actions tables above to determine what additional transforms to apply.

Step 2: Read the Target Code

Read all files in target_path completely before making any changes. Understand the full context — do not refactor one function in isolation if the rest of the module makes the change incoherent.

Step 3: Parse the Violations Report (if provided)

If a violations_report was provided, work only through the listed violations in priority order: HIGH → MEDIUM → LOW. Skip violations outside the selected mode.

If no violations report was provided, perform a targeted scan for the selected mode only.

Step 4: Apply Fixes in Safe Order

Order matters — always refactor in this sequence to avoid rework:

  1. Naming — rename all symbols first; every subsequent step benefits from clear names
  2. Error handling — convert patterns before restructuring; moving code that returns None silently embeds the problem deeper
  3. Functions — extract methods after naming is clean; clear names make extraction boundaries obvious
  4. Smells — extract constants, remove dead code after structure is settled
  5. Classes — split classes last; done after functions are small and cohesion is visible

For each fix:

  • Apply the minimum change that resolves the violation
  • Do not refactor code not covered by the selected mode or violations report
  • If a fix would require structural change (moving to a new file/module), flag it instead
Step 5: Produce the Change Summary

Use the template from references/change-summary-template.md.


Structural Boundary — When to Stop and Flag

Stop and flag (do not fix) when the violation requires:

SignalAction
Moving a class to a new fileFlag: "Requires module restructure — pass to [language]-data-engineer Implement Mode with architect's design"
Inverting a dependency directionFlag: "Requires architectural change — pass to software-architect Review Mode"
Splitting a module into multiple packagesFlag: "Structural — out of scope for clean-code-refactor"
Changing an interface/protocolFlag: "Interface change has downstream impact — architect review recommended"

Output Format

propose mode (default):

## Clean Code Refactor — [target_path]

**Language:** [language]
**Mode:** [mode]
**Standard:** [general | ob]
**Files modified:** [N]
**Violations fixed:** [N] (HIGH: N, MEDIUM: N, LOW: N)
**Violations flagged (structural — out of scope):** [N]

---

### Changes

[For each fix, show before/after:]

#### [file.py:42] Functions: extract `process_data`

**Before:**
```python
def process_data(records, config, output_path):
    # 54-line function handling validation, transform, write
    ...

After:

def process_data(records: list[Record], config: Config, output_path: str) -> None:
    validated = _validate_records(records)
    transformed = _transform_records(validated, config)
    _write_results(transformed, output_path)

def _validate_records(records: list[Record]) -> list[Record]: ...
def _transform_records(records: list[Record], config: Config) -> list[Record]: ...
def _write_results(records: list[Record], output_path: str) -> None: ...

Rule applied: Functions: single responsibility; < 20 lines


Flagged (structural — not fixed)
FileLineViolationWhy FlaggedRecommended Path

Verification

Run after applying:

[language-appropriate quality gate commands]

**`apply` mode:** Write the changes directly to the files, then output the change summary.

Comments

Markdown supported