1 XP Extreme Programming Introduction into Refactoring Refactoring

  • Slides: 22
Download presentation
1 XP: Extreme Programming Introduction into Refactoring, Refactoring to Patterns and Code Quality Refactoring

1 XP: Extreme Programming Introduction into Refactoring, Refactoring to Patterns and Code Quality Refactoring Agile Software Development Lab Spring 2008 ROOTS

2 Overview Definition An Example in Eclipse Short Description Precondition of Refactoring Checklist Advantages

2 Overview Definition An Example in Eclipse Short Description Precondition of Refactoring Checklist Advantages of Refactoring Indicators for Refactoring are called „Bad Smells“ Agile Software Development Lab Spring 2008 ROOTS

3 Defintion What is Refactoring ? Refactoring (noun): a change made to the internal

3 Defintion What is Refactoring ? Refactoring (noun): a change made to the internal structure of software to make it easier to understand cheaper to modify without changing ist observable behavior. Refactor (verb): to restructure software by applying a series of refactorings. Goals: Better readability, comprehensibility • Better design • Better maintainability and reusability • Agile Software Development Lab Spring 2008 ROOTS

4 Short Description Better readability, comprehensibility Sourcecode is for humans ! Bytecode for the

4 Short Description Better readability, comprehensibility Sourcecode is for humans ! Bytecode for the machines. Some developers are proud of their cryptic code. Some developers think they write the code only for themself. To maintain, reuse or extend a programm the code must be understood before from the programmer. We want to make good software not good documentationpapers. To understand what the code does it must be good comprehensible. Comprehesible code doesn't need to be documented comprehensive. - Agile Software Development Lab Spring 2008 ROOTS

5 Short Description Better design Beginning with minimal or comprehensive Design Minimal (like in

5 Short Description Better design Beginning with minimal or comprehensive Design Minimal (like in XP): Design grows with the Code. To get a good design we need to refactor. Given Design (Traditional SE): Good Design is given before coding begins. Nice. But with evolution the design changes. Maybe then it is not more good. What to do to keep the good design ? Refactor after or before all changes. Yippiyeah Agile Software Development Lab Spring 2008 ROOTS

6 Short Description Better maintainability and reusability Maintainability and Reusability: Changing any behaviour or

6 Short Description Better maintainability and reusability Maintainability and Reusability: Changing any behaviour or adding new functionalities become much easier tasks if you understood the code fast and when the design is good. Important: When a feature has to be added to a program, if the code is not structured in a convenient way to add the feature, first refactor the program to make it easy to add the feature, then add the feature. Agile Software Development Lab Spring 2008 ROOTS

7 Benefits of refactoring 1. Improved Design 2. Better understanding of the code 3.

7 Benefits of refactoring 1. Improved Design 2. Better understanding of the code 3. Better bug detection 4. Faster development! Agile Software Development Lab Spring 2008 ROOTS

8 Simplicity is Code Quality Everything we write must n n Run all the

8 Simplicity is Code Quality Everything we write must n n Run all the tests Express every idea that we need to express Say everything once and only once Have the minimum number of classes and methods consistent with the above (Ron Jeffries et al. : Extreme Programming Installed) Agile Software Development Lab Spring 2008 ROOTS

9 Bad Smells In the community of computer programming, code smell is any symptom

9 Bad Smells In the community of computer programming, code smell is any symptom that indicates something may be wrong. It generally indicates that the code should be refactored or the overall design should be reexamined. The term appears to have been coined by Kent Beck on Wards. Wiki. Different Kinds of Bad Smells u u u Duplicated code Long Method Large Class Switch Statements Temporary Field …. Bad Smells in Code by Kent Beck and Martin Fowler Agile Software Development Lab Spring 2008 ROOTS

10 Refactoring-Catalog è Composition of Methodes u Extract Method u Inline Method u Replace

10 Refactoring-Catalog è Composition of Methodes u Extract Method u Inline Method u Replace Temp with Query u Inline Temp u Split Temporary Variable u Remove Assignments to Parameters u Replace Method with Method Object u. . . Agile Software Development Lab Spring 2008 ROOTS

11 Extract Method How to indicate? u Code-Blocks that are logically related to each

11 Extract Method How to indicate? u Code-Blocks that are logically related to each other How to handle? u Replace with a well-named methode. void print. Owing(double amount) { print. Banner(); // print details System. out. println(“name“+_name); System. out. println(“amount“+ amount); } void print. Owing (double amount) { print. Banner(); print. Details(amount); } void print. Details (double amount) { System. out. println (“name“+_name); System. out. println (“amount“+ amount); } Agile Software Development Lab Spring 2008 ROOTS

12 Steps Defining new and well-named methodes u always „private“ Copy the Code Searching

12 Steps Defining new and well-named methodes u always „private“ Copy the Code Searching for the local variables in extracted code u Variables that will be used just in new methodes è local variables of new methodes u Variables which are changed in new methodes and will be used in old methodes è If only one: give it back as the result of new method è more than one: Parts that can not be extracted! („Replace Temp with Query“ or try to „Split Temp Variable“) u Variables that will be read in new methodes è Parameters of new methodes Agile Software Development Lab Spring 2008 ROOTS

13 Steps(2) Compiling in original methode u Replacing the extracted code by calling the

13 Steps(2) Compiling in original methode u Replacing the extracted code by calling the new methode u Deleting of the delclaration of local variables which have use anymore Compiling Testing Agile Software Development Lab Spring 2008 ROOTS

14 Example: no local variables void print. Owing(double amount) { Enumeration e = :

14 Example: no local variables void print. Owing(double amount) { Enumeration e = : orders. elements(); double outstanding = 0. 0; // print banner System. out. println("***********"); System. out. println("*** Customer owes ****"); System. out. println("***********"); // calculate outstanding while (e. has. More. Elements()) { Order each = (Order) e. next. Element(); outstanding += each. get. Amount(); // print details System. out. println(“name“+ _name); System. out. println(“amount“+ outstanding); } è Extraction of code for print banner Agile Software Development Lab Spring 2008 ROOTS

15 Example: no local variables void print. Owing(double amount) { Enumeration e = :

15 Example: no local variables void print. Owing(double amount) { Enumeration e = : orders. elements(); double outstanding = 0. 0; print. Banner(); private void print. Banner() { System. out. println("***********"); System. out. println("*** Customer owes ****"); System. out. println("***********"); } // calculate outstanding while (e. has. More. Elements()) { Order each = (Order) e. next. Element(); outstanding += each. get. Amount(); // print details System. out. println(“name“+ _name); System. out. println(“amount“+ outstanding); } è Extraction of code for print banner u Local variable which is not altered („outstanding“) Agile Software Development Lab Spring 2008 ROOTS

16 Local variable which is not altered void print. Owing(double amount) { Enumeration e

16 Local variable which is not altered void print. Owing(double amount) { Enumeration e = : orders. elements(); double outstanding = 0. 0; print. Banner(); // calculate outstanding while (e. has. More. Elements()) { Order each = (Order) e. next. Element(); outstanding += each. get. Amount(); print. Details(outstanding); } private void print. Details(double outstanding) { System. out. println(“name“+ _name); System. out. println(“amount“+ outstanding); } è Extraction of codes for calculation u Local variable , which will be altered and finally used(„outstanding“) u Local variable , which will be altered and will not be used anymore „e“ Agile Software Development Lab Spring 2008 ROOTS

17 Example: Local variable wchich will be altered void print. Owing(double amount) { Enumeration

17 Example: Local variable wchich will be altered void print. Owing(double amount) { Enumeration e = : orders. elements(); double outstanding = 0. 0; print. Banner(); outstanding = get. Outstanding(); print. Details(outstanding); } private double get. Outstanding() { Enumeration e = orders. elements(); double outstanding = 0. 0; while (e. has. More. Elements()) { Order each = (Order) e. next. Element(); outstanding += each. get. Amount(); } return outstanding; } è Extracting code for calculation u If local variable is assigned before in original method Agile Software Development Lab Spring 2008 ROOTS

18 Exapmle : local variable that was altered even before void print. Owing(double amount)

18 Exapmle : local variable that was altered even before void print. Owing(double amount) { double outstanding = amount *1. 2; print. Banner(); outstanding = get. Outstanding(outstanding); private double get. Outstanding(double start. Value) { Enumeration e = orders. elements(); double result = start. Value; print. Details(outstanding); while (e. has. More. Elements()) { Order each = (Order) e. next. Element(); result += each. get. Amount(); } } return result; } è Now are more Rafactorings possible u twice „inline temp“ for assigning to local variable „outstanding“ è In this way we can eliminate „outstanding“ from „print. Owing“-Methode Agile Software Development Lab Spring 2008 ROOTS

19 Example: Elimination of „outstanding“ void print. Owing(double amount) { double outstanding = amount

19 Example: Elimination of „outstanding“ void print. Owing(double amount) { double outstanding = amount *1. 2; print. Banner(); outstanding = get. Outstanding(outstanding); print. Details(outstanding); } Agile Software Development Lab Spring 2008 ROOTS

20 Example : End state of print. Owing() void print. Owing(double amount) { print.

20 Example : End state of print. Owing() void print. Owing(double amount) { print. Banner(); print. Details(get. Outstanding(amount *1. 2)); } Agile Software Development Lab Spring 2008 ROOTS

21 Overview Refactoring Tools Commercial Together since Version 5. 5 Free Smalltalk Refactoring Browser

21 Overview Refactoring Tools Commercial Together since Version 5. 5 Free Smalltalk Refactoring Browser u Only a few refactorings u www. togethersoft. com j. Factor u Plug-in for JBuilder and Visual. Age for Java u Extensive functionality u www. instantiations. com/jfactor/ IDEA u www. intellij. com/idea/ u IDE with builtin refactorings JBuilder since version 6 u Not tested yet Retool u New u www. chive. com Agile Software Development Lab Spring 2008 u First tool at all u Very powerfull u http: //chip. cs. uiuc. edu/users/brant/Refactory/ Xrefactory u Plug-in for Emacs u http: //www. xref-tech. com/speller Java. Refactor u Plug-in for j. Edit u http: //plugins. jedit. org/plugins/Java. Refactor Eclipse IDE u Preview, Veto and Undo of changes u Extract Method, Rename Method, . . . u www. eclipse. org ROOTS

22 Refrences Slides of SWT Lecture (Günter Kniesel) Slides of Exterme Programming for Nanjing

22 Refrences Slides of SWT Lecture (Günter Kniesel) Slides of Exterme Programming for Nanjing Univesity(Günter Kniesel) Osborne - Java 2 --Complete Reference (5 th Ed 2002) Exapmles of Kent Beck and Martin Fowler From Berkley University http: //www. berkeley. edu/ http: //sis 36. berkeley. edu/projects/streek/agile/bad-smells-in-code. html Agile Software Development Lab Spring 2008 ROOTS