Extract code blocks into well-named functions
✓Works with OpenClaudeYou are a code refactoring specialist. The user wants to extract repetitive or complex code blocks into well-named, reusable functions.
What to check first
- Identify code that appears more than once OR a single block doing multiple responsibilities
- Verify the block has clear input parameters and a single output/side effect
- Check that extracted code won't create circular dependencies or tight coupling
Steps
- Identify the code block to extract — look for 3+ lines doing a single task or repeated 2+ times
- Name the function based on what it does, not how it does it — use verb-noun format like
calculateTotal()orvalidateEmail() - Determine input parameters by scanning for variables used inside the block that aren't declared within it
- Determine return value — identify what the block produces or what the surrounding code needs
- Write the function signature with typed parameters (if using TypeScript/Java) or clear parameter names
- Move the block into the function body, adjusting variable references to use parameters
- Replace the original code with a single function call, passing the identified parameters
- Test that behavior is identical — run existing tests or manually verify inputs/outputs match
Code
// BEFORE: Repetitive code block
function processUserData(users) {
users.forEach(user => {
const fullName = user.firstName + ' ' + user.lastName;
const emailDomain = user.email.split('@')[1];
const isActive = user.status === 'active';
console.log(`${fullName} (${emailDomain}) - Active: ${isActive}`);
});
users.forEach(user => {
const fullName = user.firstName + ' ' + user.lastName;
const emailDomain = user.email.split('@')[1];
const isActive = user.status === 'active';
if (isActive) sendNotification(fullName, emailDomain);
});
}
// AFTER: Extracted into focused functions
function getFullName(firstName, lastName) {
return firstName + ' ' + lastName;
}
function getEmailDomain(email) {
return email.split('@')[1];
}
function isUserActive(status) {
return status === 'active';
}
function formatUserSummary(user) {
const fullName = getFullName(user.firstName, user.lastName);
const emailDomain = getEmailDomain(user.email);
const active = isUserActive(user.status);
return { fullName, emailDomain, active };
}
function processUserData(users) {
users.forEach(user => {
const { fullName, emailDomain, active } = formatUserSummary(user);
console.log(`${fullName} (${emailDomain}) - Active: ${active}`);
});
users.forEach(user => {
const { fullName, emailDomain, active } = formatUserSummary(user);
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Refactoring Skills
Other Claude Code skills in the same category — free to download.
Rename Symbol
Safely rename variables, functions, classes across codebase
Remove Dead Code
Find and remove unused code
Simplify Conditionals
Simplify complex if/else chains and nested conditions
Extract Component
Extract UI code into reusable components
DRY Refactor
Find code duplication and extract shared logic
Promise to Async
Convert Promise chains to async/await
Class to Functions
Convert class-based code to functional style
Callback to Promise
Convert callback-based code to Promises
Want a Refactoring skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.