refactoring cristescu marilena. definitions loose usage: reorganize a program(or something) as a...
TRANSCRIPT
![Page 1: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/1.jpg)
RefactoringCristescu Marilena
![Page 2: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/2.jpg)
Definitions
• Loose Usage: Reorganize a program(or something)
• As a noun: a change made to the internal structure of some software to make it easier to understand and cheaper to modify, without changing the observable behavior of that software
• As a verb: the activity of restructuring software by applying a series of refactorings without changing the observable behavior of that software
![Page 3: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/3.jpg)
What is Refactoring?
• A series of small steps, each of which changes the program’s internal structure without changing its external behavior – Martin Fowler
• Verify no change in external behavior by:– Testing– Using the right tool – IDE– Formal code analysis by tool– Being very, very careful
![Page 4: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/4.jpg)
Why do we Refactor?• Helps us deliver more business value faster• Improves the design of our software:
– Easier to maintain and understand– Easier to facilitate change– More flexibility– Increase re-usability
• Minimizes technical dept• Keep development at speed• To make the software easier to understand• To help find bugs• To “Fix broken windows” – Pragmatic Programmers
![Page 5: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/5.jpg)
When should we Refactor?
• To add new functionality• To find bugs• For code reviews• For TDD (Test, Code, Refactor)
![Page 6: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/6.jpg)
Team Techniques• Encourage refactoring culture
– Nobody gets things right the first time– Nobody can write clear code without reviews– Refactoring is progress
• Provide sound testing base– Tests are essential for refactoring– Build system and run tests daily
• Pair Programming– Two programmers working together can be quicker than
working separately– Refactor with the class writer and a class user
![Page 7: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/7.jpg)
How do we Refactor?
• We look for Code-Smells• Things that we suspect are not quite
right or cause us severe pain if we do not fix
![Page 8: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/8.jpg)
Common Code Smells
• Duplicated code• Feature Envy• Comments• Long Method• Long Parameter List• Switch Statements• Improper Naming
![Page 9: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/9.jpg)
Some Refactorings
![Page 10: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/10.jpg)
Move Method• A method is, or will be, using or used by more features of
another class than the class on which it is defined.• Create a new method with a similar body in the class it uses
most. Either turn the old method into a simple delegation, or remove it altogether.
![Page 11: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/11.jpg)
Extract Class
• You have one class doing work that should be done by two.
• Create a new class and move the relevant fields and methods from the old class into the new class.
![Page 12: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/12.jpg)
Replace Magic Number with Symbolic Constant
• You have a literal number with a particular meaning.
• Create a constant, name it after the meaning, and replace the number with it..
double potentialEnergy(double mass, double height) {
return mass * 9.81 * height;}
double potentialEnergy(double mass, double height) {return mass * GRAVITATIONAL_CONSTANT * height;}
static final double GRAVITATIONAL_CONSTANT = 9.81;
![Page 13: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/13.jpg)
Replace Subclass with Fields
• You have subclasses that vary only in methods that return constant data.
• Change the methods to superclass fields and eliminate the subclasses.
![Page 14: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/14.jpg)
Rename Method
• The name of a method does not reveal its purpose.
• Change the name of the method.
![Page 15: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/15.jpg)
Parameterize Method
• Several methods do similar things but with different values contained in the method body.
• Create one method that uses a parameter for the different values.
![Page 16: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/16.jpg)
Pull Up Field
• Two subclasses have the same field.• Move the field to the superclass.
![Page 17: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/17.jpg)
Separate Domain from Presentation
• You have GUI classes that contain domain logic.• Separate the domain logic into separate domain classes
![Page 18: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/18.jpg)
Database Refactoring• A database refactoring is a simple change
to a database schema that improves its design while retaining both its behavioral and informational semantics
• Database Smells:– stored code: Monster Procedures, Spaghetti,
Duplication, IF-ELSE overuse, low cohesion– Database schema: Multi-purpose table /
column, Redundant data, Tables with many columns / rows, Lack of constraints
![Page 19: Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some](https://reader035.vdocument.in/reader035/viewer/2022062801/56649e705503460f94b6e4c6/html5/thumbnails/19.jpg)
Bibliography
• Martin Fowler: Refactoring: Improving the Design of Existing Code, Addison–Wesley.
• http://en.wikipedia.org/wiki/Database_refactoring