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