Refactoring Tools Can Prep Your Code For Web Services

John Zukowski looks at JBuilder 7's new "refactoring" features, as well as other tools and techniques for cleaning, speeding and revving your code before implementing web services

Tags: Refactoring, JBuilder, Exception, Developers, Support, Try/catch Block, Extract Method,

Refactoring is the process of taking poorly designed programs and improving the quality of these programs. The techniques of "refractoring" also can clean up existing code to work better in a "web services" environment.

Popularized by the Martin Fowler book titled "Refactoring: Improving the Design of Existing Code", the process of refactoring code is an area where automated tool support is not only possible but practically a requirement if you want full analysis of changes and their effect before they occur.

Applying Refactoring to Web Services Prep
While refactoring techniques have not been widely used or understood by the majority of developers, those working on "web services" projects may find the techniques and tools helpful in preparing their code to be used -- or to be accessed -- as a service. Improvements to existing code from refactoring typically occur in either the area of performance or maintainability. Lately, refactoring is becoming less niche and more mainstream.

Aside from Fowler's book, there has been an explosion in the available resources and tools on the subject for all types of developers (Java, ASP/.NET and C++). Adding to the parade of refactoring resources, for example, Borland's JBuilder 7.0's refactoring support has been enhanced.

The following section takes an in-depth look at how JBuilder users might exploit this new refactoring capability (and how it compares with earlier versions).

Refactoring in JBuilder
The practice of using JBuilder to refactor code was first introduced with JBuilder 6. While refactoring was supported, the features were not that sophisticated. In fact, JBuilder 6 provided developers with only the ability to rename a method or variable.
In fact, the basic process of renaming doesn't even require such support. Naming (or renaming) can be handled via the search and replace capabilities of any word processor. In short, the JBuilder 6 capabilities would only ensure that whole words are replaced, and the software would check the effect of the changes on ancestors (earlier code versions and dependent classes) of the class being changed.
Refactoring in JBuilder 7 - Features Revealed
In contrast, JBuilder 7 has much richer refactoring support -- a condition refactoring guru Fowler would call "Crossing the Rubicon"

In addition to its renaming capabilities, JBuilder 7 also supports refactorings that require further comprehension of the underlying source code, the so-called "Rubicon." To help with project evolution, the refactorings support in JBuilder 7 includes a rich set of source code support, including: Extract Method; Introduce Explaining Variable; Add/Remove/Reorder Parameters; Surround with Try/Catch Block; and Move Package.

Here is a quick look at each feature, and the problems they solve.

  • The Extract Method refactoring permits the extracting of a code fragment into its own method. Pulling a code fragment into its own method permits the fragment to have a well-defined name to help in the comprehension and maintainability of the code. Extract Method also permits greater reusability, as common fragments won't need to be repeated and updated separately. By extracting fragments into their own method, a developer can expose smaller-grained capabilities in things like web services, rather than being limited a single high-end capability

  • The Introduce Explaining Variable refactoring is a way of taking the results of an intermediate expression and saving that expression in a variable. This variable can help explain the purpose of the underlying code and make the code more manageable.

  • The Add/Remove/Reorder Parameter are altering refactorings that help with the evolution of code. As the requirements of the underlying code change, parameters to methods either become unnecessary or essentially just evolve. For instance, when credit cards were first accepted on the web, a user was only required to enter the credit card number, expiration date, and name. Now, many places require other entries -- notably the few digits that appear on the back next to your signature. Making the change to the underlying code that checks credit card numbers is simple. But it's not often easy to propagating that change to services, datasets and applications that will need the extra numbers. JBuilder 7's refactoring features shine in this type of change, making it easier for developers to revealing these numbers via a wide range of calling methods.

  • The Surround with Try/Catch Block refactoring makes it easier for develoeprs to use Java's exception handling features of Java. Java requires the handling of exceptions within code. This automated refactoring method calculates what exceptions could be thrown and automates the generating of the surrounding code to handle those exceptions. All you must do is add the programming logic to handle or log what to do when the exception happens. For instance, if credit card limit is exceeded for an online purchase, that's probably an exceptional condition that would be handled in this manner.

  • The Move Package refactoring goes beyond the JBuilder 6 renaming refactorings. With Java, packages are logical groupings of classes. As programs evolve, what may have once been a logical grouping might require further subgrouping, or other regroupings. These packages are structured such that the physical directory they are located in helps determine the package the classes are in. The Move Package refactoring will physically move source code and adjust the source accordingly, when such a move is necessary.

  • Code Sample - Demo
    To demonstrate, start up JBuilder 7, close all the projects, and create a new application. In the main application class, define a method that creates a new URL. Remember to import the package.

    private void helpMe() {
    String urlString = "the url";
    URL url = new URL(urlString);

    At this point, notice that the call to the URL constructor shows a squiggly red line under the class name. Resting the mouse over the name shows a message of "Unreported exception:; must be caught or declared to be thrown." What this means is the code is missing a try/catch block to deal with an exception thrown by the constructor.

    While a developer could manually add the necessary try/catch block, JBuilder can automatically add the necessary code. By pressing "Ctrl-Shift-C" or right mousing and selecting "Surround with Try / Catch" function will cause the single line of code to be surrounded by the specific try-catch block necessary for the thrown exception or exceptions. In this case, that exception is MalformedURLException, meaning the new method definition appears as follows:

    private void helpMe() {
    String urlString = "the url";
    try {
    URL url = new URL(urlString);
    catch (MalformedURLException ex) {

    With the Extract Method feature, for instance, a developer can gain access to a popup menu that will name the method by simply highlighting the entire "Try-Catch block" and pressing Ctrl-Shift-E or right mouse over the selected block and select Extract Method. After naming the method and pressing OK, the urlString variable used by the URL constructor is now a parameter for the newly generated method.

    This feature of the tool to extract methods will automatically detect what variables are referenced within the newly generated method and automatically convert the variables to parameters to the new method.

    More Refactoring Resources
    Borland's JBuilder 7 is just the latest in a growing list of refactoring tools and literature aimed at helping enterprise developers better prep their source code for web services and application/data integration. Here is more of IDN's Best of the Web resources and links for "Refactoring"

  • Get the latest from one of the leading voices on Java refactoring, Martin Fowler, author of "Refactoring" from Addison-Wesley. Also, get a core list of patterns, code samples and approaches for refactoring from Fowler's book and other contributors .

  • Martin Fowler also has a great collection of articles that walk developers through principals and benefits of refactoring and extreme programming (XP).

  • Visual Studio .NET has all of the elements that it could possibly need to support a good refactoring tool. Get a MCSE's view of how it can benefit development at

  • Download a copy of Jfactor, refactoring tools that work within Java IDEs. Versions available for Visual Age and JBuilder.

  • Refactoring using an "extreme programming" model with techniques and workshop references are available from Joshua Kerievsky at

  • Download Transmogrify, an Open Source refactoring tool with special Java classes support.

  • back