Skip to content

Commit 230c081

Browse files
riqueufmgCopilotaaronpowell
authored
Create java-refactoring-extract-method.prompt.md (#284)
* Create java-refactoring-extract-method.prompt.md * Update prompts/java-refactoring-extract-method.prompt.md Co-authored-by: Copilot <[email protected]> * Update README.prompts.md * Update java-refactoring-extract-method.prompt.md * Update java-refactoring-extract-method.prompt.md * Update java-refactoring-extract-method.prompt.md * Fixing readme --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Aaron Powell <[email protected]>
1 parent aa38e38 commit 230c081

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

README.prompts.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Ready-to-use prompt templates for specific development scenarios and tasks, defi
8282
| [Project Folder Structure Blueprint Generator](prompts/folder-structure-blueprint-generator.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Ffolder-structure-blueprint-generator.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Ffolder-structure-blueprint-generator.prompt.md) | Comprehensive technology-agnostic prompt for analyzing and documenting project folder structures. Auto-detects project types (.NET, Java, React, Angular, Python, Node.js, Flutter), generates detailed blueprints with visualization options, naming conventions, file placement patterns, and extension templates for maintaining consistent code organization across diverse technology stacks. |
8383
| [Project Workflow Documentation Generator](prompts/project-workflow-analysis-blueprint-generator.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fproject-workflow-analysis-blueprint-generator.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fproject-workflow-analysis-blueprint-generator.prompt.md) | Comprehensive technology-agnostic prompt generator for documenting end-to-end application workflows. Automatically detects project architecture patterns, technology stacks, and data flow patterns to generate detailed implementation blueprints covering entry points, service layers, data access, error handling, and testing approaches across multiple technologies including .NET, Java/Spring, React, and microservices architectures. |
8484
| [README Generator Prompt](prompts/readme-blueprint-generator.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Freadme-blueprint-generator.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Freadme-blueprint-generator.prompt.md) | Intelligent README.md generation prompt that analyzes project documentation structure and creates comprehensive repository documentation. Scans .github/copilot directory files and copilot-instructions.md to extract project information, technology stack, architecture, development workflow, coding standards, and testing approaches while generating well-structured markdown documentation with proper formatting, cross-references, and developer-focused content. |
85+
| [Refactoring Java Methods with Extract Method](prompts/java-refactoring-extract-method.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fjava-refactoring-extract-method.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fjava-refactoring-extract-method.prompt.md) | Refactoring using Extract Methods in Java Language |
8586
| [Repo Story Time](prompts/repo-story-time.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Frepo-story-time.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Frepo-story-time.prompt.md) | Generate a comprehensive repository summary and narrative story from commit history |
8687
| [Review And Refactor](prompts/review-and-refactor.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Freview-and-refactor.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Freview-and-refactor.prompt.md) | Review and refactor code in your project according to defined instructions |
8788
| [Spring Boot Best Practices](prompts/java-springboot.prompt.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fjava-springboot.prompt.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/prompt?url=vscode-insiders%3Achat-prompt%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Fprompts%2Fjava-springboot.prompt.md) | Get best practices for developing applications with Spring Boot. |
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)