Oracle Blog

Tor Norbye's Weblog

Ruby Screenshot of the Week #23: Extract Method and More Refactorings!

Last week I promised to catch up on my e-mail, but I had been missing feature work too much during the bug phase so I put it off for a week... to implement some more quickfix refactorings:

Extract Method

Introduce Variable

Introduce Constant

Here's how it works. Let's start with "Extract Method". You're looking at some code like this:

You decide there's too much going on in this method, and you want to pull the middle section into
its own method. Select it, and notice the lightbulb which shows up on the left:

Press Alt-Enter to show the quick fix alternatives:

Select Extract Method, and the IDE will pop up a dialog asking you for the name of the new method you want to extract from the selected code fragment:

Press OK (or just hit Enter), and the code will mutate into the following:

There's a lot to notice here. First, there's a new method, and the active selection and caret is on a comment for that method (so you can just type to replace it). The new method is added below the one you extracted code from. And the most important part about this refactoring is that the IDE figures out which variables to pass in to the method, and which variables to pass back out:

a, b and d are accessed from within the fragment, so they are passed in.

c is reassigned in the fragment without reading the previous value, so
doesn't need to be passed in.

f and h are assigned locally inside the extracted fragment, but are not read
outside of it, so do not need to be passed back out

g is assigned inside the fragment, and read later outside, so it is returned
from the new method but not passed in

h is assigned inside the fragment, and is read later, but it is assigned
before this read access so the value doesn't need to be passed back

i is also assigned inside the fragment, and -may- be read after the fragment,
so it too is passed back out

Ruby's multiple return values makes this refactoring much cleaner than in Java where you have
to jump through some hoops to extract code fragments that modify multiple local variables...

Now let's take a look at Introduce Constant. Let's say you're looking at code like this (unlike the above contrived example from one of my unit tests for Extract Method, the following is from
the standard Ruby Library's Date class):

There are a lot of "magic" numbers here. I honestly don't know what some of them are - but I recognize 365.25 as the number of days per year. Let's make that clearer - select that constant. (Tip - just move the caret to it and press Ctrl-Shift-Dot, which selects progressively larger logical elements around the caret). This produces the above lightbulb, so let's press Alt Enter again:

I can now choose to either introduce a field, or a variable, or a constant. A constant is most natural here. (You won't be offered constant if the selected code fragment is not a constant expression.) So choose Introduce Constant:

In the dialog asking for the name of the new constant, notice that it also detected some duplicates of this constant in the same class (3 of them to be exact), and asks if you want to replace all of them. I do - so I select the checkbox and press Ok:

The IDE has inserted a new constant at the top of the class, and has warped to it to let me edit a comment for the constant. I can also scroll down and see that the constants below were updated:

The search for duplicates only looks for single constants at the moment, not more complicated expressions - it will do that soon. As always, please report any bugs you encounter. This is in the daily 6.1 trunk builds, although I've deliberately kept the code 6.0 compatible such that I can put this out on the update center for 6.0 as well.

hi Tor! Excellent!! I have some question from another side - is there a way to increase the size of font in main menu of NetBeans, and also in the names of opened files, content of the project in the right panel? They look too small - really hardly to see on them. Thanks a lot!!

Jon, do you have any specific issues you'd like to see addressed first? Just smarter type inference in general? Or tracking of block variables, or ability for it to show operators when invoking completion between identifiers, etc?
<p>
Greg, thanks and let me know how the migration for gvim works out!
<p>
Freeman, try running NetBeans with the --fontsize argument, e.g. netbeans --fontsize 16 (you can specify the startup flag persistently by editing the etc/netbeans.conf file (or etc/nbrubyide.conf if you're running the distro from deadlock.netbeans.org).

Freeman, anti aliasing is supposed to be used automatically, as long as (1) you're using a recent Java 6 version, and (2) you've enabled it on the rest of your desktop. I forwarded your HAML comment to the HAML module developer, Dylan Bruzenak.

Casper, yes, precisely - there are several variations; one is to return in Object[] (which I don't like because of the lack of type safety), another is to have multiple "out" parameters; if I want to pass back an int and a boolean for example, I pass in a int[] and a boolean[] and I return the results in [0] of each array. (And I never do this in API code; in that case, there's a result object class.)

[Trackback] If you haven't tried calling Java classes from a JRuby application yet, here is a simple code snippet to get you started. Paste the following code into the Ruby shell (JRuby IRB), press Enter, and a small desktop app opens (To open the Ruby shell in th...

Once again thanks for all the great improvements! This product is very exciting and I absolutely love it. I've been using beta 1 but decided to switch to the nightly builds. Can I safely run the exe for the nightly builds and have to do any reconfiguring of svn or projects? Wasn't sure if it was safe to do this from beta 1? Thanks again Tor, great job.

That won't include the extra hints but they should appear on the update center pretty soon.

Your userdir (where persistent settings for things like SVN are stored) is typically -not- migrated from milestone build to milestone build since some things change incompatibly during development; it's major version to major version compatibility that is a priority. So, you -may- have to do that setup over again. But as a simple fix you can try copying your userdir to a new location and invoking netbeans with --userdir /your/new/location and it will probably work.

There are some possibilities - either you've found a bug such that the code you're selecting isn't properly recognized as a refactorable code fragment, or perhaps the module isn't installed at all. First, go to the Options dialog, the Ruby category, the "Hints" panel - and see if you get a lot of extra hints. (Selection-based hints like Extract Method won't be listed there, but you should see at least a dozen hints if you have the extras properly installed). If so, check whether the following code fragment generates a warning

x = 1
y = 2
if (x = y)
puts "Hello"
end

That should generate a warning if you have the latest build. If you do, then please mail me the exact code fragment you're selecting so I can see why the code finds a problem with it. (I examine code fragments and disable refactoring if you've selected something "unbalanced", or something containing code not eligible for extract method, such as code containing new method definitions etc.)

I have a question. When I start Mongrel, I see development.log in output window. In this log using some codes to colorize output. Output window doesn't support this highlighting. Will It be in future version?