You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@netbeans.apache.org by Scott Wierschem <sc...@potentialpower.com> on 2019/10/09 04:24:12 UTC
Refactoring Kata notes
I discussed this idea with Geertjan after Oracle Code One and he suggested
I share it here. There is a refactoring Kata that a friend of mine put
together that works in IntelliJ and Eclipse, but NetBeans has some missing
functionality that keeps it from working there. I made notes of the
differences working through it in IntelliJ and NetBeans and am submitting
my results here. (I tried to rework them using Markdown, but decided that
"shipping" is a more important feature than "pretty".) I'm happy to assist
anyone in going over this and with enhancing the refactoring functions in
NetBeans.
Thanks,
Scott Wierschem
--------------------------8<---------------------
Llewellyn Falco has put together a nice kata for helping developers to
memorize the "Inline" refactoring keysequence. It can be found at his "Learn
with LLew" Github site here:
https://github.com/LearnWithLlew/KeyboardShortcutKatas.java
I worked through the Inline.java file at
https://github.com/LearnWithLlew/KeyboardShortcutKatas.java/tree/master/src/main/java/learnwithllew/KeyboardShortcutKatas
using IntelliJ, and made notes of what happened when I tried to do the same
things in NetBeans:
(Starred option is the one selected)
inline a at use point (line 13)
Options
* inline all references and remove the variable (1 occurrence)
inline this reference only and keep the variable
inline b at use point (line 12)
Options
Method b() has one occurrence
* Inline all and remove the method
Inline all and keep the method
Inline this only and keep the method
Checkboxes
Search in comments and strings
Search for text occurrences
inline i at use point (line 12)
Options
* inline all references and remove the variable (2 occurrence)
inline this reference only and keep the variable
inline s at definition point (line 10)
(automatically inlines and removes varible at all locations)
inline j at definition point (line 10)
(automatically inlines and removes varible at all locations)
inline e at definition point (line 7)
Options
Field b has one occurrence
* Inline all and remove the field
Inline all and keep the field
Inline this only and keep the field
Checkboxes
Search in comments and strings
Search for text occurrences
inline f at use point (line 9)
Options
Method f(int) has one occurrence
* Inline all and remove the method
Inline all and keep the method
Inline this only and keep the method
Checkboxes
Search in comments and strings
Search for text occurrences
inline m at definition (line 9)
(automatically inlines and removes varible at all locations)
inline t at definition (line 9)
error displayed: Cannot perform refactoring
Another variable 't' definition is used together with
inlined one
inline n at definition (line 21)
Options
Method n(int) has one occurrence
* Inline all and remove the method
Inline all and keep the method
Inline this only and keep the method
Checkboxes
Search in comments and strings
Search for text occurrences
(line 10 now says "if (false)")
Alt+Enter on useless "if" statement
Context menu options
Remove 'if' statement
---------------------
Invert 'if' condition
Remove braces from 'if' statement
(NOTE: NetBeans flags this as a dead branch, but the Alt+Enter menu just
suggests inverting the "if" statement)
select "Remove 'if' statement'
Complete statement is removed
(line 11 has a calculation with all literal numeric values)
select a number and type Alt+Enter
Context menu options
Simplify
----------
Add clarifying parentheses
Convert number to...
Extract method reference
Replace '-3 + 3 + (5 - 5 + 2)' with constant value '2'
Split into declaration and assignment
(NOTE: NetBeans suggests various numeric manipulations, but no
simplifying of the calculation)
select "Simplify"
Calculation is replaced with '2'
inline l at definition point (line 11)
(automatically inlines and removes varible at all locations)
inline t at definition point (line 9)
(automatically inlines and removes varible at all locations)
inline q at use point (line 13)
Options
Local variable 'q'
* inline all references and remove the variable (1 occurrence)
inline this reference only and keep the variable
(NOTE: NetBeans warns that this could change the program functionality.
Perhaps we should add parentheses in a situation like this?)
inline c at use point (line 10)
Options
Method c(int) has one occurrence
* Inline all and remove the method
Inline all and keep the method
Inline this only and keep the method
Checkboxes
Search in comments and strings
Search for text occurrences
inline k at definition point (line 9)
(lambda is restructured as follows:
return 42 + ((Supplier<Integer>) () -> {
return 2 - 2;
}).get() + 1 - O.create().p + Extensions.h(7) + 2 - 2 + 0;
(NOTE: NetBeans restructures this without the "(Supplier<Integer>)" cast
which makes it report an error condition. Adding the cast corrects the
error.)
inline h at definition point (line 16)
Options
Method h(int) has one occurrence
* Inline all and remove the method
Inline all and keep the method
Inline this only and keep the method (NOTE: this line is
grayed out)
Checkboxes
Search in comments and strings
Search for text occurrences
Select "Extensions" class name (line 14) and hit Alt+Enter
Context menu
Safe delete 'learnWithLlew.keyboardShortcutKatas.Extensions'
Change access modifier
Create subclass
Add Javadoc
(NOTE: NetBeans doesn't suggest deleting the unused empty class)
Select "Safe delete..."
Dialog box
Search for usages and delete class "Extensions"?
Checkboxes
Search in comments and strings
Search for text occurrences (force checked)
Buttons: OK and Cancel
Click "OK" - class is removed
inline p at definition (line 16)
Dialog box Options
Field p has one occurrence
* Inline all and remove the field
Inline all and keep the field
Inline this only and keep the field (grayed out)
Checkboxes
Search in comments and strings
Search for text occurrences
inline O at constructor definition (line 15)
error message: Class cannot be inlined because it has static methods
(NOTE: NetBeans just reports, "Cannot refactor this object")
click on "create()" method name and hit "Alt+Enter"
Context menu
Safe delete 'create()'
-----------------------
Change access modifier
Generate missed test methods
Insert '@Contract(value = '-> true', pure = true) @NotNull'
Add Javadoc
(NOTE: NetBeans does not suggest deleting the unused, empty method)
select "Safe delete"
Dialog box
Search for usages and delete method "create`"?
Checkboxes
Search in comments and strings
Search for text occurrences
Buttons: OK and Cancel
Click "OK" - class is removed
inline O
error message: "Class is never used"
(NOTE: NetBeans just reports, "Cannot refactor this object")
Select "O" and hit "Alt+Enter"
Context menu
Safe delete 'learnWithLlew.keyboardShortcutKatas.O'
Change access modifier
Create subclass
Add Javadoc
Select "Safe delete..."
Dialog box
Search for usages and delete class "O"?
Checkboxes
Search in comments and strings
Search for text occurrences (force checked)
Buttons: OK and Cancel
Click "OK" - class is removed
select "2" on line 10
hit "Alt+Enter"
Context menu options
Simplify
----------
Add clarifying parentheses
Convert number to...
Extract method reference
Replace '2 - 2' with constant value '2'
Split into declaration and assignment
(NOTE: NetBeans does not offer to simplify these literal calculations)
select "Simplify"
calculations change to "0"
"return" is grayed out
click on "get" (line 11)
hit "Alt+Enter"
Context menu
Replace method call on lambda with lambda body
-------------------------------
Introduce local variable
Add clarifying parentheses
Add method contract to 'get()'
(NOTE: NetBeans will unroll the lambda if you click on the arrow operator
and hit "Alt+Enter". Then it suggests replacing with an anonymous inner
class. If this is done, no other automated refactoring is possible.)
select "Replace..."
lambda is replaced with a set of numeric literal calculations
select "42" (line 9)
hit "Alt+Enter"
Context menu options
Simplify
----------
Introduce local variable
Add clarifying parentheses
Convert number to...
Replace '2 - 2' with constant value '2'
select "Replace..."
calculations change to "42"
All done!