[Cuis-dev] Extract method and select category refactor

Matías Waisman matiasewaisman at gmail.com
Sat Dec 7 13:04:58 PST 2024


Hello everybody, happy Saturday!

While using the Extract Method refactoring, I often found myself wanting to
move a newly extracted method to a different category most times. It was
quite tedious to first extract the method and then manually create or
select the target category. Sometimes, I even forgot to move the method
altogether. I realized it would be very useful if the "Extract Method"
refactoring also allowed the user to specify the category for the extracted
method as part of the process.

The refactoring first prompts the user to name the method and its
parameters (as usual). Then, an additional prompt asks the user to specify
the target category for the new method. By default, it suggests the current
category where the extraction is taking place. If the specified category
does not already exist, it will be created, and the method will be placed
inside it.

Initially, I thought the best way to implement this feature was to enhance
the existing Extract Method refactoring by adding the category selection
functionality. However, after telling my idea to Hernán Wilkinson, he
pointed out that it might not be ideal to force the user to click an
additional button during the process. Following his advice, I implemented
this as a new refactoring option that coexists with the original Extract
Method refactoring.

The new refactoring is accessible via Cmd + L or from the usual refactoring
menu.

Since much of the functionality is shared with the existing
ExtractMethodApplier class, I made some adjustments. Specifically, I
modified requestRefactoringParameters and createExtractMethodNewMethodFor
to include the category as a parameter in the class
ExtractMethodAndSelectCategoryApplier.

I also reorganized ExtractMethodApplier to serve as a superclass, with two
subclasses: ExtractMethodInSameCategoryApplier and
ExtractMethodAndSelectCategoryApplier. Each subclass defines the specific
parameters to request from the user during the refactoring process.
References to the original ExtractMethodApplier were updated to use
ExtractMethodInSameCategoryApplier.

Regarding the naming of the classes, I had some doubts. An alternative
naming could rename ExtractMethodApplier (the superclass) to
TransferCodeApplier and revert ExtractMethodInSameCategoryApplier to simply
ExtractMethodApplier. I’m open to suggestions on this!

Any feedback or comments are appreciated, below is the change set.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20241207/6351449f/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ExtractMethodAndSelectCategory-mew.001.cs.st
Type: application/octet-stream
Size: 8785 bytes
Desc: not available
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20241207/6351449f/attachment.obj>


More information about the Cuis-dev mailing list