refactoring

16
Clean Code Refactoring 1

Upload: behrouz-bakhtiari

Post on 09-May-2015

1.245 views

Category:

Education


0 download

DESCRIPTION

This is my presentation in Tabriz open space meeting about Refactoring.

TRANSCRIPT

Page 1: Refactoring

Clean CodeRefactoring

1

Page 2: Refactoring

Refactoring

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

2

Page 3: Refactoring

Refactoring

What is Refactoring?

Martin Fowler : "a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior“.

William C. Wake : "Refactoring is the art of safely improving the design of existing code“.

3

Page 4: Refactoring

Refactoring

The goal of refactoring is NOT to add new functionality

The goal is refactoring is to make code easier to maintain in the future

The process of refactoring involves the removal of duplication, the simplification of complex logic, and the clarification of unclear code.

4

Page 5: Refactoring

Refactoring

When Should You Refactor?

• Refactor When You Add Function

• Refactor When You Need to Fix a Bug

• Refactor As You Do a Code Review

5

Page 6: Refactoring

Refactoring

The Refactoring Cycle

Choose the worst smell

Select a refactoring that will address the smell

Apply the refactoring

6

Page 7: Refactoring

RefactoringBad 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 Inheritance Hierarchies• Lazy Class• Speculative Generality• Temporary Field• Message Chains• Middle Man

7

Page 8: Refactoring

RefactoringBad Smells in Code

• Inappropriate Intimacy• Alternative Classes with Different Interfaces• Incomplete Library Class• Data Class• Refused Bequest• Comments

8

Page 9: Refactoring

RefactoringExtract Method

void printOwing() { printBanner(); //print details System.out.println ("name: " + _name); System.out.println ("amount " + getOutstanding()); }

void printOwing() { printBanner(); //print details System.out.println ("name: " + _name); System.out.println ("amount " + getOutstanding()); }

void printOwing() { printBanner(); printDetails(getOutstanding()); } Void printDetails (double outstanding) { System.out.println ("name: " + _name); System.out.println ("amount " + outstanding); }

Reusabilit

y

Readability

High Cohesion

Polymorphism

9

Page 10: Refactoring

RefactoringMove Method

10

Page 11: Refactoring

RefactoringMove Method

low CouplingHigh Cohesion

11

Page 12: Refactoring

RefactoringEncapsulate Field

you should never make your data public. When you make data public, other objects can change and access data values without the owning object's knowing about it.

12

Page 13: Refactoring

RefactoringExtract Interface

13

Page 14: Refactoring

RefactoringRemove Parameter

A parameter is no longer used by the method body. Remove it.

14

Page 15: Refactoring

RefactoringReplace Nested Conditional with Guard Clauses

A method has conditional behavior that does not make clear the normal path of execution.Use guard clauses for all the special cases.

Readability

15