User talk:Sheng yi: Difference between revisions
No edit summary |
|||
Line 6: | Line 6: | ||
===List of main refactoring=== | ===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. | |||
{| | {| | ||
|Merge Class Parts | |Merge Class Parts |
Revision as of 01:16, 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 iner-structure without modify the outer form. 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.
Merge Class Parts |
Convert Local Variable to Field |
Encapsulate Field |
Extract Method |
Inline Class |
Inline Method |
Move Field |
Move Method |
Rename Class |
Rename Field |
Rename Local Variable |
Rename Method |
Replace Temporary Variable with Query |
Split Temporary Variable |
Some Ruby Example of refactoring
wait for Julian
Some Java Example of refactoring
wait for Julian
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 |
The Ruby refactoring tools in Eclipse
Ruby vs Java in terms of refactoring
A function passed as an argument is called a callback, since it is "called back" by the other function.[2] In general a callback is a piece of "your" code you ask "some other piece" of code to invoke in certain events, e.g. some code to be executed if a button is clicked on the screen.
In most languages you specify a call back by passing the address of your subroutine to the system you're requesting the callback from.
In RUBY callback is implemented using blocks. A nameless block of code may be passed as an argument to a method for implementation of callback.
Blocks and Closures
Blocks are basically nameless functions. You can pass a nameless function to another function, and then that function can invoke the passed-in nameless function. For example, a function could perform iteration by passing one item at a time to the nameless function. In Ruby, any method can be called with a block as an implicit argument. Inside the method, you can call the block using the yield keyword with a value. We can create a closure out of a block. A closure is a nameless function. You can pass around a nameless function object, the closure, to another method to customize the behavior of the method. As another example, if you have a sort method to sort an array or list, you can pass a block to define how to compare the elements. This is not iteration. This is not a loop. But it is using blocks. A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away.[3]
Example
In RUBY any chunk of code between braces {} or between do ... end is a block. This can be associated with a function call also.
Eg.{ puts "Hello" } <- block
def about_block puts "In the function" yield end about_block {puts "This is the block"} <- Associated with a function call
Output:
In the function
This is the block
"yield" is the keyword which invokes the block of code associated with the function call.
Detailed Explanation of the program : The function about_blocks is defined and called at the end. When it is called the control passes to the first "puts" in the function. When the next line is executed (yield) it refers to the block associated with the function call. So, it prints "This is the block"
Functor
A functor is nothing more than an object that behaves like a function (which is the case in RUBY - everything is an object).[2]
Approach to the Question
Suppose we need to associate actions to a particular button so that it does a required function. Eg. In a form if we press CLEAR, it should clear the form and SUBMIT should submit the form. Ruby’s blocks are a convenient way to do this. We assume that on pressing the button a callback method, button_pressed, will be invoked. The obvious way of adding functionality to these buttons is to create subclasses of Button(which is the class) and have each subclass implement its own button_pressed method. Eg.
class FormButton < Button def initialize super("Submit") # invoke Button's initialize end def button_pressed # do submit actions... end end submit_button = FormButton.new
This will lead to a large number of subclasses. We can fix this problem using blocks in RUBY. pressbutton = PressButton.new
class FormButton < Button def initialize(label, &action) super(label) @action = action end def button_pressed @action.call(self) end end submit_button = FormButton.new("Submit") { pressbutton.submit } reset_button = FormButton.new("Clear") { pressbutton.reset }
The key to all this is the second parameter to FormButton#initialize. If the last parameter in a method definition is prefixed with an ampersand (such as &action), Ruby looks for a code block whenever that method is called. That code block is converted to an object of class Proc and assigned to the parameter. You can then treat the parameter as any other variable. When the callback method button_pressed is invoked, we use the Proc#call method on that object to invoke the block.
Comparisons of Smalltalk and Ruby code
Smalltalk
foo | xs | xs := #(1 2 3 4). xs do: [:x | ^x]. ^0 bar Transcript show: (self foo) "prints 1"
In Smalltalk, ^ will return from method foo, and not just from the closure. do: is defined as a plain method, and there is nothing special about it.
foo ^[ x: | ^x ] bar | f | f := self foo. f value: 123 "error!"
When block returned by method foo is invoked, it attempts to return a value from foo. Since the call to foo has already completed, this operation results in an error.
Ruby
def foo f = Proc.new { return "return from foo from inside proc" } f.call # control leaves foo here return "return from foo" end def bar f = lambda { return "return from lambda" } f.call # control does not leave bar here return "return from bar" end puts foo # prints "return from foo from inside proc" puts bar # prints "return from bar"
It allows the programmer to choose the way he wants "return" to be captured.Both Proc.new and lambda in this example are ways to create a closure.[4]
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