开发者

What is a concrete visual example of refactoring?

开发者 https://www.devze.com 2023-04-05 01:56 出处:网络
I\'ve never used refactoring before (though I\'ve seen the button through an Eclipse dropdown menu), and while I\'ve read the Wikipedia page of what it does, it still doesn\'t provide me with 开发者_J

I've never used refactoring before (though I've seen the button through an Eclipse dropdown menu), and while I've read the Wikipedia page of what it does, it still doesn't provide me with 开发者_JAVA百科a concrete visual example of how it's done (of course I know that it's some way to automatically make code more organized).


Refactoring is not automatic ;) Most of the time its being done by hand, yet there are tools for automating simple edits.

How it looks ? JavaScript example:

var myArray = new Array();
myArray[0] = "apples";
myArray[1] = "bananas";
myArray[2] = "carrots";

becomes

var fruits = [ "apples", "bananas", "carrots" ];

For more complex stuff, look at Martin Fowler's example: http://codecourse.sourceforge.net/materials/Refactoring-A-First-Example.rtf


Do you really need a picture? The simplest refactoring is

"rename a variable (in a scope)"

Refactorings don't automatically make the code more organized; it requires input from you to determine what actions to take to rearrange the code to improve its structure. The "refactorings" will carry out your wishes automatically (e.g, do the correct renaming for the above).

Other standard refactorings including removing redundant code, lifting blocks of statements out of a large block to make a subroutine, etc. Mostly what these do is what you would do by hand. All the refactoring tools do is take common reorganizing activities, package them into actions you can invoke and automate them.


If you are looking for refactoring from eclipse IDE, it does following things:

1) Extract selected piece of code to a new method(Extract Method) 2) Extract selected variable to Local variable, constant etc For example:

myObject.getName()

is used at multiple places in your method then is better to

3) You can also move the code to super-class(Pull Up) or sub-class(Push Down)

Which essentially means you don't have to explicitly copy-paste code to do refactoring but the eclipse IDE does that for you.


If you are interested and like to get a good understanding of refactoring, I suggest you to pick a copy of Refactoring - Improving the Design of Existing Code by Martin Fowler.

Also refer the author's web site for visual diagrams regarding refactoring.

  1. http://refactoring.com/
  2. Catalog
0

精彩评论

暂无评论...
验证码 换一张
取 消