refactoring cristescu marilena. definitions loose usage: reorganize a program(or something) as a...
TRANSCRIPT
RefactoringCristescu Marilena
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
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
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
When should we Refactor?
• To add new functionality• To find bugs• For code reviews• For TDD (Test, Code, Refactor)
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
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
Common Code Smells
• Duplicated code• Feature Envy• Comments• Long Method• Long Parameter List• Switch Statements• Improper Naming
Some Refactorings
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.
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.
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;
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.
Rename Method
• The name of a method does not reveal its purpose.
• Change the name of the method.
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.
Pull Up Field
• Two subclasses have the same field.• Move the field to the superclass.
Separate Domain from Presentation
• You have GUI classes that contain domain logic.• Separate the domain logic into separate domain classes
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
Bibliography
• Martin Fowler: Refactoring: Improving the Design of Existing Code, Addison–Wesley.
• http://en.wikipedia.org/wiki/Database_refactoring