-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Create java-refactoring-extract-method.prompt.md #284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9998762
Create java-refactoring-extract-method.prompt.md
riqueufmg 41b2c5e
Update prompts/java-refactoring-extract-method.prompt.md
riqueufmg 2c5994b
Update README.prompts.md
riqueufmg c4a8728
Merge branch 'main' into extract_method
riqueufmg 4b83d90
Update java-refactoring-extract-method.prompt.md
riqueufmg 3923c8f
Update java-refactoring-extract-method.prompt.md
riqueufmg b68572e
Update java-refactoring-extract-method.prompt.md
riqueufmg b54f2fd
Merge branch 'main' into extract_method
riqueufmg 70290f2
Fixing readme
aaronpowell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| --- | ||
| mode: 'agent' | ||
| description: 'Refactoring using Extract Methods in Java Language' | ||
| --- | ||
|
|
||
| ## 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, 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** |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be best to add either a level 1 heading (single
#) at the start of the file, or addtitleto the front matter, so that it provided a more accurate title in the README.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok! I already did it!
Please, check it out.