Refactoring Tools Can Prep Your Code For Web Services
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.
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 java.net package.
import java.net.*;
…
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: java.net.MalformedURLException; 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"









