Prompt Library ⚙️ Technical Refactor Code for Clarity and Maintainability
GPT-4o ⚙️ Technical Intermediate

Refactor Code for Clarity and Maintainability

Refactor a code block to improve readability, reduce complexity, and follow best practices — without changing any behavior.

👁 5 views ⎘ 0 copies ♥ 0 likes

The Prompt

# Refactor Code for Clarity and Maintainability

You are a senior software engineer who refactors code for long-term maintainability without changing behavior.

## Code to Refactor

Language: [LANGUAGE]
Team size and experience level: [TEAM_CONTEXT]
Original author's intent: [ORIGINAL_PURPOSE]

```
[PASTE_CODE_HERE]
```

## Refactoring Objectives

Improve the code across these dimensions while preserving all existing behavior:

**1. Naming**
Replace vague names with names that reveal intent. No single-letter variables except conventional loop counters.

**2. Function decomposition**
Break functions longer than 20–30 lines into single-purpose units. Each function should do one thing and do it well.

**3. Complexity reduction**
Simplify nested conditionals. Replace complex boolean logic with named predicates. Aim for a cyclomatic complexity under 5 per function.

**4. Remove duplication**
Identify and extract repeated patterns into reusable functions or constants.

**5. Improve comments**
Remove comments that explain what the code obviously does. Add comments only where the WHY is non-obvious.

## Output Format

1. Refactored code (complete, runnable)
2. Change summary: list of what was changed and why
3. Before/after comparison for the most impactful change
4. Any behavior that's tricky to verify didn't change — add a test for it

## Constraint

Do NOT add new features or error handling not present in the original code.

📝 Fill in the blanks

Replace these placeholders with your own content:

[LANGUAGE]
[TEAM_CONTEXT]
[ORIGINAL_PURPOSE]
[PASTE_CODE_HERE]

How to use this prompt

1
Copy the prompt

Click "Copy Prompt" above to copy the full prompt text to your clipboard.

2
Replace the placeholders

Swap out anything in [BRACKETS] with your specific details.

3
Paste into GPT-4o

Open your preferred AI assistant and paste the prompt to get started.