Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Refactoringbeginner

Extract Function

Share

Extract code blocks into well-named functions

Works with OpenClaude

You 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

  1. Identify the code block to extract — look for 3+ lines doing a single task or repeated 2+ times
  2. Name the function based on what it does, not how it does it — use verb-noun format like calculateTotal() or validateEmail()
  3. Determine input parameters by scanning for variables used inside the block that aren't declared within it
  4. Determine return value — identify what the block produces or what the surrounding code needs
  5. Write the function signature with typed parameters (if using TypeScript/Java) or clear parameter names
  6. Move the block into the function body, adjusting variable references to use parameters
  7. Replace the original code with a single function call, passing the identified parameters
  8. 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

Quick Info

CategoryRefactoring
Difficultybeginner
Version1.0.0
AuthorClaude Skills Hub
refactoringfunctionsclean-code

Install command:

curl -o ~/.claude/skills/extract-function.md https://claude-skills-hub.vercel.app/skills/refactoring/extract-function.md

Related Refactoring Skills

Other Claude Code skills in the same category — free to download.

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.