User talk:Sheng yi: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(9 intermediate revisions by the same user not shown)
Line 39: Line 39:


=== Some Ruby Example of refactoring ===
=== Some Ruby Example of refactoring ===
wait for Julian
before rename:
 
def badRenameMethod
    puts("Hello World");
end
 
after rename:
 
def hello
put("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
 
=== Some Java Example of refactoring ===
=== Some Java Example of refactoring ===
wait for Julian
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 ==
== Refactoring support for Current Ruby IDE ==
Line 66: Line 146:


=== Example of using Ruby refactoring tools in Eclipse ===
=== Example of using Ruby refactoring tools in Eclipse ===
rename:  
example of rename:  
{{multiple image
  | width    = 100
  | footer    = Two cards used by football referees
  | image1    = r00.jpg
  | alt1      = Yellow cartouche
  | caption1  = Caution
  | image2    = r01.jpg
  | alt2      = Red cartouche
  | caption2  = Ejection
  | image3    = r02.jpg
  | alt3      = Red cartouche
  | caption3  = Ejection
  | image4    = r04.jpg
  | alt4      = Red cartouche
  | caption4  = Ejection
  | image5    = r05.jpg
  | alt5      = Red cartouche
  | caption5  = Ejection


  }}
{|
| [[Image:r00.jpg|thumb|upright|alt= rename step0]]
| [[Image:r01.jpg|thumb|upright|alt= rename step1]]
| [[Image:r02.jpg|thumb|upright|alt= rename step2]]
| [[Image:r03.jpg|thumb|upright|alt= rename step3]]
| [[Image:r04.jpg|thumb|upright|alt= rename step4]]
|}


[[Image:r00.jpg]]
example of encapsulation:
[[Image:r01.jpg]]
{|
[[Image:r02.jpg]]
| [[Image:r05.jpg|thumb|upright|alt= encapsulation step0]]
[[Image:r03.jpg]]
| [[Image:r06.jpg|thumb|upright|alt= encapsulation step1]]
[[Image:r04.jpg]]
| [[Image:r07.jpg|thumb|upright|alt= encapsulation step2]]
| [[Image:r08.jpg|thumb|upright|alt= encapsulation step3]]
| [[Image:r09.jpg|thumb|upright|alt= encapsulation step4]]
|}


encapsulation:
=== Example of using Java refactoring tools in Eclipse ===
[[Image:r05.jpg]]
example of rename:  
[[Image:r06.jpg]]
[[Image:r07.jpg]]
[[Image:r08.jpg]]
[[Image:r09.jpg]]


=== Example of using Java refactoring tools in Eclipse ===
{|
| [[Image:j00.jpg|thumb|upright|alt= rename step0]]
| [[Image:j01.jpg|thumb|upright|alt= rename step1]]
| [[Image:j02.jpg|thumb|upright|alt= rename step2]]
| [[Image:j03.jpg|thumb|upright|alt= rename step3]]
| [[Image:j04.jpg|thumb|upright|alt= rename step4]]
| [[Image:j05.jpg|thumb|upright|alt= rename step0]]
|}


example of encapsulation:
{|
| [[Image:j06.jpg|thumb|upright|alt= encapsulation step1]]
| [[Image:j07.jpg|thumb|upright|alt= encapsulation step2]]
| [[Image:j08.jpg|thumb|upright|alt= encapsulation step3]]
| [[Image:j09.jpg|thumb|upright|alt= encapsulation step4]]
|}


== Ruby vs Java in terms of refactoring==
== Ruby vs Java in terms of refactoring==

Latest revision as of 03:02, 6 September 2009

What is code refactoring

According to the famous book "Refactoring: Improving the Design of Existing Code " by Martin Fowler: 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 main refactoring

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

Some Ruby Example of refactoring

before rename:

def badRenameMethod
   puts("Hello World");
end

after rename:

def hello
put("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

Some 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

example of rename:

rename step0
rename step1
rename step2
rename step3
rename step4

example of encapsulation:

encapsulation step0
encapsulation step1
encapsulation step2
encapsulation step3
encapsulation step4

Example of using Java refactoring tools in Eclipse

example of rename:

rename step0
rename step1
rename step2
rename step3
rename step4
rename step0

example of encapsulation:

encapsulation step1
encapsulation step2
encapsulation step3
encapsulation step4

Ruby vs Java in terms of refactoring

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 on ruby on rail, http://www.good-tutorials.com/tutorials/ruby-on-rails/refactoring