refactoring
SKILL.md
Refactoring Skill
This skill helps identify code smells and apply refactoring techniques to improve code quality. The foundational principles are from Refactoring.guru, adapted for Chrome Extension projects using Vanilla JS, Modules, and Side Panel architecture.
Core Principles
- Clean Code: Easy to read, understand, and maintain.
- Dirty Code: Full of "smells", difficult to maintain and extend.
- Baby Steps: Make one small change at a time, ensuring tests pass.
Process
- Identify Smells: Observe the code and find characteristics matching "Code Smells".
- Select Technique: Choose an appropriate refactoring technique based on the smell type.
- Refactor: Modify the code.
- Verify: Ensure functionality is not affected (Manual Test / Unit Test).
Common Code Smells and Solutions
1. Bloaters
Code, methods, or classes that have grown too large to manage.
-
Long Method
- Symptoms: A function exceeds 30-50 lines, or contains multiple levels of nesting.
- Solution: Extract Method. Move partial logic to a new function.
- Project Context: Large event listeners in
sidepanel.js, orrender()functions.
-
Large Class / Module
- Symptoms: A file (e.g.,
modules/uiManager.js) takes on too many responsibilities. - Solution: Extract Class/Module. Split by functionality.
- Symptoms: A file (e.g.,
-
Long Parameter List
- Symptoms: A function receives more than 3-4 parameters.
- Solution: Introduce Parameter Object. Pass an Object configuration.
2. Object-Orientation Abusers
Incorrect application of OO principles.
- Switch Statements
- Symptoms: Complex
switchorif-elsechains used to determine types and execute different logic. - Solution: Replace Conditional with Polymorphism. In Vanilla JS, use Object Map or Strategy Pattern.
- Symptoms: Complex
3. Change Preventers
Changing one place requires changing multiple places.
- Shotgun Surgery
- Symptoms: Every small feature addition requires modifying 5-6 files.
- Solution: Move Method/Field. Consolidate related logic into a single module.
4. Dispensables
Useless or redundant code.
- Comments
- Symptoms: Using comments to explain "what the code does" (instead of "why it does it").
- Solution: Rename Method/Variable. Let the code be self-explanatory.
- Duplicate Code
- Symptoms: Same logic appears in two or more places.
- Solution: Extract Method and share.
- Project Context: Create element logic in multiple Renderers -> Unify using
modules/ui/elements.jsor utility functions.
Common Refactoring Techniques
Composing Methods
- Extract Method: Select a code segment -> Create new function -> Name it -> Replace original with function call.
- Inline Method: When the function body is clearer than its name, inline it back to the call site.
- Replace Temp with Query: Replace temporary variables with function calls to reduce local variable interference.
Organizing Data
- Encapsulate Field: (Less enforced in JS) Use getter/setter to encapsulate variable access.
- Replace Magic Number with Symbolic Constant: Replace
86400withSECONDS_IN_DAY.
Simplifying Conditional Expressions
- Decompose Conditional: Extract complex
if (a && b || c)intoif (isSpecialCase()). - Consolidate Conditional Expression: Merge conditional checks that have the same result.
- Replace Nested Conditional with Guard Clauses: Use Guard Clauses to return early, reducing nesting.
Project-Specific Refactoring Guidelines (Arc-Like Chrome Extension)
-
Separate DOM Operations:
- Avoid direct DOM manipulation in business logic (
modules/stateManager.js). - Ensure all DOM generation is in
*Renderer.jsmodules. - Ensure all DOM queries are in
modules/ui/elements.js(if possible).
- Avoid direct DOM manipulation in business logic (
-
State Management:
- Avoid relying on
window.globalVar. UsestateManagermodule to hold cross-module state.
- Avoid relying on
-
Chrome API Encapsulation:
- Don't call
chrome.bookmarks.*directly in UI components. Usemodules/apiManager.jsfor future replacement or testing.
- Don't call
-
Style Consistency:
- Avoid using
element.style.color = 'red'. Use CSS classes (element.classList.add('error')) withindex.css.
- Avoid using
How to Use This Skill
When User requests "Refactor this file" or "Clean up this code":
- Check: Read the file and compare against the Code Smells list above.
- Plan: Propose a refactoring plan (e.g., "I will break this 100-line render function into 3 sub-functions").
- Execute: Perform the modifications.
- Verify: Ensure functionality remains consistent.
Weekly Installs
1
Repository
tai-ch0802/skills-bundleGitHub Stars
1
First Seen
Mar 1, 2026
Security Audits
Installed on
junie1
windsurf1
amp1
cline1
augment1
trae1