home / skills / baz-scm / awesome-reviewers / code-refactoring

code-refactoring skill

/_skills/code-refactoring

This skill helps you simplify and restructure code by breaking large functions into focused helpers and removing redundancy for better maintainability.

npx playbooks add skill baz-scm/awesome-reviewers --skill code-refactoring

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.6 KB
---
name: code-refactoring
description: The practice of restructuring and simplifying code continuously – reducing complexity, improving design, and keeping codebases clean.
version: '1.0'
---
# Code Refactoring & Simplicity

Great developers continually refactor code to make it simpler and more efficient. Over time, software accumulates complexity; refactoring is the skill of untangling that complexity. By breaking down large functions and eliminating unnecessary logic, you improve readability and reduce technical debt. Simple designs are easier to test and evolve.

## Examples
- Splitting a 300-line function that does many things into smaller helper functions each focused on one task.
- Removing duplicate code by refactoring it into a reusable module or library.

## Guidelines
- **Decompose Large Functions:** If a function is doing too much or exceeds roughly 50 lines, split it into smaller, focused functions. Each function should ideally handle one responsibility. This makes the code easier to understand and test.
- **Simplify Complex Logic:** Reduce nesting and complexity in control flow. Apply the “exit early” principle to handle edge cases upfront and avoid deep nested `if`/`else` blocks. For example, return early on error conditions instead of wrapping the main logic in an else-clause.
- **Eliminate Redundancy:** Refactor to remove duplicate or convoluted code. Break down complex boolean expressions or chained operations into simpler steps. Simplifying tricky code by using clearer constructs or standard library functions makes it more approachable and reduces potential bugs.

Overview

This skill helps developers and AI agents continuously refactor code to reduce complexity, improve design, and keep codebases clean. It focuses on breaking down large functions, removing redundancy, and simplifying control flow so code is easier to read, test, and evolve. The goal is measurable improvement in maintainability and reduced technical debt over time.

How this skill works

The skill inspects code for long functions, duplicated logic, and complex conditionals, then recommends concrete refactorings such as extraction into focused helper functions, consolidation of repeated code, and early-exit patterns. It suggests minimal, testable changes and provides rationale tied to readability, testability, and risk. Outputs include clear refactor steps, sample code snippets, and guidance for verifying behavior with tests.

When to use it

  • When a function grows beyond ~50 lines or handles multiple responsibilities
  • During code review to identify opportunities for simplifying logic
  • Before adding features to reduce the chance of introducing bugs
  • When you observe duplicated code or repeated patterns across modules
  • When complex conditional nesting makes behavior hard to follow

Best practices

  • Decompose large functions into single-responsibility helpers and keep units small
  • Apply exit-early checks for edge cases to avoid deep nesting
  • Replace repeated code with shared functions or small modules
  • Break complex boolean expressions into named intermediate variables
  • Make incremental refactors that preserve behavior and are covered by tests

Example use cases

  • Split a 300-line handler into smaller functions, each with a focused unit test
  • Refactor duplicated validation logic into a shared library used across services
  • Simplify nested if/else chains using early returns and clearer guards
  • Introduce a small adapter to replace convoluted chaining of transformations
  • Turn a complex one-off script into composable functions for reuse and testing

FAQ

Will refactoring change program behavior?

Refactoring aims to preserve behavior. Always run tests or add tests before refactoring to ensure behavior remains unchanged.

How large is too large for a function?

Functions over ~50 lines or those doing more than one logical task are good candidates for decomposition, though context matters.