| name | code-simplifier |
| description | Simplifies and refines code for clarity, consistency, and maintainability while preserving all functionality. Use when asked to "simplify code", "clean up code", "refactor for clarity", "improve readability", or review recently modified code for elegance. Focuses on project-specific best practices. |
| argument-hint | <file_path_or_description_of_recent_changes> |
Code Simplifier
Apply expert code simplification techniques. Prioritize readable, explicit code over overly compact solutions while following project-specific best practices.
Simplify the code at $ARGUMENTS. If no target is specified, identify and simplify code that has been recently modified or touched in the current session.
Core Principles
-
Preserve Functionality: Never change what the code does - only how it does it. All original features, outputs, and behaviors must remain intact.
-
Apply Project Standards: Follow project coding standards and match existing patterns.
-
Enhance Clarity: Simplify code structure by:
- Reducing unnecessary complexity and nesting
- Eliminating redundant code and abstractions
- Improving readability through clear variable and function names
- Consolidating related logic, and merging types, functions, or constants that overlap so the reader holds fewer distinct concepts in their head
- Removing unnecessary comments that describe obvious code
- Avoiding nested ternary operators - prefer switch statements or if/else chains for multiple conditions
- Removing derivable state - if a value can be computed from values already in scope, don't pass or store it separately
- Choosing clarity over brevity - explicit code is often better than overly compact code
-
Maintain Balance: Avoid over-simplification that could:
- Reduce code clarity or maintainability
- Create overly clever solutions that are hard to understand
- Combine too many concerns into single functions or components
- Remove helpful abstractions that improve code organization
- Prioritize "fewer lines" over readability (e.g., nested ternaries, dense one-liners)
- Make the code harder to debug or extend
-
Focus Scope: Only refine code that has been specified or recently modified, unless explicitly instructed to review a broader scope.
Refinement Process
- Read and understand the specified code
- Identify opportunities to improve elegance and consistency
- Apply project-specific best practices and coding standards
- Ensure all functionality remains unchanged
- Verify the refined code is simpler and more maintainable
Examples
Nested ternaries → early returns
const status = isLoading
? "loading"
: hasError
? "error"
: isComplete
? "complete"
: "idle";
function getStatus(isLoading: boolean, hasError: boolean, isComplete: boolean) {
if (isLoading) return "loading";
if (hasError) return "error";
if (isComplete) return "complete";
return "idle";
}
Overly compact → clear steps
const result = arr
.filter((x) => x > 0)
.map((x) => x * 2)
.reduce((a, b) => a + b, 0);
const positiveNumbers = arr.filter((x) => x > 0);
const doubled = positiveNumbers.map((x) => x * 2);
const sum = doubled.reduce((a, b) => a + b, 0);
Redundant abstraction → direct check
function isNotEmpty(arr: unknown[]) {
return arr.length > 0;
}
if (isNotEmpty(items)) { ... }
if (items.length > 0) { ... }
Derivable state → compute where needed
function render(content: string, baseline: string, isDirty: boolean) {
return isDirty ? `${content} *` : content;
}
render(content, baseline, content !== baseline);
function render(content: string, baseline: string) {
const isDirty = content !== baseline;
return isDirty ? `${content} *` : content;
}
render(content, baseline);