agentleFS
ilyaevilyaev/glotti/.agents/skills/refactoring/SKILL.md

refactoring

Skill1 starsChanged 9 months agoMITView on GitHub

General code refactoring and optimization skill for TypeScript, React, and Node.js projects.

69 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use ci-cd, logging and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • JohnJBorenSkill

    ci-cd

    CI/CD pipeline design, GitHub Actions, testing automation, and deployment strategies

    Also in 5 projects that use this

  • diego-tobalinaSkill

    logging

    Logging best practices for Java (SLF4J/Logback) and Node.js (Winston/Pino). Use for structured logging, log levels, and avoiding sensitive data exposure.

    Also in 4 projects that use this

  • AWSSkill

    planning

    Discovers user intent and generates a structured, step-by-step plan for model customization workflows. This skill must always be activated alongside any other skill when the user's request relates to model customization — including fine-tuning, training, building, customizing, reviewing data, or getting advice on approach, regardless of domain. Do not skip this skill even if the immediate ask is narrow (e.g., reviewing data format or a single workflow step), because planning discovers the full scope of work needed. Also activate when the user wants to resume, continue, or modify an existing plan.

    Also in 8 projects that use this

  • dkprogSkill

    dependency-management

    Apply when wiring dependencies, integrating external services, or designing interfaces between modules. Ensures loose coupling through dependency injection, interface segregation, and proper abstraction boundaries.

    Also in 3 projects that use this

  • javalenciacaiSkill

    multi-agent-orchestration

    Design and coordinate multi-agent systems where specialized agents work together to solve complex problems. Covers agent communication, task delegation, workflow orchestration, and result aggregation. Use when building coordinated agent teams, complex workflows, or systems requiring specialized expertise across domains.

    Also in 3 projects that use this

  • harrissohail6Skill

    database-design

    Database schema design and SQL best practices. Use when designing tables, writing queries, or optimizing database performance.

    Also in 5 projects that use this

The file

Show its rules and text

We split this skill into the rules it gives an agent

17 rules. 0 are common, 13 are distinctive, and 4 are very specific to this project.

  • 0

    Common

    Most of the 61 edited versions we read keep these.

  • 13

    Distinctive

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Before modifying any code, the AI MUST analyze the target file(s) using this framework:

    line 13
  2. Distinctive:

    Regex Check: Ensure regex patterns are correctly escaped for their context (constructor vs literal).

    line 20
  3. Distinctive:

    When a file mixes many concerns, apply this extraction order:

    line 26
  4. Distinctive:

    Prefer constructor injection for classes and parameter injection for functions.

    line 39
  5. Distinctive:

    Validation: Use schemas for all request data (query, params, body).

    line 49
  6. Distinctive:

    Error Handling: Use standardized error formats and centralized error handlers.

    line 54
  7. Distinctive:

    When optimizing UI components:

    line 58
  8. Very specific:

    Memoization: Use useMemo for expensive calculations and useCallback for stable event handlers.

    line 60
  9. Distinctive:

    Read the code and analyze it against the Analysis Framework (Section 1).

    line 69
  10. Very specific:

    Create a refactoring log (e.g., docs/refactoring/[filename]_log.md).

    line 70
  11. Distinctive:

    Document findings, violations, and a step-by-step refactoring plan with a checklist.

    line 71
  12. Very specific:

    Backup/Safety: Ensure you have a way to roll back.

    line 73
  13. Distinctive:

    Follow the extraction order (constants -> pure functions -> state -> etc.).

    line 77
  14. Distinctive:

    After completing a step, update the refactoring log.

    line 81
  15. Very specific:

    Check for type errors (tsc).

    line 85
  16. Distinctive:

    Ensure tests pass (if available).

    line 86
  17. Distinctive:

    Verify the logic remains consistent with the original implementation.

    line 87

“Common” is measured: kept by at least 6 in 10 of the 61 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Code Refactoring & Optimization Skill

This skill guides the AI to refactor and optimize code, ensuring improvements align with modern architecture, patterns, and quality standards for TypeScript-based stacks.

1. Analysis Framework

Before modifying any code, the AI MUST analyze the target file(s) using this framework:

  • Responsibility Check: Does this file/function do too much? (e.g., mixing data fetching, business logic, and UI rendering). Files with excessive responsibilities or too many mutable closure variables are "god objects" — split them.
  • Dependency Check: Are there circular dependencies or cross-boundary imports? Are there duplicate instantiations of core services or stores?
  • Security Check: Are inputs validated (e.g., using Zod)? Do string validations explicitly use .trim() and .max(limit) to prevent DoS attacks? Are objects .strict() to prevent prototype pollution or JSON bloat? Are outputs sanitized?
  • Performance Check: Are expensive operations cached? Are unrelated computations blocking the event loop? Watch for O(n²) patterns in data processing.
  • Type Safety: Are there any types or loose casts (as string) that can be made strict? Replace loose state variables with a typed state object.
  • Regex Check: Ensure regex patterns are correctly escaped for their context (constructor vs literal).

2. Standard Refactoring Patterns

A. "God File" Decomposition

When a file mixes many concerns, apply this extraction order:

  1. Constants first: Extract magic numbers and static configurations to a constants.ts or config.ts file.
  2. Pure functions next: Extract stateless logic to its own utility module. These are easiest to test and reuse.
  3. Stateful classes/logic: Extract logic that manages internal state into classes or dedicated state handlers. Pass dependencies via constructor or factory functions.
  4. Typed state object: Replace loose variables with a typed interface + factory function. This makes state explicit and easier to debug.
  5. Protocol/serialization layer: Extract message parsing and serialization into a dedicated module if handling network traffic or complex data formats.
  6. Edge/Integration module: Extract external API connections or third-party integrations into a bridge module.
  7. Feature-specific modules: Extract complex feature logic into standalone modules.
  8. Slim orchestrator: The original file should become a thin orchestrator that wires modules together and handles lifecycle.

B. Dependency Injection (DI)

Prefer constructor injection for classes and parameter injection for functions. Avoid importing singletons or global state directly into the middle of business logic modules.

  • Useful for testing: allows mocking dependencies.
  • Clearer contracts: all dependencies are visible at the entry point.

C. API/Server Route Refactoring

When refactoring routes:

  1. Dependency Injection: Inject services or stores instead of importing them directly.
  2. Validation: Use schemas for all request data (query, params, body).
    • CRITICAL: String validations must include .trim() and .max(N) to prevent memory exhaustion.
    • CRITICAL: Object schemas should use .strict().
  3. Service Extraction: Move complex business logic to a service layer.
  4. Middleware: Extract cross-cutting concerns (auth, logging) to middleware.
  5. Error Handling: Use standardized error formats and centralized error handlers.

D. Client-Side Optimization (React)

When optimizing UI components:

  1. Memoization: Use useMemo for expensive calculations and useCallback for stable event handlers.
  2. Splitting: Break large components into smaller, single-purpose sub-components.
  3. Hooks: Extract logic into custom hooks to keep view components clean and focused.

3. Execution Workflow

To apply this skill:

  1. Analyze & Plan:
    • Read the code and analyze it against the Analysis Framework (Section 1).
    • Create a refactoring log (e.g., docs/refactoring/[filename]_log.md).
    • Document findings, violations, and a step-by-step refactoring plan with a checklist.
  2. Backup/Safety: Ensure you have a way to roll back. Using git or creating a -legacy variant is recommended for significant manual refactors.
  3. Refactor (Iterative):
    • Execute the next step(s) from the plan.
    • Follow the extraction order (constants -> pure functions -> state -> etc.).
    • Apply changes incrementally.
  4. Update Log:
    • After completing a step, update the refactoring log.
    • Mark completed items and note any deviations.
  5. Verify:
    • Check for type errors (tsc).
    • Ensure tests pass (if available).
    • Verify the logic remains consistent with the original implementation.

4. Common Technical Pitfalls

Bug PatternExampleFix
Regex double-escaping[^\\w\\s] in regex literalUse [^\w\s]
Duplicate singletonsMultiple files instantiating the same storeInitialize once at entry point, inject elsewhere
Stale closure stateAccessing local variables after async gapsUse a unified state object or refs
Unguarded network callsSending to closed connections/socketsAlways check connection state before sending
Missing input limitsAccepting unbound strings in APIAlways use .max() and .trim() in validations

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.