refactoring improving the design of existing code atakan Şimşek e1298306

28
REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Post on 21-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

REFACTORING

Improving the Design of Existing Code

Atakan Şimşek e1298306

Page 2: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Outline Definition of Refactoring Why&When Refactor? Refactoring Steps Refactoring Examples Bad Smells

Definiton Solution

Conclusion

Page 3: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Definition

Refactoring (noun):

Improving the design of existing code

without changing the code's observable

behavior.

Page 4: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Some Definitions [Fowler] a change made to the

internal structure of software to make it easier to understand cheaper to modifywithout changing its observable behavior

[Beck] Improving the design after it has been written.

Page 5: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Why Refactor ?

Easier to understand It becomes easy for others to understand.

To find the bugs Finding the Bugs in the program.

Page 6: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

To reduce Software maintenance costs

To faciliate Future changes

Page 7: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

To program faster Code complexity tends to increase

rapidly over time(Patching new functionalities)

Development slows down

Refactoring helps keeping complexity under control

Development proceed more rapidly

Page 8: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

When to Refactor ? When you add a function

Helps you to understand the code when modifying.

Sometimes the existing design does not allow you to easily add the feature.

When you need to fix a bug If it is difficult to trace an error:

Code was not clear enough for you to see the bug in the first place.

Page 9: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

When you do a Code Review

When you detect a “bad smell” (an indication that something is wrong) in the code

Page 10: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Refactor? or NOT?

Code does not work : NOT

Code has some bugs : REFACTOR

Very Close to Deadline : NOT

There are bad smells : REFACTOR

Page 11: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Bad Smells

Bad smell Proposed refactoring

Duplicated Code Extract Class

Long Method Extract Method

Large Class Extract SubclassExtract Interface

Long Parameter List Replace Parameter with Method

Lazy Class Inline Class

Page 12: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Where Can We Use?

COSE

Producer Side

Consumer Side

Page 13: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Conditions of Refactoring Refactoring implies working

incrementally Making changes to the program in

small steps In between run the unit tests

regularly

If you make a mistake it's easy to back out.

Page 14: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Steps to Refactoring Analysis, helps to identify the code

Identify problems in code by review using bad smells of code

Introduce a refactoring

Test

Page 15: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Refactorings Add Parameter Change Association Reference to value Value to reference Collapse hierarchy Consolidate

conditionals Procedures to objects Decompose

conditional Encapsulate collection Encapsulate downcast Encapsulate field Extract class

Extract Interface Extract method Extract subclass Extract superclass Form template method Hide delegate Hide method Inline class Inline temp Introduce assertion Introduce explain

variable Introduce foreign method

Page 16: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Refactoring examples

Change Association Encapsulate Field Replace Number with Constants Compose Conditions Extract Class

Page 17: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Bi-directional Association to Unidirectional

We have a two-way association but one class no longer needs features from the other.

Page 18: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Self Encapsulate Field Create getting and setting methods for the

field and use only those to access the field.

private int _low, _high;boolean includes (int arg) {

return arg >= _low && arg <= _high;

}

private int _low, _high;boolean includes (int arg){

return arg >= getLow() && arg <= getHigh();

}int getLow() {return _low;}int getHigh() {return _high;}

Page 19: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Replace Magic Number with Symbolic Constant 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 20: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Consolidate conditionals

When we have a complicated conditional (if-then-else) statement.

double disabilityAmount()

{

if (_seniority < 2) return 0;

if (_monthsDisabled > 12)

return 0;

if (_isPartTime) return 0

// compute the disability amount

double disabilityAmount()

{

if (isNotEligableForDisability())

return 0;

// compute the disability

amount

Page 21: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Extract ClassYou have one class doing work that should

be done by two. Create a new class and move the relevant fields

Page 22: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Bad Smells in Code Duplicated Code Long Method Large Class Long Parameter

List Divergent Change Shotgun Surgery Feature Envy Data Clumps Primitive Obsession Switch Statements

Parallel Interface Hierarchies Lazy Class Speculative Generality Temporary Field Message Chains Middle Man Inappropriate Intimacy Incomplete Library Class Data Class Refused Bequest

Page 23: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Few solutions to Bad Smells

Duplicated Code: If you see the same code structure in

more than one place

Page 24: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Duplicated Code(cont.) Bugs copied as well Increase maintenance cost

solution: perform EXTRACT METHOD and invoke the code from both places.

Page 25: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Long Parameter ListAn object invokes a method, then passes the result as a parameter for a method.

solution: Use REPLACE PARAMETER with METHOD Remove the parameter and let the

receiver invoke the same method with sender.

Page 26: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Long Method

The longer a procedure is the more difficult it is to understand.

solution: perform EXTRACT METHOD

Page 27: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

Conclusion:

Refactoring is useful to any program that has at least one of the following shortcomings:

Programs that are hard to read Programs that have bad smells Programs that require additional

behavior

Page 28: REFACTORING Improving the Design of Existing Code Atakan Şimşek e1298306

References:

More Information can be found http://www.refactoring.com/

http://www.refactoring.be/

Refactoring: Improving the Design of Existing Code By Martin Fowler