Refactor Legacy Code

aj-geddes/useful-ai-prompts/skills/refactor-legacy-code

作者 aj-geddes3f5182cfd739無授權條款355 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫7 個月前更新

Modernize and improve legacy codebases while maintaining functionality. Use when you need to refactor old code, reduce technical debt, modernize deprecated patterns, or improve code maintainability without breaking existing behavior.

AI 產生的概覽

指導在保留現有行為的前提下,系統性地重構遺留程式碼庫。

功能
此技能提供一套結構化的遺留程式碼現代化方法:評估程式碼庫、建立測試安全網、增量重構、現代化過時模式並減少相依性。它附有涵蓋各階段的參考指南與一個完整範例,以及一支測試脚手架 shell 指令碼和一個 JavaScript 測試範本。產出是為提升可維護性、可讀性與效能而不改變功能的指引與配套工件。
適用情境
適用於現代化過時的程式碼模式或已棄用的 API、減少技術債、提升可讀性與可維護性、從單體程式碼中抽出可重用元件,或為新功能開發做準備。它適合必須保留現有行為、且需要在變更前建立測試的情境。
執行需求
需要能讀取隨附參考指南並執行隨附指令碼(scripts/scaffold-tests.sh 與 templates/test-template.js)的代理環境。快速開始部分提到 tree、npm outdated 或 pip list --outdated 以及 linter 或 SonarQube 等外部工具,但此技能本身不需要憑證或網路存取。

Refactor Legacy Code

Table of Contents

Overview

This skill helps you systematically refactor legacy code to improve maintainability, readability, and performance while preserving existing functionality. It follows industry best practices for safe refactoring with comprehensive testing.

When to Use

  • Modernizing outdated code patterns or deprecated APIs
  • Reducing technical debt in existing codebases
  • Improving code readability and maintainability
  • Extracting reusable components from monolithic code
  • Upgrading to newer language features or frameworks
  • Preparing code for new feature development

Quick Start

First, analyze the legacy code to understand:

bash
# Review the codebase structuretree -L 3 -I 'node_modules|dist|build'
# Check for outdated dependenciesnpm outdated  # or pip list --outdated, composer outdated, etc.
# Identify code complexity hotspots# Use tools like:# - SonarQube for code smells# - eslint for JavaScript# - pylint for Python# - RuboCop for Ruby

Reference Guides

Detailed implementations in the references/ directory:

GuideContents
Code Assessment [blocked]Code Assessment
Establish Safety Net [blocked]Establish Safety Net
Incremental Refactoring [blocked]Incremental Refactoring
Modernize Patterns [blocked]Modernize Patterns
Reduce Dependencies [blocked]Reduce Dependencies, Documentation
Complete Refactoring Example [blocked]Complete Refactoring Example
Benefits Achieved [blocked]Benefits Achieved

Best Practices

✅ DO

  • Refactor incrementally: Small, testable changes
  • Run tests frequently: After each refactoring step
  • Commit often: Create logical, atomic commits
  • Keep existing tests passing: Don't break functionality
  • Use IDE refactoring tools: Safer than manual edits
  • Review code coverage: Ensure tests cover refactored code
  • Document decisions: Why, not just what
  • Seek peer review: Fresh eyes catch issues

❌ DON'T

  • Mix refactoring with new features: Separate concerns
  • Refactor without tests: Recipe for breaking changes
  • Change behavior: Refactoring should preserve functionality
  • Refactor large chunks: Increases risk and review difficulty
  • Ignore code smells: Address them systematically
  • Skip documentation: Future maintainers need context

來源與署名

來源:aj-geddes/useful-ai-prompts位於skills/refactor-legacy-code提交3f5182c

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架