New to Claude Skills? Learn how to install them →

Jgithub on GitHub

Java Refactoring Extract Method

OfficialFree

Improve Java method readability and maintainability.

by github37.7k stars on github/awesome-copilot
5 views
Updated Aug 10, 2026
Get this skill

Free · Opens the source repo

What Java Refactoring Extract Method does

The Java Refactoring Extract Method skill is designed for developers looking to enhance their Java code quality through the Extract Method refactoring technique. This skill provides clear examples of how to refactor complex methods into smaller, more manageable ones, thereby improving readability, testability, and maintainability. By breaking down lengthy methods into well-defined, purpose-driven methods, developers can create code that is easier to understand and modify.

The skill includes specific guidelines for identifying methods that exceed certain thresholds in terms of lines of code, number of statements, and cyclomatic complexity. This structured approach helps developers pinpoint which methods require refactoring. Once identified, the skill guides users through the process of extracting code blocks into new methods, ensuring that the functionality remains intact while enhancing modularity and reducing coupling.

This skill is particularly useful for Java developers who want to adhere to best practices in software development. By applying the Extract Method technique, developers can create cleaner code that is not only easier to read but also simpler to test. This can lead to fewer bugs and a more efficient development process overall. The skill is also beneficial for teams working on large codebases, where maintaining clarity and organization is crucial.

Ultimately, the Java Refactoring Extract Method skill serves as a practical tool for improving Java code quality, making it an essential addition for any developer looking to refine their coding practices and enhance their software development workflow.

When to use it

Use this skill when you encounter Java methods that are overly complex or lengthy, making them hard to read and maintain.

When not to use it

This skill may not be suitable for very simple methods that do not exceed the complexity thresholds, as refactoring might introduce unnecessary overhead.

What you can build with it

Refactoring a Complex Method

You have a Java method that exceeds 20 lines of code and is difficult to understand. Use this skill to break it down into smaller, clearer methods.

Improving Code Testability

Your existing Java code is hard to test due to its complexity. Apply the Extract Method technique to create methods that are easier to test in isolation.

Enhancing Code Readability

Team members struggle to comprehend a lengthy Java method. Utilize this skill to refactor it, making it more readable and maintainable for the team.

How to install Java Refactoring Extract Method

View source

1. Install with the skills CLI

npx skills add github/awesome-copilot/java-refactoring-extract-method --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by github

Refactoring Java Methods with Extract Method

Role

You are an expert in refactoring Java methods.

Below are 2 examples (with titles code before and code after refactoring) that represents Extract Method.

Code Before Refactoring 1:

public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerId) {
	assertNotBuild();
	if (bpartnerId > 0) {
		setC_BPartner_ID(bpartnerId);
	}
	return this;
}

Code After Refactoring 1:

public FactLineBuilder bpartnerIdIfNotNull(final BPartnerId bpartnerId) {
	if (bpartnerId != null) {
		return bpartnerId(bpartnerId);
	} else {
		return this;
	}
}
public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerRepoId) {
	return bpartnerIdIfNotNull(BPartnerId.ofRepoIdOrNull(bpartnerRepoId));
}

Code Before Refactoring 2:

public DefaultExpander add(RelationshipType type, Direction direction) {
     Direction existingDirection = directions.get(type.name());
     final RelationshipType[] newTypes;
     if (existingDirection != null) {
          if (existingDirection == direction) {
               return this;
          }
          newTypes = types;
     } else {
          newTypes = new RelationshipType[types.length + 1];
          System.arraycopy(types, 0, newTypes, 0, types.length);
          newTypes[types.length] = type;
     }
     Map<String, Direction> newDirections = new HashMap<String, Direction>(directions);
     newDirections.put(type.name(), direction);
     return new DefaultExpander(newTypes, newDirections);
}

Code After Refactoring 2:

public DefaultExpander add(RelationshipType type, Direction direction) {
     Direction existingDirection = directions.get(type.name());
     final RelationshipType[] newTypes;
     if (existingDirection != null) {
          if (existingDirection == direction) {
               return this;
          }
          newTypes = types;
     } else {
          newTypes = new RelationshipType[types.length + 1];
          System.arraycopy(types, 0, newTypes, 0, types.length);
          newTypes[types.length] = type;
     }
     Map<String, Direction> newDirections = new HashMap<String, Direction>(directions);
     newDirections.put(type.name(), direction);
     return (DefaultExpander) newExpander(newTypes, newDirections);
}
protected RelationshipExpander newExpander(RelationshipType[] types,
          Map<String, Direction> directions) {
     return new DefaultExpander(types, directions);
}

Task

Apply Extract Method to improve readability, testability, maintainability, reusability, modularity, cohesion, low coupling, and consistency.

Always return a complete and compilable method (Java 17).

Perform intermediate steps internally:

  • First, analyze each method and identify those exceeding thresholds:
    • LOC (Lines of Code) > 15
    • NOM (Number of Statements) > 10
    • CC (Cyclomatic Complexity) > 10
  • For each qualifying method, identify code blocks that can be extracted into separate methods.
  • Extract at least one new method with a descriptive name.
  • Output only the refactored code inside a single java block.
  • Do not remove any functionality from the original method.
  • Include a one-line comment above each new method describing its purpose.

Code to be Refactored:

Now, assess all methods with high complexity and refactor them using Extract Method

Frequently asked questions about Java Refactoring Extract Method

Similar skills