
Java Refactoring Extract Method
Apply the Extract Method refactor in Java with before-and-after patterns so long methods become smaller, named units your agent can change safely.
Overview
Java Refactoring Extract Method is an agent skill for the Ship phase that guides Extract Method refactoring in Java using concrete before-and-after examples.
Install
npx skills add https://github.com/github/awesome-copilot --skill java-refactoring-extract-methodWhat is this skill?
- Positions the agent as an expert Java refactoring partner focused on Extract Method
- Ships two labeled before/after examples (builder guard clauses and expander direction logic)
- Shows extracting shared logic into named helpers while preserving public API behavior
- Useful for ERP-style builders and collection/mutation code with nested conditionals
- Concrete diff-style guidance instead of generic “clean code” advice
- 2 worked Extract Method examples with titled code before and code after sections
Adoption & trust: 8.8k installs on skills.sh; 34.6k GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
Your Java method mixes guard clauses, duplication, and branching so every small change risks regressions.
Who is it for?
Solo builders cleaning up legacy or fast-written Java service code who want the agent to follow Extract Method examples rather than invent new structure.
Skip if: Non-Java stacks, greenfield classes with no smell yet, or broad refactors (rename package, change architecture) outside Extract Method.
When should I use this skill?
Refactoring using Extract Methods in Java Language.
What do I get? / Deliverables
You get smaller named methods with the same outward behavior, making the next fix or feature edit easier for you and your agent.
- Refactored Java source using Extract Method
- Preserved public behavior with clearer named helper methods
Recommended Skills
Journey fit
Canonical shelf is Ship → review because the skill is a targeted code-quality refactor ritual during implementation hardening, not greenfield architecture. Extract Method is a review-and-reshape pass on existing Java—reducing duplication and clarifying control flow before merge or release.
How it compares
Use instead of asking for a generic “refactor this class”—the skill locks the agent to Extract Method with worked Java diffs.
Common Questions / FAQ
Who is java-refactoring-extract-method for?
Solo and indie developers shipping Java APIs or services who need a focused Extract Method assist during code review or cleanup.
When should I use java-refactoring-extract-method?
During Ship review when a method has grown nested conditionals or duplicated blocks and you want a named helper extracted without changing behavior.
Is java-refactoring-extract-method safe to install?
It is refactoring guidance only—always run your test suite after extractions and review the Security Audits panel on this page before trusting third-party skill packs.
SKILL.md
READMESKILL.md - Java Refactoring Extract Method
# 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: ```java public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerId) { assertNotBuild(); if (bpartnerId > 0) { setC_BPartner_ID(bpartnerId); } return this; } ``` ## Code After Refactoring 1: ```java 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: ```java 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: ```java 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**