Wikipedia defines Code Refactoring as:
the process of restructuring existing computer code â changing the factoring â without changing its external behavior. Refactoring improves nonfunctional attributes of the software.
Did you know that Xojo has a bunch of refactoring features that can help make this process more efficient?
When you access the contextual menu of a class (right-click or control-click on OS X), you’ll see these two features in the menu: New Subclass and Extract Superclass.
Click on New Subclass and a new class is added to your project that automatically has the selected class set as its Super.
The Extract Superclass feature opens a dialog that lets you choose the methods and properties to extract from the selected class and move to a superclass. The selected class also gets its Super set to the newly created super class.
Similar to Extract Superclass, this feature opens a dialog that lets you choose the methods to extract into a new Interface. The selected class keeps the method definitions and has the Interface added to it.
Methods and Properties
The contextual menu that displays for methods and properties also has some refactoring features.
With a regular method selected, you will see Convert to Shared. This feature converts the method from a regular method (aka an instance method) to a shared method. If you were to right-click on a shared method then you would instead see Convert to Regular which does the opposite.
Properties also have Convert to Shared/Convert to Regular. In addition they have Convert to Computed Property. This feature takes your current regular property, renames it with an “m” at the beginning, makes it private and then makes a public computed property that uses the regular property as its back end storage for the Get and Set components.
When you are editing code, the contextual menu has several useful refactoring features.
Wrap in If/End If
This takes either the current line of code or the current selection and wraps it in an If..Then/End If block.
Wrap in Do/Loop
As above, but wraps it in an Do..Loop block.
Wrap in While/Wend
As expected, wraps it in a While..Wend block.
Convert to Method
This feature takes the current selection and moves it to a method for you and then takes you to the method where you can specify the name and parameters. Don’t forget to go back to the original code and add the method call!
Convert to Constant
Convert to Constant takes the selected text and moves it to a constant definition. The name of the constant is the name of the selected text. Xojo replaces the selected text with the newly created constant name. This is very handy for localizing as it makes it quick and easy to move hard-coded strings to constants.
Define Missing Method
Creates a new method using the selected method as the method name. If you have supplied parameters, their types are automatically added to the method definition. For method calls without parameters, you’ll need to include (and select) blank parenthesis in order for this option to be enabled in the menu.
This is my favorite feature. I use this all the time. It takes the selected block of code and applies consistent formatting to it. There is nothing that I hate more than source code that has inconsistent naming.
Inconsistent capitalization drives me nuts (see Coding Guidelines):
dim i as Integer
I really prefer to see:
Dim i As Integer
or at the very least this, even though I don’t like all lowercase:
dim i as integer
Standardize Format keeps me sane!
Take the time to improve your code by refactoring it. Your future self will thank you. Also, consider coming to XDC 2016, your future self will enjoy it!