CSC/ECE 517 Fall 2009/wiki 1a 7a HJ: Difference between revisions
(37 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
According to Wikipedia: "Code refactoring is the process of changing a program's internal structure without modifying its external functional behavior or existing functionality with the goals of readability, maintainability, performance, extensibility and simplicity." | According to Wikipedia: "Code refactoring is the process of changing a program's internal structure without modifying its external functional behavior or existing functionality with the goals of readability, maintainability, performance, extensibility and simplicity." | ||
<br> | |||
== Goals of refactoring == | == Goals of refactoring == | ||
Line 27: | Line 26: | ||
# ''' is more flexible towards future changes ''' | # ''' is more flexible towards future changes ''' | ||
# ''' could lead to a faster compilation time ''' | # ''' could lead to a faster compilation time ''' | ||
<br> | <br> | ||
Line 34: | Line 32: | ||
Here is a list of some examples of refactoring techniques used. For further information, the reader please refer to Lecture 3 of CSC 517 of Dr. Gehringer [1] inspired of the site [2] and to the section 2.1.2 of the thesis of Thomas Corbat [3]. | Here is a list of some examples of refactoring techniques used. For further information, the reader please refer to Lecture 3 of CSC 517 of Dr. Gehringer [1] inspired of the site [2] and to the section 2.1.2 of the thesis of Thomas Corbat [3]. | ||
<br> | <br> | ||
Line 61: | Line 58: | ||
# '''Convert Local Variables to Field''' | # '''Convert Local Variables to Field''' | ||
# '''Change Method Signature''' | # '''Change Method Signature''' | ||
<br> | |||
== Examples of Refactoring == | == Examples of Refactoring == | ||
=== Examples of Refactoring for JAVA === | I inspire myself for the exemples of Dr. Gehringer's course. | ||
=== ''Examples of Refactoring for JAVA'' === | |||
JAVA has a lot of tools for refactoring. | JAVA has a lot of tools for refactoring. | ||
Line 72: | Line 73: | ||
==== Rename ==== | ==== Rename ==== | ||
We refactor the code by renaming. | |||
Before renaming: | |||
<pre> | |||
public class rename_Method | public class rename_Method | ||
{ | { | ||
static void WrongRenameMethod() { | static void WrongRenameMethod() { | ||
System.out.println(" | System.out.println("Hello, world"); | ||
} | } | ||
} | } | ||
</pre> | |||
After using rafactor tool rename: | |||
<pre> | |||
public class rename_Method | public class rename_Method | ||
{ | { | ||
static void | static void hello(){ | ||
System.out.println(" | System.out.println("Hello, world"); | ||
} | } | ||
} | } | ||
</pre> | |||
==== Encapsulation ==== | |||
<br> | |||
The initial code is: | |||
<pre> | |||
public class encap_abc { | |||
public String abc; | |||
} | |||
</pre> | |||
After using the encapsulation, the refactored code is: | |||
<pre> | |||
public class encap_abc { | |||
private int abc; | |||
public void setAbc(int abc) | |||
{ | |||
this.abc; | |||
} | |||
public int getAbc(){ | |||
return abc; | |||
} | |||
} | |||
</pre> | |||
<br> | |||
=== Examples of | === ''Examples of Refactoring for Ruby'' === | ||
Line 104: | Line 130: | ||
==== Rename ==== | ==== Rename ==== | ||
Before rename: | |||
<pre> | |||
def FirstMethod | def FirstMethod | ||
puts("NC State"); | puts("NC State University"); | ||
end | end | ||
</pre> | |||
After refactoring with rename: | |||
<pre> | |||
def NCSU | def NCSU | ||
puts("NC State"); | puts("NC State University"); | ||
end | end | ||
</pre> | |||
==== Encapsulation ==== | ==== Encapsulation ==== | ||
<br> | |||
The initial code is: | |||
<pre> | |||
class OneClass | |||
def initialize | |||
@field = 0; | |||
end | |||
</pre> | |||
The refactored code after using encapsulate is: | |||
<ref> | |||
class OneClass | |||
def initialize | |||
@field = 0; | |||
end | |||
def field | |||
@field | |||
end | |||
private: field | |||
end | |||
</ref> | |||
<br> | |||
== Current IDE and Refactoring == | == Current IDE and Refactoring == | ||
Line 125: | Line 173: | ||
Most of Java IDE support refactoring. | Most of Java IDE support refactoring like Eclipse or IntelliJ IDEA. | ||
<br> | <br> | ||
Eclipse which | Eclipse or IntelliJ IDEA which are some of the most used JAVA IDE, have a lot of refactoring tools already : '''one clik and you refactor your code'''. | ||
=== Current Ruby IDE and Refactoring === | === Current Ruby IDE and Refactoring === | ||
Line 143: | Line 191: | ||
* RubyMine | * RubyMine | ||
* Eclipse with Aptana | * Eclipse with Aptana | ||
=== Current Ruby on Rails IDE and Refactoring === | |||
RubyMine [8] is the only Ruby on Rails IDE with refactoring support but it is still the beginning of refactoring in Ruby on Rails. | |||
<br> | <br> | ||
== Ruby and JAVA Refactoring Tools : a small comparaison == | == Ruby Ruby on Rails and JAVA Refactoring Tools : a small comparaison == | ||
{| class="wikitable" | {| border="1" class="wikitable" width="60%" align="center" | ||
|+ Comparaison of Refactoring Tools | |+ '''Comparaison of Refactoring Tools''' | ||
<br> | |||
<br> | |||
|- | |- | ||
|- | |- | ||
|- | |- | ||
! scope=col | JAVA | ! scope=col | JAVA Refactoring tools | ||
! scope=col | Ruby | ! scope=col | Ruby Refactoring tools | ||
! scope=col | Ruby on Rails Refactoring tools | |||
|- | |- | ||
|Rename | |||
|Rename | |Rename | ||
|Rename | |Rename | ||
Line 162: | Line 217: | ||
|Move | |Move | ||
|Move | |Move | ||
| | |||
|- | |- | ||
|Extract Class | |Extract Class | ||
|Convert Local Variable or Field | |Convert Local Variable or Field | ||
|Extract Methode | |||
|- | |- | ||
|Extract Super Class | |Extract Super Class | ||
|Extract Constant | |Extract Constant | ||
| | |||
|- | |||
|Extract Local Variable | |||
|Pull Up | |||
| | |||
|- | |||
|Extract Constant | |||
|Pull Down | |||
| | |||
|- | |- | ||
|Extract Interface | |||
|Encapsulate Field | |Encapsulate Field | ||
| | |||
|- | |||
|Use Supertype | |||
| | |||
| | |||
|- | |||
|Encapsulate Field | |||
| | |||
| | |||
|- | |||
|Pull Up | |||
| | |||
| | |||
|- | |||
|Pull Down | |||
| | |||
| | | | ||
|- | |- | ||
|Introduce Parameter | |Introduce Parameter | ||
| | | | ||
|Introduce Variable | |||
|- | |- | ||
|Introduce Parameter Object | |||
| | |||
| | |||
|} | |} | ||
<br> | |||
== Conclusion == | == Conclusion == | ||
Line 187: | Line 273: | ||
Ruby is like a children which is learning and improving his knowledge by trying to be inspired by what the other already have. | Ruby is like a children which is learning and improving his knowledge by trying to be inspired by what the other already have. | ||
Ruby on Rails knowledge in refactoring is less than Ruby and Ruby and Ruby on Rails is making his first move in the refactoring world. | |||
However, refactoring tools for both JAVA and Ruby are integrated into some IDE like Eclipse and RubyMine for both Ruby and Ruby on Rails. | |||
<br> | |||
== References == | == References == | ||
Line 196: | Line 284: | ||
[1] http://courses.ncsu.edu/csc517/common/lectures/notes/lec3-f09.pdf | [1] http://courses.ncsu.edu/csc517/common/lectures/notes/lec3-f09.pdf | ||
<pre> | |||
Refactoring and version control, CSC517, NCSU, Dr. Ed Gehringer | Refactoring and version control, CSC517, NCSU, Dr. Ed Gehringer | ||
</pre> | |||
[2] http://www.refactoring.com | [2] http://www.refactoring.com | ||
[3] | [3] http://subs.emis.de/LNI/Proceedings/Proceedings106/gi-proc-106-039.pdf | ||
Complete Thesis: | <pre> | ||
Complete Thesis: </pre> http://r2.ifs.hsr.ch/rubyrefactoring.pdf | |||
Refactoring Support for the Eclipse Ruby Development Tools | Refactoring Support for the Eclipse Ruby Development Tools | ||
HSR, University of Applied Science Rapperswil | HSR, University of Applied Science Rapperswil | ||
Diploma Thesis of Thomas Corbat, Lukas Felber and Mirko Stocker | Diploma Thesis of Thomas Corbat, Lukas Felber and Mirko Stocker | ||
[4] http://refactormycode.com/codes/recent/ruby | [4] http://refactormycode.com/codes/recent/ruby | ||
Forum for help on refactoring in Ruby | <pre> | ||
Forum for help on refactoring in Ruby | |||
</pre> | |||
[5] http://refactormycode.com/codes/recent/java | [5] http://refactormycode.com/codes/recent/java | ||
Forum for help on refactoring in Java | <pre> | ||
Forum for help on refactoring in Java | |||
</pre> | |||
[6] http://www.slideshare.net/jweiss/refactoring-ruby-on-rails-applications | [6] http://www.slideshare.net/jweiss/refactoring-ruby-on-rails-applications | ||
[7] http://railsforum.com/viewtopic.php?id=699 | |||
<pre> | |||
An exemple of refactoring Ruby on Rails | |||
</pre> | |||
[8] http://www.jetbrains.com/ruby/features/ruby_ide.html#refactorings | |||
The only IDE which support Refactoring for Ruby on Rails |
Latest revision as of 12:20, 17 September 2009
Definition of code refactoring
According to Wikipedia: "Code refactoring is the process of changing a program's internal structure without modifying its external functional behavior or existing functionality with the goals of readability, maintainability, performance, extensibility and simplicity."
Goals of refactoring
Refactoring is motivated by the difficulty of adding new functionality to a program or fixing a bug in it.
As we said before, after code refactoring, the program does the the exact same thing it did before.
Refactoring is very useful to improve a program because a software can not be perfect when it is first developed.
Refactoring is one of the most useful tool for software engineer because they can write a rough design which work at first and then refactor the code when they conceive a better design.
The differences are that the new code:
- is easier to read and so to understand
- contains less duplication
- is less complex
- is more flexible towards future changes
- could lead to a faster compilation time
Common Refactorings Tools
Here is a list of some examples of refactoring techniques used. For further information, the reader please refer to Lecture 3 of CSC 517 of Dr. Gehringer [1] inspired of the site [2] and to the section 2.1.2 of the thesis of Thomas Corbat [3].
- Rename
- Methods
- Fields
- Packages
- Projects
- Parameters
- Local Variables
- Encapsulate Field
- Move
- Method
- Field
- Pull up
- Field
- Method
- Push down
- Field
- Method
- Extract
- Method
- Class
- Constant
- Local Variables
- Convert Local Variables to Field
- Change Method Signature
Examples of Refactoring
I inspire myself for the exemples of Dr. Gehringer's course.
Examples of Refactoring for JAVA
JAVA has a lot of tools for refactoring. Here are some examples of Refactoring for JAVA :
Rename
We refactor the code by renaming.
Before renaming:
public class rename_Method { static void WrongRenameMethod() { System.out.println("Hello, world"); } }
After using rafactor tool rename:
public class rename_Method { static void hello(){ System.out.println("Hello, world"); } }
Encapsulation
The initial code is:
public class encap_abc { public String abc; }
After using the encapsulation, the refactored code is:
public class encap_abc { private int abc; public void setAbc(int abc) { this.abc; } public int getAbc(){ return abc; } }
Examples of Refactoring for Ruby
Ruby is still a newbie in refactoring . Here are some refactoring tools :
Rename
Before rename:
def FirstMethod puts("NC State University"); end
After refactoring with rename:
def NCSU puts("NC State University"); end
Encapsulation
The initial code is:
class OneClass def initialize @field = 0; end
The refactored code after using encapsulate is: <ref> class OneClass
def initialize @field = 0; end
def field @field end private: field
end
</ref>
Current IDE and Refactoring
Current Java IDE and Refactoring
Most of Java IDE support refactoring like Eclipse or IntelliJ IDEA.
Eclipse or IntelliJ IDEA which are some of the most used JAVA IDE, have a lot of refactoring tools already : one clik and you refactor your code.
Current Ruby IDE and Refactoring
The following Current Ruby IDE do not support refactoring :
- Ruby in Steel
- Arachno Ruby
- FreeRIDE
- Mondrian Ruby IDE
The only current Ruby IDE with refactoring support are:
- RubyMine
- Eclipse with Aptana
Current Ruby on Rails IDE and Refactoring
RubyMine [8] is the only Ruby on Rails IDE with refactoring support but it is still the beginning of refactoring in Ruby on Rails.
Ruby Ruby on Rails and JAVA Refactoring Tools : a small comparaison
JAVA Refactoring tools | Ruby Refactoring tools | Ruby on Rails Refactoring tools |
---|---|---|
Rename | Rename | Rename |
Move | Move | |
Extract Class | Convert Local Variable or Field | Extract Methode |
Extract Super Class | Extract Constant | |
Extract Local Variable | Pull Up | |
Extract Constant | Pull Down | |
Extract Interface | Encapsulate Field | |
Use Supertype | ||
Encapsulate Field | ||
Pull Up | ||
Pull Down | ||
Introduce Parameter | Introduce Variable | |
Introduce Parameter Object |
Conclusion
The differences between the two languages JAVA and Ruby is the number of methods available for refactoring and the number of IDE which support refactoring in the two languages.
JAVA has two times more options for refactoring and all of JAVA IDE support refactoring.
Ruby is like a children which is learning and improving his knowledge by trying to be inspired by what the other already have.
Ruby on Rails knowledge in refactoring is less than Ruby and Ruby and Ruby on Rails is making his first move in the refactoring world.
However, refactoring tools for both JAVA and Ruby are integrated into some IDE like Eclipse and RubyMine for both Ruby and Ruby on Rails.
References
[1] http://courses.ncsu.edu/csc517/common/lectures/notes/lec3-f09.pdf
Refactoring and version control, CSC517, NCSU, Dr. Ed Gehringer
[2] http://www.refactoring.com
[3] http://subs.emis.de/LNI/Proceedings/Proceedings106/gi-proc-106-039.pdf
Complete Thesis:
http://r2.ifs.hsr.ch/rubyrefactoring.pdf
Refactoring Support for the Eclipse Ruby Development Tools HSR, University of Applied Science Rapperswil Diploma Thesis of Thomas Corbat, Lukas Felber and Mirko Stocker
[4] http://refactormycode.com/codes/recent/ruby
Forum for help on refactoring in Ruby
[5] http://refactormycode.com/codes/recent/java
Forum for help on refactoring in Java
[6] http://www.slideshare.net/jweiss/refactoring-ruby-on-rails-applications
[7] http://railsforum.com/viewtopic.php?id=699
An exemple of refactoring Ruby on Rails
[8] http://www.jetbrains.com/ruby/features/ruby_ide.html#refactorings
The only IDE which support Refactoring for Ruby on Rails