Presentation is loading. Please wait.

Presentation is loading. Please wait.

Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some.

Similar presentations


Presentation on theme: "Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some."— Presentation transcript:

1 Refactoring Cristescu Marilena

2 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

3 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

4 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

5 When should we Refactor? To add new functionality To find bugs For code reviews For TDD (Test, Code, Refactor)

6 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

7 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

8 Common Code Smells Duplicated code Feature Envy Comments Long Method Long Parameter List Switch Statements Improper Naming

9 Some Refactorings

10 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.

11 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.

12 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;

13 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.

14 Rename Method The name of a method does not reveal its purpose. Change the name of the method.

15 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.

16 Pull Up Field Two subclasses have the same field. Move the field to the superclass.

17 Separate Domain from Presentation You have GUI classes that contain domain logic. Separate the domain logic into separate domain classes

18 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

19 Bibliography Martin Fowler: Refactoring: Improving the Design of Existing Code, Addison–Wesley. http://en.wikipedia.org/wiki/Database_refact oring


Download ppt "Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some."

Similar presentations


Ads by Google