Slide 1

Slide 1 text

Refactoring

Slide 2

Slide 2 text

What is refactoring?

Slide 3

Slide 3 text

"A change to the system that leaves its behavior unchanged, but enhances some nonfunctional quality – simplicity, flexibility, understandability, performance" Kent Beck, Extreme Programming Explained

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Why refactoring?

Slide 6

Slide 6 text

Prevent “design decay” Clean up messes in the code Simplify the code

Slide 7

Slide 7 text

Increase readability and understandability Find bugs Reduce debugging time

Slide 8

Slide 8 text

How to refactor?

Slide 9

Slide 9 text

1. Make sure your tests pass 2. Find some code that “smells” 3. Determine how to simplify this code 4. Make the simplifications 5. Run tests to ensure things still work correctly 6. Repeat the simplify/test cycle until the smell is gone

Slide 10

Slide 10 text

When to refactor?

Slide 11

Slide 11 text

When you add functionality When you learn something about the code When you fix a bug When the code smells

Slide 12

Slide 12 text

“Here’s a guideline Don Roberts gave me: The first time you do something, you just do it. The second time you do something similar, you wince at the duplication, but you do the duplicate thing anyway. The third time you do something similar, you refactor.” The Three Rule, Marting Fowler

Slide 13

Slide 13 text

When NOT to refactor?

Slide 14

Slide 14 text

When the tests aren’t passing When you should just rewrite the code When you have impending deadlines

Slide 15

Slide 15 text

“The other time you should avoid refactoring is when you are close to a deadline. At that point the productivity gain from refactoring would appear after the deadline and thus be too late” Marting Fowler

Slide 16

Slide 16 text

Code Smells

Slide 17

Slide 17 text

Indicators that something may be wrong in the code Can occur both in production code and test code

Slide 18

Slide 18 text

The Bloaters -Long Method -Large Class -Primitive Obsession -Long Parameter List -DataClumps

Slide 19

Slide 19 text

The Object-Orientation Abusers -Switch Statements -Temporary Field -Refused Bequest -Alternative Classes with Different Interfaces

Slide 20

Slide 20 text

The Change Preventers -Divergent Change -Shotgun Surgery -Parallel Inheritance Hierarchies

Slide 21

Slide 21 text

The Dispensables -Lazy class -Data class -Duplicate Code -Dead Code, -Speculative Generality

Slide 22

Slide 22 text

The Couplers -Feature Envy -Inappropriate Intimacy -Message Chains -Middle Man

Slide 23

Slide 23 text

Refactors