refactoring et al

27
Refactoring et al Naveen Muguda

Upload: naveenkumar-muguda

Post on 09-Apr-2017

301 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Refactoring et al

Refactoring et alNaveen Muguda

Page 2: Refactoring et al

FactorsFactors are numbers you can multiply together to get another number:

Example: 2 and 3 are factors of 6, because 2 × 3 = 6.

3 and 4 are factors of 12, because 3 × 4 = 12. Also 2 × 6 = 12 so 2 and 6 are also factors of 12.

In Algebra, factors are what you can multiply together to get an expression. (x+3) and (x+1) are factors of x2 + 4x + 3:

Page 3: Refactoring et al

Software,Composition

Software is composed by combination of componentscomponents can be● methods● objects● functions● classes

Page 4: Refactoring et al

Refactoring

to restructure software without changing its observable behavior.

change from 2*6 to 3*4, while the product remains 12

Page 5: Refactoring et al

Why?

Understand your code better

4398046511104 => 2 ^ 42

Page 6: Refactoring et al

Why?

Improve your design of your software

21600 = 2^5 * 3 ^ 3 * 5 ^ 2

Page 7: Refactoring et al

why?

find bugs

reasoning about factors helps you to find bugs

is a number power of 2? !(x & (x - 1))

Page 8: Refactoring et al

refactoring in the real world

Refactor (verb): to restructure software by applying a series of refactorings with-out changing its observable behavior.

Page 9: Refactoring et al

refactoring in Test Driven Design

red green refactor

Page 10: Refactoring et al

Changeneed to Change doesn’t change

● business evolves● rules change● new users, new use cases● more data● …..

Page 11: Refactoring et al

Two Hats

Software Development has two distinct activities1. adding functionality2. refactoring

Page 12: Refactoring et al

Two Hats: Metaphor

(x + 1) (x + 3) + 2 x^2 + 3x - 1 => 3 x^2 + 7x + 2 => (3x + 1) (x + 2)

Page 13: Refactoring et al

Two hats ...

swap hats frequently. add new functionality, realize this would be much easier if the code were structured differently. refactor for a while.

Page 14: Refactoring et al
Page 15: Refactoring et al

Bad Smells

● Wrong Name

● Long Method● Large Class● Long Parameter List

Page 16: Refactoring et al

Bad Smells

● Duplicated code● Switch statement

Page 17: Refactoring et al

...

● Divergent Change● Shotgun Surgery

Page 18: Refactoring et al

Refactorings

● extract method

● Introduce Explaining Variable● Split Temporary Variable● Remove Assignments to Parameters● Replace Magic Number with Symbolic

Constant

Page 19: Refactoring et al

...

● Move Method● Extract Class● Move Field

Page 20: Refactoring et al

...

● encapsulate field● encapsulate collections

Page 21: Refactoring et al
Page 22: Refactoring et al

Design Patterns

Some problems keep re-appearingproviding a name increases design vocabularyraises level of abstractioneasier for communicationproblem/solution gets well understood

Page 23: Refactoring et al

Proxy

Provides a place holder for another object applications● remote proxy● protection proxy● smart proxy

Page 24: Refactoring et al
Page 25: Refactoring et al

Singleton

● restricts instantiation to only one object● mechanism to access this object without

creating it● class level method to retrieve this object● constructor made private

Page 26: Refactoring et al

State

allows objects to change behavior when its internal state changes

Participants● State● Concrete State Subclasses

Page 27: Refactoring et al