refactoring et al
TRANSCRIPT
Refactoring et alNaveen Muguda
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:
Software,Composition
Software is composed by combination of componentscomponents can be● methods● objects● functions● classes
Refactoring
to restructure software without changing its observable behavior.
change from 2*6 to 3*4, while the product remains 12
Why?
Understand your code better
4398046511104 => 2 ^ 42
Why?
Improve your design of your software
21600 = 2^5 * 3 ^ 3 * 5 ^ 2
why?
find bugs
reasoning about factors helps you to find bugs
is a number power of 2? !(x & (x - 1))
refactoring in the real world
Refactor (verb): to restructure software by applying a series of refactorings with-out changing its observable behavior.
refactoring in Test Driven Design
red green refactor
Changeneed to Change doesn’t change
● business evolves● rules change● new users, new use cases● more data● …..
Two Hats
Software Development has two distinct activities1. adding functionality2. refactoring
Two Hats: Metaphor
(x + 1) (x + 3) + 2 x^2 + 3x - 1 => 3 x^2 + 7x + 2 => (3x + 1) (x + 2)
Two hats ...
swap hats frequently. add new functionality, realize this would be much easier if the code were structured differently. refactor for a while.
Bad Smells
● Wrong Name
● Long Method● Large Class● Long Parameter List
Bad Smells
● Duplicated code● Switch statement
...
● Divergent Change● Shotgun Surgery
Refactorings
● extract method
● Introduce Explaining Variable● Split Temporary Variable● Remove Assignments to Parameters● Replace Magic Number with Symbolic
Constant
...
● Move Method● Extract Class● Move Field
...
● encapsulate field● encapsulate collections
Design Patterns
Some problems keep re-appearingproviding a name increases design vocabularyraises level of abstractioneasier for communicationproblem/solution gets well understood
Proxy
Provides a place holder for another object applications● remote proxy● protection proxy● smart proxy
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
State
allows objects to change behavior when its internal state changes
Participants● State● Concrete State Subclasses