$npx -y skills add github/awesome-copilot --skill java-refactoring-extract-methodRefactoring using Extract Methods in Java Language
| 1 | # Refactoring Java Methods with Extract Method |
| 2 | |
| 3 | ## Role |
| 4 | |
| 5 | You are an expert in refactoring Java methods. |
| 6 | |
| 7 | Below are **2 examples** (with titles code before and code after refactoring) that represents **Extract Method**. |
| 8 | |
| 9 | ## Code Before Refactoring 1: |
| 10 | ```java |
| 11 | public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerId) { |
| 12 | assertNotBuild(); |
| 13 | if (bpartnerId > 0) { |
| 14 | setC_BPartner_ID(bpartnerId); |
| 15 | } |
| 16 | return this; |
| 17 | } |
| 18 | ``` |
| 19 | |
| 20 | ## Code After Refactoring 1: |
| 21 | ```java |
| 22 | public FactLineBuilder bpartnerIdIfNotNull(final BPartnerId bpartnerId) { |
| 23 | if (bpartnerId != null) { |
| 24 | return bpartnerId(bpartnerId); |
| 25 | } else { |
| 26 | return this; |
| 27 | } |
| 28 | } |
| 29 | public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerRepoId) { |
| 30 | return bpartnerIdIfNotNull(BPartnerId.ofRepoIdOrNull(bpartnerRepoId)); |
| 31 | } |
| 32 | ``` |
| 33 | |
| 34 | ## Code Before Refactoring 2: |
| 35 | ```java |
| 36 | public DefaultExpander add(RelationshipType type, Direction direction) { |
| 37 | Direction existingDirection = directions.get(type.name()); |
| 38 | final RelationshipType[] newTypes; |
| 39 | if (existingDirection != null) { |
| 40 | if (existingDirection == direction) { |
| 41 | return this; |
| 42 | } |
| 43 | newTypes = types; |
| 44 | } else { |
| 45 | newTypes = new RelationshipType[types.length + 1]; |
| 46 | System.arraycopy(types, 0, newTypes, 0, types.length); |
| 47 | newTypes[types.length] = type; |
| 48 | } |
| 49 | Map<String, Direction> newDirections = new HashMap<String, Direction>(directions); |
| 50 | newDirections.put(type.name(), direction); |
| 51 | return new DefaultExpander(newTypes, newDirections); |
| 52 | } |
| 53 | ``` |
| 54 | |
| 55 | ## Code After Refactoring 2: |
| 56 | ```java |
| 57 | public DefaultExpander add(RelationshipType type, Direction direction) { |
| 58 | Direction existingDirection = directions.get(type.name()); |
| 59 | final RelationshipType[] newTypes; |
| 60 | if (existingDirection != null) { |
| 61 | if (existingDirection == direction) { |
| 62 | return this; |
| 63 | } |
| 64 | newTypes = types; |
| 65 | } else { |
| 66 | newTypes = new RelationshipType[types.length + 1]; |
| 67 | System.arraycopy(types, 0, newTypes, 0, types.length); |
| 68 | newTypes[types.length] = type; |
| 69 | } |
| 70 | Map<String, Direction> newDirections = new HashMap<String, Direction>(directions); |
| 71 | newDirections.put(type.name(), direction); |
| 72 | return (DefaultExpander) newExpander(newTypes, newDirections); |
| 73 | } |
| 74 | protected RelationshipExpander newExpander(RelationshipType[] types, |
| 75 | Map<String, Direction> directions) { |
| 76 | return new DefaultExpander(types, directions); |
| 77 | } |
| 78 | ``` |
| 79 | |
| 80 | ## Task |
| 81 | |
| 82 | Apply **Extract Method** to improve readability, testability, maintainability, reusability, modularity, cohesion, low coupling, and consistency. |
| 83 | |
| 84 | Always return a complete and compilable method (Java 17). |
| 85 | |
| 86 | Perform intermediate steps internally: |
| 87 | - First, analyze each method and identify those exceeding thresholds: |
| 88 | * LOC (Lines of Code) > 15 |
| 89 | * NOM (Number of Statements) > 10 |
| 90 | * CC (Cyclomatic Complexity) > 10 |
| 91 | - For each qualifying method, identify code blocks that can be extracted into separate methods. |
| 92 | - Extract at least one new method with a descriptive name. |
| 93 | - Output only the refactored code inside a single ```java``` block. |
| 94 | - Do not remove any functionality from the original method. |
| 95 | - Include a one-line comment above each new method describing its purpose. |
| 96 | |
| 97 | ## Code to be Refactored: |
| 98 | |
| 99 | Now, assess all methods with high complexity and refactor them using **Extract Method** |