Analyze and simplify code by extracting reusable functions, removing redundancies, and improving overall code quality through refactoring techniques
I analyze code and automatically simplify it by:
Use this skill when you need to:
Code Analysis
Simplification Strategies
Refactoring Execution
Validation
/simplify --file src/utils/helper.ts
/simplify --dir src/components --recursive
/simplify --pattern "**/*.java" --dry-run
/simplify --aggressive --max-complexity 10
| Option | Description |
|--------|-------------|
| --file <path> | Simplify a single file |
| --dir <path> | Simplify all files in a directory |
| --recursive | Include subdirectories (use with --dir) |
| --pattern <glob> | Files matching glob pattern |
| --dry-run | Show changes without applying them |
| --aggressive | Apply more aggressive simplifications |
| --max-complexity <n> | Target complexity threshold |
| --extract-methods | Extract repeated code into methods |
| --remove-dead-code | Remove unreachable code and unused items |
| --fix-idioms | Apply language-specific idiomatic patterns |
Before:
// Duplicate code in multiple places
const calculateTax = (amount: number) => amount * 0.08;
const calculateShipping = (weight: number) => weight * 0.5;
const calculateDiscount = (price: number) => price * 0.1;
// Later in the file...
const calculateTax = (amount: number) => amount * 0.08;
const calculateShipping = (weight: number) => weight * 0.5;
const calculateDiscount = (price: number) => price * 0.1;
After:
const calculateTax = (amount: number) => amount * 0.08;
const calculateShipping = (weight: number) => weight * 0.5;
const calculateDiscount = (price: number) => price * 0.1;
// Used where needed
Before:
function processUser(user: User): Result {
if (user !== null) {
if (user.isActive) {
if (user.hasPermission) {
// Main logic
return performAction(user);
}
}
}
return defaultResult;
}
After:
function processUser(user: User): Result {
if (!user || !user.isActive || !user.hasPermission) {
return defaultResult;
}
return performAction(user);
}
Before:
const isValid = (status: string) => {
if (status === 'active' || status === 'pending') {
return true;
} else {
return false;
}
};
After:
const isValid = (status: string) => status === 'active' || status === 'pending';
Before:
const names: string[] = [];
for (let i = 0; i < users.length; i++) {
names.push(users[i].name);
}
After:
const names = users.map(user => user.name);
Before:
const city = user && user.address && user.address.city !== null
? user.address.city
: 'Unknown';
After:
const city = user?.address?.city ?? 'Unknown';
| Metric | Before | After | Target | |--------|--------|-------|--------| | Cyclomatic Complexity | 15 | 8 | ≤ 10 | | Lines of Code | 450 | 320 | - | | Code Duplication | 12% | 0% | 0% | | Nesting Depth | 5 | 2 | ≤ 3 |
calculateTotalWithTax() > process()# Code Simplification Report
## Changes Applied
### Extracted Methods
- `extractUserData()` from 3 locations
- `validateInput()` from 2 locations
### Removed Dead Code
- Unused function: `legacyProcess()`
- Unreachable branch in `handleEvent()`
### Simplified Conditionals
- Replaced nested ifs with guard clause (5 occurrences)
- Simplified boolean expressions (3 occurrences)
## Metrics Improvement
| Metric | Before | After | Change |
|--------|--------|-------|--------|
| LOC | 1,250 | 980 | -22% |
| Complexity | 18 avg | 11 avg | -39% |
| Functions | 45 | 52 | +16% |
## Files Modified
- src/utils/helper.ts (3 changes)
- src/services/user.ts (2 changes)
- src/components/form.tsx (1 change)
| Pattern | Simplification | |---------|----------------| | Duplicate code | Extract to function | | Nested conditionals | Guard clauses | | Long boolean expressions | Simplify with early returns | | Manual iteration | Array methods (map/filter/reduce) | | Null checks | Optional chaining | | Default values | Nullish coalescing | | Switch with 2 cases | Ternary operator | | Unused code | Remove entirely |
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer