CSC/ECE 517 Fall 2009/wiki 1a 9b SJ

From Expertiza_Wiki
Jump to navigation Jump to search

Refactoring tools for Ruby and Ruby on Rails.

What is Code Refactoring

According to the famous book "Refactoring: Improving the Design of Existing Code " by Martin Fowler: [5], refactoring is the process of applying behavior-preserving transformations to a program with the objective of improving the program’s design. Intuitively people refer to code refactoring as "cleaning it up."

Motivation of Refactoring

Refactoring is very useful for software engineer to maintain the quality of their code in different environment. In fact, no software can be guaranteed to be perfect when it is first developed. Usually when the source code is applied to different customer needs, we want to improve the inner-structure without modify the external behavior. The ability to refactor your code takes the pressure off the design phase of software development. Refactoring gives you the ability to change the design of the code at a later stage. This means that you don’t have to get the design absolutely right before you write any code. You can get a rough design worked out, code it up and then if (when) you spot a better design you can refactor your code towards the better design.

List of Common Refactorings

In the following we list some popular refactoring to specify how refactoring transform the code. For more detail information, the reader please refer to the section 2.1.2 of the thesis of Thomas Corbat [1]. [1] provide Ruby example for each refactoring this list.

1. Merge Class Parts
2. Convert Local Variable to Field
3. Encapsulate Field
4. Extract Method
5. Inline Class
6. Inline Method
7. Move Field
8. Move Method
9. Rename Class
10. Rename Field
11. Rename Local Variable
12. Rename Method
13. Replace Temporary Variable with Query
14. Split Temporary Variable

Ruby Example of Refactoring

  • Before Rename:
def badRenameMethod
    puts("Hello World");
end
  • After Rename:
def hello
    puts("Hello World");
end
  • Before Encapsulation:
class SomeClass
    def initialize
        @field = 0;
    end
end
  • After Encapsulation:
class SomeClass                
    def initialize
        @field = 0;
    end
    
    def field
        @field
    end
    private :field
    
    def field= field
        @field = field
    end
    private :field=
end

Java Example of Refactoring

  • Before Rename:
public class rename_method
{
    static void badRenameMethod()
    {
        System.out.println("Hello, world!");
    }
}
  • After Rename:
public class rename_method
{
    static void hello()
    {
        System.out.println("Hello, world!");
    }
}
  • Before Encapsulation:
public class encap_field
{
    public String field;
}
  • After Encapsulation:
public class encap_field
{
    private int field;

    public void setField(int field)
    {
        this.field = field;
    }

    public int getField()
    {
        return field;
    }
}

Refactoring support for Current Ruby IDE

This the following table we summary the current ruby IDE and their refactoring support situation.

Current Ruby IDE Refactoring Support (Y/N)
ActiveState Komodo N
NetBeans N
Arachno Ruby N
FreeRIDE N
Mondrian Ruby IDE N
Ruby in Steel N
RubyMine Y
Eclipse (Aptana) Y

Example of Using Ruby Refactoring Tools in Eclipse

Here the renaming of the method "badRenameMethod" to "Hello" is illustrated for Ruby. The source code is shown in section 1.3.

The badly named method "badRenameMethod is shown with a call to this method.
The method is highlighted and then right clicked on. Refactor then Rename is selected.
The method is renamed to "hello"
A preview of each call to the method.
The resulting code after refactoring is shown with newly renamed method.

Here the encapsulation of the field "field" is illustrated for Ruby. The source code is show in section 1.3.

The field to be encapsulated is shown inside its class.
The field is highlighted and then right clicked on. Refactor then Encapsulate Field is selected.
A reader and writer method is selected to be generated and labeled private.
A preview of the encapsulation.
The resulting code after refactoring is shown with encapsulated field.

Example of Using Java Refactoring Tools in Eclipse

Here the renaming of the method "badRenameMethod" to "Hello" is illustrated for Java. The source code is show in section 1.4.

The badly named method "badRenameMethod is shown.
The badly named method is called.
The method is highlighted and then right clicked on. Refactor then Rename is selected.
The method is renamed to "hello" as shown and preview is selected.
A preview of each call to the method.
The resulting code after refactoring is shown with newly renamed method. The call is shown here.

Here the encapsulation of the field "field" is illustrated for Java. The source code is show in section 1.4.

The field to be encapsulated is shown inside its class.
The field is highlighted and then right clicked on. Refactor then Encapsulate Field is selected.
A getter(reader) and setter(writer) mehtod is selected and named.
The resulting code after refactoring is shown with encapsulated field.

Comparison of Ruby and Java Refactoring Tools

Comprehensiveness

These are the following refactoring options available in eclipse for Ruby and Java.

Refactoring Options for Java in Eclipse Refactoring Options for Ruby in Eclipse
Rename Rename
Move Move
Change Method Signature Merge Class Parts in File
Extract Local Variable Pull Up
Extract Constant Pull Down
Inline Merge With External Class Parts
Convert Anonymous Class to Nested Split Local Variable
Convert Member to Top Level Merge With External Class Parts
Convert Local Variable to Field Inline
Extract Super Class Extract Constant
Extract Interface Encapsulate Field
Extract Class Convert Local Variable to Field
Use Supertype where possible
Pull Up
Pull Down
Introduce Parameter Object
Introduce Indirection
Introduce Factory
Introduce Parameter
Encapsulate Field
Generalize Declared Type
Infer Generic Type Arguments

Ease of Use and Integrated Develop Environment(IDE) Integration

The refactoring options for Ruby and Java cover all the basic refactorings. The differences in the number of options mainly come from the difference in the two languages. Ruby is a dynamically typed language, has unbounded polymorphism, and all classes are open to extension or can be inherited. On the other hand, Java is a statically typed language, polymorphism is mainly implemented via interfaces and inheritance, and classes cannot be extended at run-time. The given examples (in section [1.3], [1.4]) illustrate the ease of use of the refactoring tools for both Ruby and Java and how well these tools are integrated into the Eclipse IDE.

References

[1] Thomas Corbat, Lukas Felber, Mirko Stocker Refactoring Support for the Eclipse Ruby Development Tools, Diploma Thesis, HSR University of Applied Sciences Rapperswil Institute for Software.

[2] Code Refactoring Wiki, http://en.wikipedia.org/wiki/Code_refactoring.

[3] The First Workshop on Refactoring, https://netfiles.uiuc.edu/dig/RefactoringWorkshop

[4] Refactoring Tutorial for Ruby on Rails, http://www.good-tutorials.com/tutorials/ruby-on-rails/refactoring

[5] Martin Fowler Refactoring: Improving the Design of Existing Code