agile korea 2013 유석문

34
삶삶 삶삶삶삶 삶삶 삶삶삶삶 (Refactoring) 삶삶삶

Upload: sangchel-hwang

Post on 17-May-2015

1.198 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Agile korea 2013 유석문

삶이 편해지는 쉬운 리팩토링

(Refactoring)유석문

Page 2: Agile korea 2013 유석문

http://xaxor.com/bizarre/23678-dangerous-work-part-4-construc-tion-workers.html

Refactoring?!?!!

Page 3: Agile korea 2013 유석문

What is Refactoring?

A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior. — Refactoring, Martin Fowler, page 53.

Refactoring typically involves• Removing duplicated or dead code• Simplifying complex code• Clarifying unclear code

Page 4: Agile korea 2013 유석문

Refactoring is revision

All good writing is based upon revision. — Jacques Barzun, Simple & Direct, 4th Edition

Revision means to re-see or see again.

Page 5: Agile korea 2013 유석문

How to Refactor

The secret to successful refactoring is to take baby steps.

When refactoring, your ultimate goal is not to:

• break anything• make things worse• introduce new behavior

Page 6: Agile korea 2013 유석문

How to Refactor

• Verify that all automated tests (unit tests) pass

• Decide what code to change

• Implement one or more refactorings carefully

• Run the unit tests whenever you wish to confirm that

changes have not altered system behavior

• Repeat until the refactoring is complete or revert to an

earlier

state

Page 7: Agile korea 2013 유석문

Refactoring?!?!!

http://daily-pins.com/there-was-a-spider-its-gone-now/

Page 8: Agile korea 2013 유석문

A Chicken and Egg Problem?

Many people, when they first encounter the concept of unit testing and

refactoring legacy code, consider it a chicken-and-egg conundrum: Can't do one without having the other.

Unit Testing Refac-toring

Page 9: Agile korea 2013 유석문

Start with the Universals

Always start refactoring work with the three universals.

1. Reformat and Commit. Establish a baseline for later

diffs.

2. Eliminate the Noise. A refactored file always minimizes

noise and maximizes signal.

3. Get To Testability. We're going to need some tests. Can

we even start to test our class?

Page 10: Agile korea 2013 유석문

Consolidate Conditional Expression

Clarity

"Any fool can write code that a computer can understand.Good programmers write code that humans can understand."— Martin Fowler, Refactoring

Page 11: Agile korea 2013 유석문

Extract Hierarchy

Highly CohesiveMethods do only one thing and classes have a single, clear responsi-bility.

Page 12: Agile korea 2013 유석문

Extract Superclass

Duplication FreeA program should express each idea once and only once. Code may notalways appear to be identical and yet may be expressing the same logicand information. Duplicate code may diverge to become out of sync

Page 13: Agile korea 2013 유석문

Hide Method

EncapsulatedA form of information hiding, code that is encapsulateddoes not expose data or behavior that should be invisible

Page 14: Agile korea 2013 유석문

Inline Method

SimpleCode reflects the shortest path to a solution and incorporatesonly enough complexity to handle its known responsibilities.Simple code is easier to maintain and evolve.

Page 15: Agile korea 2013 유석문

Caller Creates

When you need to create a new class or method that will be invoked by a

caller, don't perform the creation where the new code will reside (e.g. in a

new file or within the class that will contain the new method).

Instead, declare the to-be-created class/method where it will be used, i.e.

in the caller code.

This will allow you to use IDE tools to automatically generate the exact

code needed by the client, instead of retrofitting client code to the new

class/method after creation.

Page 16: Agile korea 2013 유석문

Automated Refactorings

Page 17: Agile korea 2013 유석문

Rejected Parameter

To avoid passing a parameter to code you want to extract, you must first reject the parameter from the extraction.

The video on the next page presents three different ways to reject a parameter:1. Inline the unwanted parameter.2. Move the unwanted parameter out of the extraction block.3. Turn the unwanted parameter into a field so it can be refer-

enced from the extracted method instead of being passed as a parameter.

Page 18: Agile korea 2013 유석문

Move (Caller Swap)

Move responsibilities from a caller to a parameterby calling the parameter with the original caller as its pa-

rameter.

Page 19: Agile korea 2013 유석문

Encapsulated Dependency

Before moving code to a new class, first encapsu-late dependencies, like fields.

Page 20: Agile korea 2013 유석문

Remove Duplication by Extract Method - Before

Page 21: Agile korea 2013 유석문

Remove Duplication - After

Page 22: Agile korea 2013 유석문

Sample

Page 23: Agile korea 2013 유석문

Removing A Long Method Smell

Page 24: Agile korea 2013 유석문

Removing A Long Method Smell (Composed Method)

Page 25: Agile korea 2013 유석문

Removing Unnecessary hierarchy

Page 26: Agile korea 2013 유석문

Removing Unnecessary hierarchy

Page 27: Agile korea 2013 유석문

Change hierarchy

A Map holds keys and val-ues, whereas List and Set

just hold objects.

Page 28: Agile korea 2013 유석문

Duplicated Fields & Methods

Duplicated Fields & Methods in List and Set

AbstractCollection's method, addAll(...), con-tains the smells Long Method, Duplicated Code , Switch Statement and Alternative Classes With Different Interfaces

Page 29: Agile korea 2013 유석문

Duplicated Fields & Methods

Page 30: Agile korea 2013 유석문

Don’t Fix Bugs during Refactoring

A List should allow duplicates, yet in AbstractCollection your addAll(...) method

is checking whether the element is already

contained before adding it.

There is a time and place to fix bugs and it is

not during refactoring.

Page 31: Agile korea 2013 유석문

Primitive Obsession In Map

Map suffers from the Primitive Obsession smell because it wastes too much code manipulatingthe keys and values arrays.

Page 32: Agile korea 2013 유석문

Scaffolding

Builders make changes to buildings by first putting up scaffolding to make their workeasier and safer.

Extract Method refactoring to produce:

getValueAt(...)

setValueAt(...)

When you no longer need your scaffolding, you can remove it by

applying the Inline Method refactoring.

Page 33: Agile korea 2013 유석문

A Temporary Field In Map

Map has a field called indexWhereKeyFound that is a prime example of the Temporary Field Smell.

Page 34: Agile korea 2013 유석문

Thank you.