Friday, April 27, 2012

Real Studio Refactoring Tips

Did you know that Real Studio has a bunch of refactoring features that can help make you more efficient?  Let's take a look at them.

Classes

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.

New Subclass
Click on New Subclass and Real Studio creates a new class in your project that automatically has the selected class set as its Super.

Extract Superclass
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.
Extract Superclass Dialog
Methods

With the Code Editor open, you can see refactoring features when you view the contextual menu for methods and properties.

With a regular method selected, you will see Convert to Shared.  This feature converts the method from a regular method (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 have a similar feature to Convert to Shared or Convert to Regular.  In addition they have Convert to Computed Property.  This feature will take your current regular property, rename it with an "m" at the beginning, make it private and then make a public computed property that uses the regular property as its back end storage for the Get and Set components.

Code Editor

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, though!

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.  Real Studio replaces the selected text with the newly created constant name.  This is very handy for localizing as it makes is quick and easy for you to move hard-coded strings to constants.

Define Missing Method
When I write my code, I often will type a method name that I expect to use even though it doesn't actually exist yet.  I can then go back, select the method name and choose Define Missing Method to have Real Studio create the method definition for me.  In order for this option to be enabled, you do have to select a method that has parenthesis even if there are no arguments.  So Calculate() works but just Calculate does not.

Standardize Format
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:
dim i as Integer

It should be:
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!

Properly Formatted Code


I hope one of these features will help make your development more efficient!

10 comments:

Thomas Tempelmann said...

1. To all who read this: If you are going to add localization to your app and you face converting 100s of strings in your code into constants, have a look at my program "Arbed" which automates this significantly. See: http://www.tempel.org/RB/Localization

2. "dim i as integer" is actually not the "correct" case, but "dim i as Integer" is, usually: "dim" and "as" are keywords - capitalize them as you like. "Integer" is not a keyword, though, it's simply a pre-defined type. And most users write their types ("MyClass") in upper case, so Integer should be as well. ;)

Paul Lefebvre said...

I've used Arbed to localize an app; it worked great.

Capitalized keywords are the only way to go!

Eduo said...

Is there any reason a keyboard shortcut for "standardize" is not present? One would think it would be there since day one.

"Standardize all code on Save" is an option I wouldn't mind having :)

Beatrix Willius said...

"Standardize format" is the lamest thing ever. Even old, old Access 2000 did this automatically.

Paul Lefebvre said...

There is a Feedback case to improve Standardize Format:

Paul Lefebvre said...

(comment ate the link)

Feedback case for Standardize Format:

feedback://showreport?report_id=4155

Thomas Tempelmann said...

Maybe it's time I roll out the syntax beautifier feature in Arbed, which not only could standardize keyword case but also add/remove spaces around () and such things.

Rick said...
This comment has been removed by the author.
Rick said...

Many IDEs have a "reformat code", "beautify", or similar feature. You can ask for all code or select a range of the code.
My previous post I copied/pasted some examples, but Blogger acrambled the post and I deleted. :)

Unknown said...

When talking about Standardize Format, you may want to have a look to my Code Formatting folder in Feedback. There are multiple cases around the topic, also the request for Standarizing whitespace is present, but maybe it should be moved to it's own case...

feedback://subscribe?folder_id=10

Tobias Bußmann