refactoring fest
DESCRIPTION
Materials by Naresh Jain from XP days, Kyiv, April 08. http://www.agileukraine.org/2008/03/xp-days.htmlTRANSCRIPT
![Page 1: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/1.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring FestNaresh Jain
1
![Page 2: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/2.jpg)
Licensed Under Creative Commons by Naresh Jain
Tutorial Schedule
What, Why, When and How... Refactor?
How to Refactor to Patterns
How to deal with Legacy Code?
Refactoring Hands-on session
2
![Page 3: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/3.jpg)
Licensed Under Creative Commons by Naresh Jain
Three Golden Rules
Once and only once [DRY]
Express intent
Tell, don’t ask
3
![Page 4: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/4.jpg)
Licensed Under Creative Commons by Naresh Jain
Definitions
4
![Page 5: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/5.jpg)
Licensed Under Creative Commons by Naresh Jain
Definitions
Loose Usage
4
![Page 6: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/6.jpg)
Licensed Under Creative Commons by Naresh Jain
Definitions
Loose Usage
Reorganize a program (or something)
4
![Page 7: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/7.jpg)
Licensed Under Creative Commons by Naresh Jain
Definitions
Loose Usage
Reorganize a program (or something)
As a noun
4
![Page 8: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/8.jpg)
Licensed Under Creative Commons by Naresh Jain
Definitions
Loose Usage
Reorganize a program (or something)
As a noun
a change made to the internal structure of some software to make it easier to understand and cheaper to modify, without changing the observable behavior of that software
4
![Page 9: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/9.jpg)
Licensed Under Creative Commons by Naresh Jain
Definitions
Loose Usage
Reorganize a program (or something)
As a noun
a change made to the internal structure of some software to make it easier to understand and cheaper to modify, without changing the observable behavior of that software
As a verb
4
![Page 10: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/10.jpg)
Licensed Under Creative Commons by Naresh Jain
Definitions
Loose Usage
Reorganize a program (or something)
As a noun
a change made to the internal structure of some software to make it easier to understand and cheaper to modify, without changing the observable behavior of that software
As a verb
the activity of restructuring software by applying a series of refactorings without changing the observable behavior of that software.
4
![Page 11: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/11.jpg)
Licensed Under Creative Commons by Naresh Jain
What is Refactoring?
5
![Page 12: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/12.jpg)
Licensed Under Creative Commons by Naresh Jain
What is Refactoring?
A series of small steps, each of which changes the program’s
internal structure without changing its external behavior - Martin Fowler
5
![Page 13: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/13.jpg)
Licensed Under Creative Commons by Naresh Jain
What is Refactoring?
A series of small steps, each of which changes the program’s
internal structure without changing its external behavior - Martin Fowler
Verify no change in external behavior by
Testing
Using the right tool - IDE
Formal code analysis by tool
Being very, very careful
5
![Page 14: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/14.jpg)
Licensed Under Creative Commons by Naresh Jain
What if you hear...
6
![Page 15: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/15.jpg)
Licensed Under Creative Commons by Naresh Jain
What if you hear...
We’ll just refactor the code to add logging support
6
![Page 16: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/16.jpg)
Licensed Under Creative Commons by Naresh Jain
What if you hear...
We’ll just refactor the code to add logging support
6
![Page 17: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/17.jpg)
Licensed Under Creative Commons by Naresh Jain
What if you hear...
We’ll just refactor the code to add logging support
Can you refactor the code so that it authenticates against LDAP instead of Database?
6
![Page 18: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/18.jpg)
Licensed Under Creative Commons by Naresh Jain
What if you hear...
We’ll just refactor the code to add logging support
Can you refactor the code so that it authenticates against LDAP instead of Database?
6
![Page 19: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/19.jpg)
Licensed Under Creative Commons by Naresh Jain
What if you hear...
We’ll just refactor the code to add logging support
Can you refactor the code so that it authenticates against LDAP instead of Database?
We have too much duplicate code, we need to refactor the code to eliminate duplication
6
![Page 20: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/20.jpg)
Licensed Under Creative Commons by Naresh Jain
What if you hear...
We’ll just refactor the code to add logging support
Can you refactor the code so that it authenticates against LDAP instead of Database?
We have too much duplicate code, we need to refactor the code to eliminate duplication
6
![Page 21: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/21.jpg)
Licensed Under Creative Commons by Naresh Jain
What if you hear...
We’ll just refactor the code to add logging support
Can you refactor the code so that it authenticates against LDAP instead of Database?
We have too much duplicate code, we need to refactor the code to eliminate duplication
This class is too big, we need to refactor it
6
![Page 22: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/22.jpg)
Licensed Under Creative Commons by Naresh Jain
What if you hear...
We’ll just refactor the code to add logging support
Can you refactor the code so that it authenticates against LDAP instead of Database?
We have too much duplicate code, we need to refactor the code to eliminate duplication
This class is too big, we need to refactor it
6
![Page 23: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/23.jpg)
Licensed Under Creative Commons by Naresh Jain
What if you hear...
We’ll just refactor the code to add logging support
Can you refactor the code so that it authenticates against LDAP instead of Database?
We have too much duplicate code, we need to refactor the code to eliminate duplication
This class is too big, we need to refactor it
Caching?
6
![Page 24: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/24.jpg)
Licensed Under Creative Commons by Naresh Jain
What if you hear...
We’ll just refactor the code to add logging support
Can you refactor the code so that it authenticates against LDAP instead of Database?
We have too much duplicate code, we need to refactor the code to eliminate duplication
This class is too big, we need to refactor it
Caching?
6
![Page 25: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/25.jpg)
Licensed Under Creative Commons by Naresh Jain
Origin
Ward Cunningham and Kent Beck
Smalltalk style
Ralph Johnson at University of Illinois at Urbana-Champaign
Bill Opdyke’s Thesis
ftp://st.cs.uiuc.edu/pub/papers/refactoring/opdyke-thesis.ps.Z
John Brant and Don Roberts: The Refactoring Browser
7
![Page 26: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/26.jpg)
Licensed Under Creative Commons by Naresh Jain
Why do we Refactor?
8
![Page 27: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/27.jpg)
Licensed Under Creative Commons by Naresh Jain
Why do we Refactor?
Helps us deliver more business value faster
8
![Page 28: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/28.jpg)
Licensed Under Creative Commons by Naresh Jain
Why do we Refactor?
Helps us deliver more business value faster
Improves the design of our software
Combat’s “bit rot”
Easier to maintain and understand
Easier to facilitate change
More flexibility
Increased re-usability
8
![Page 29: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/29.jpg)
Licensed Under Creative Commons by Naresh Jain
Why do we Refactor?...
9
![Page 30: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/30.jpg)
Licensed Under Creative Commons by Naresh Jain
Why do we Refactor?...
Minimizes technical debt
9
![Page 31: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/31.jpg)
Licensed Under Creative Commons by Naresh Jain
Why do we Refactor?...
Minimizes technical debt
Keep development at speed
9
![Page 32: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/32.jpg)
Licensed Under Creative Commons by Naresh Jain
Why do we Refactor?...
Minimizes technical debt
Keep development at speed
To make the software easier to understand
Write for people, not the compiler
Understand unfamiliar code
9
![Page 33: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/33.jpg)
Licensed Under Creative Commons by Naresh Jain
Why do we Refactor?...
Minimizes technical debt
Keep development at speed
To make the software easier to understand
Write for people, not the compiler
Understand unfamiliar code
To help find bugs
refactor while debugging to clarify the code
9
![Page 34: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/34.jpg)
Licensed Under Creative Commons by Naresh Jain
Why do we Refactor?...
Minimizes technical debt
Keep development at speed
To make the software easier to understand
Write for people, not the compiler
Understand unfamiliar code
To help find bugs
refactor while debugging to clarify the code
To “Fix broken windows” - Pragmatic Programmers
9
![Page 35: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/35.jpg)
Licensed Under Creative Commons by Naresh Jain
ReadabilityWhich code segment is easier to read?
Sample 1
if (date.before(Summer_Start) || date.after(Summer_End)){ charge = quantity * winterRate + winterServiceCharge;else charge = quantity * summerRate;}
Sample 2
if (isSummer(date)) { charge = summerCharge(quantity);Else charge = winterCharge(quantity);}
10
![Page 36: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/36.jpg)
Licensed Under Creative Commons by Naresh Jain
When should you refactor?
11
![Page 37: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/37.jpg)
Licensed Under Creative Commons by Naresh Jain
When should you refactor?
To add new functionality
refactor existing code until you understand it
refactor the design to make it simple to add
11
![Page 38: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/38.jpg)
Licensed Under Creative Commons by Naresh Jain
When should you refactor?
To add new functionality
refactor existing code until you understand it
refactor the design to make it simple to add
To find bugs
refactor to understand the code
11
![Page 39: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/39.jpg)
Licensed Under Creative Commons by Naresh Jain
When should you refactor?
To add new functionality
refactor existing code until you understand it
refactor the design to make it simple to add
To find bugs
refactor to understand the code
For code reviews
immediate effect of code review
allows for higher level suggestions
11
![Page 40: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/40.jpg)
Licensed Under Creative Commons by Naresh Jain
When should you refactor?
To add new functionality
refactor existing code until you understand it
refactor the design to make it simple to add
To find bugs
refactor to understand the code
For code reviews
immediate effect of code review
allows for higher level suggestions
Like championship snooker players we are setting ourselves up for
our next shot
11
![Page 41: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/41.jpg)
Licensed Under Creative Commons by Naresh Jain
The Two Hats
Adding Function
Add new capabilities to the system
Adds new tests
Get the test working
Refactoring
Does not add any new featuresDoes not add tests (but may change some)Restructure the code to remove redundancy
12
![Page 42: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/42.jpg)
Licensed Under Creative Commons by Naresh Jain
The Two Hats
Adding Function
Add new capabilities to the system
Adds new tests
Get the test working
Refactoring
Does not add any new featuresDoes not add tests (but may change some)Restructure the code to remove redundancy
Swap frequently between the hats, but only wear one at a time
12
![Page 43: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/43.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
13
![Page 44: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/44.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
13
![Page 45: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/45.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the Test
13
![Page 46: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/46.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the TestPass
13
![Page 47: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/47.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the Test
Fail
Pass
13
![Page 48: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/48.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the Test
Make a little change
Fail
Pass
13
![Page 49: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/49.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the Test
Make a little change
Run the Test
Fail
Pass
13
![Page 50: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/50.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the Test
Make a little change
Run the Test
Fail
Fail
Pass
13
![Page 51: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/51.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the Test
Make a little change
Run the Test
Fail
Pass
Fail
Pass
13
![Page 52: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/52.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the Test
Make a little change
Run the Test
Refactor
Fail
Pass
Fail
Pass
13
![Page 53: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/53.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the Test
Make a little change
Run the Test
Refactor
Fail
Pass
Fail
Pass
13
![Page 54: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/54.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the Test
Make a little change
Run the Test
Refactor
Fail
Pass
Fail
Pass
13
![Page 55: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/55.jpg)
Licensed Under Creative Commons by Naresh Jain
Team Techniques
Encourage refactoring culture
nobody gets things right first time
nobody can write clear code without reviews
refactoring is forward progress
14
![Page 56: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/56.jpg)
Licensed Under Creative Commons by Naresh Jain
Team Techniques...
15
![Page 57: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/57.jpg)
Licensed Under Creative Commons by Naresh Jain
Team Techniques...
Provide sound testing base
tests are essential for refactoring
build system and run tests daily
15
![Page 58: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/58.jpg)
Licensed Under Creative Commons by Naresh Jain
Team Techniques...
Provide sound testing base
tests are essential for refactoring
build system and run tests daily
Pair Programming
two programmers working together can be quicker than working separately
refactor with the class writer and a class user
15
![Page 59: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/59.jpg)
Licensed Under Creative Commons by Naresh Jain
How do we Refactor?
We looks for Code-Smells
Things that we suspect are not quite right or will cause us severe pain if we do not fix
16
![Page 60: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/60.jpg)
Licensed Under Creative Commons by Naresh Jain
Common Code Smells
The Big Stinkers
Duplicated code
Feature Envy
Inappropriate Intimacy
Comments
Long Method
Long Parameter List
Switch Statements
Improper Naming
17
![Page 61: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/61.jpg)
Licensed Under Creative Commons by Naresh Jain
Duplicated Code
18
![Page 62: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/62.jpg)
Licensed Under Creative Commons by Naresh Jain
Duplicated Code
There is obvious duplication
Such as copy and paste
18
![Page 63: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/63.jpg)
Licensed Under Creative Commons by Naresh Jain
Duplicated Code
There is obvious duplication
Such as copy and paste
There are unobvious duplications
Such as parallel inheritance hierarchies.
Similar algorithms
18
![Page 64: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/64.jpg)
Licensed Under Creative Commons by Naresh Jain
Duplicated Code
There is obvious duplication
Such as copy and paste
There are unobvious duplications
Such as parallel inheritance hierarchies.
Similar algorithms
Remedies
Extract Method
Pull Up Field
18
![Page 65: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/65.jpg)
Licensed Under Creative Commons by Naresh Jain
Feature Envy
19
![Page 66: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/66.jpg)
Licensed Under Creative Commons by Naresh Jain
Feature Envy
A method that seems more interested in some other class than the one it is in.
19
![Page 67: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/67.jpg)
Licensed Under Creative Commons by Naresh Jain
Feature Envy
A method that seems more interested in some other class than the one it is in.
Remedies:
Move Method
Extract Method
19
![Page 68: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/68.jpg)
Licensed Under Creative Commons by Naresh Jain
Extract Method
20
![Page 69: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/69.jpg)
Licensed Under Creative Commons by Naresh Jain
Extract Methodvoid printOwning(double amount){
printBanner();
// print detailsSystem.Console.WriteLine(string.Format(“name: “, name);System.Console.WriteLine(string.Format(“amount: “, amount);
}
20
![Page 70: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/70.jpg)
Licensed Under Creative Commons by Naresh Jain
Extract Methodvoid printOwning(double amount){
printBanner();
// print detailsSystem.Console.WriteLine(string.Format(“name: “, name);System.Console.WriteLine(string.Format(“amount: “, amount);
}
void printOwning(double amount){printBanner();printDetails(amount);
}
void printDetails(double amount){System.Console.WriteLine(string.Format(“name: “, name);System.Console.WriteLine(string.Format(“amount: “, amount);
}
20
![Page 71: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/71.jpg)
Licensed Under Creative Commons by Naresh Jain
Inappropriate Intimacy
21
![Page 72: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/72.jpg)
Licensed Under Creative Commons by Naresh Jain
Inappropriate Intimacy
Two or more classes fiddling with each other’s private parts.
21
![Page 73: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/73.jpg)
Licensed Under Creative Commons by Naresh Jain
Inappropriate Intimacy
Two or more classes fiddling with each other’s private parts.
Remedies
Move Method and Move Field
Change Bi-directional Association to Unidirectional
Extract Class
21
![Page 74: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/74.jpg)
Licensed Under Creative Commons by Naresh Jain
Extract Class
22
![Page 75: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/75.jpg)
Licensed Under Creative Commons by Naresh Jain
Extract Class
PersonnameofficeAreaCodeofficeNumber
getTelephoneNumber
22
![Page 76: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/76.jpg)
Licensed Under Creative Commons by Naresh Jain
Extract Class
PersonnameofficeAreaCodeofficeNumber
getTelephoneNumber
TelephoneNumber
areaCodenumber
getTelephoneNumber
Person
nametelephoneNumber
getTelephoneNumber
22
![Page 77: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/77.jpg)
Licensed Under Creative Commons by Naresh Jain
Comments
23
![Page 78: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/78.jpg)
Licensed Under Creative Commons by Naresh Jain
Comments
Comments – be suspicious!
23
![Page 79: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/79.jpg)
Licensed Under Creative Commons by Naresh Jain
Comments
Comments – be suspicious!
Comments are a deodorant.
23
![Page 80: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/80.jpg)
Licensed Under Creative Commons by Naresh Jain
Comments
Comments – be suspicious!
Comments are a deodorant.
Comments represent a failure to express an idea in the code.
23
![Page 81: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/81.jpg)
Licensed Under Creative Commons by Naresh Jain
Comments
Comments – be suspicious!
Comments are a deodorant.
Comments represent a failure to express an idea in the code.
Remedies:
Extract Method
Rename Method
Introduce Assertion
23
![Page 82: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/82.jpg)
Licensed Under Creative Commons by Naresh Jain
Introduce Assertion
24
![Page 83: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/83.jpg)
Licensed Under Creative Commons by Naresh Jain
Introduce Assertiondouble GetExpenseLimit() { // should have either expense limit or a primary project return(_expenseLimit != NULL_EXPENSE) ? _expenseLimit : _primaryProject.GetMemberExpenseLimit();}
24
![Page 84: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/84.jpg)
Licensed Under Creative Commons by Naresh Jain
Introduce Assertiondouble GetExpenseLimit() { // should have either expense limit or a primary project return(_expenseLimit != NULL_EXPENSE) ? _expenseLimit : _primaryProject.GetMemberExpenseLimit();}
double GetExpenseLimit() {assert(_expenseLimit != NULL_EXPENSE || _primaryProject != null, “Expense Limit and Primary Project must not be null”);
return(_expenseLimit != NULL_EXPENSE) ? _expenseLimit : _primaryProject.GetMemberExpenseLimit();}
24
![Page 85: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/85.jpg)
Licensed Under Creative Commons by Naresh Jain
Long Method
25
![Page 86: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/86.jpg)
Licensed Under Creative Commons by Naresh Jain
Long Method
Good OO code is easiest to understand and maintain with shorter methods with good names
Long methods tend to be harder to read and understand
25
![Page 87: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/87.jpg)
Licensed Under Creative Commons by Naresh Jain
Long Method
Good OO code is easiest to understand and maintain with shorter methods with good names
Long methods tend to be harder to read and understand
Remedies:
Extract Method
Replace Temp with Query
Replace Method with Method Object.
Decompose Conditional
25
![Page 88: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/88.jpg)
Licensed Under Creative Commons by Naresh Jain
Replace Temp with Query
double basePrice = _quanity * _itemPrice;
if(basePrice > 1000){
return basePrice * 0.95;}else{ return basePrice * 0.98; }
26
![Page 89: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/89.jpg)
Licensed Under Creative Commons by Naresh Jain
Replace Temp with Query
double basePrice = _quanity * _itemPrice;
if(basePrice > 1000){
return basePrice * 0.95;}else{ return basePrice * 0.98; }
if(getBasePrice() > 1000) {return getBasePrice() * 0.95;
}else { return getBasePrice() * 0.98; }
double getBasePrice() { return _quanitiy * _itemPrice;}
26
![Page 90: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/90.jpg)
Licensed Under Creative Commons by Naresh Jain
Long parameter list
27
![Page 91: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/91.jpg)
Licensed Under Creative Commons by Naresh Jain
Long parameter list
Functions should have as few parameters as possible.
27
![Page 92: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/92.jpg)
Licensed Under Creative Commons by Naresh Jain
Long parameter list
Functions should have as few parameters as possible.
Remedies:
Replace Parameter with Method
Preserve Whole Object
Introduce Parameter Object
27
![Page 93: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/93.jpg)
Licensed Under Creative Commons by Naresh Jain
Introduce Parameter Object
28
![Page 94: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/94.jpg)
Licensed Under Creative Commons by Naresh Jain
Introduce Parameter Object
Customer
AmoutInvoicedIn(Date start, Date end)AmoutRecivedIn(Date start, Date end)AmoutOverdueIn(Date start, Date end)
28
![Page 95: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/95.jpg)
Licensed Under Creative Commons by Naresh Jain
Introduce Parameter Object
Customer
AmoutInvoicedIn(Date start, Date end)AmoutRecivedIn(Date start, Date end)AmoutOverdueIn(Date start, Date end)
Customer
AmoutInvoicedIn(DateRange range)AmoutRecivedIn(DateRange range)AmoutOverdueIn(DateRange range)
28
![Page 96: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/96.jpg)
Licensed Under Creative Commons by Naresh Jain
Switch Statements
29
![Page 97: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/97.jpg)
Licensed Under Creative Commons by Naresh Jain
Switch Statements
Type cases are evil because they tend to be duplicated many times.
29
![Page 98: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/98.jpg)
Licensed Under Creative Commons by Naresh Jain
Switch Statements
Type cases are evil because they tend to be duplicated many times.
Remedies:
Replace Type Code with Subclasses
Replace Type Code with State / Strategy
Replace Conditional with Polymorphism.
Replace Parameter with Explicit Methods
Introduce Null Object.
29
![Page 99: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/99.jpg)
Licensed Under Creative Commons by Naresh Jain
Replace Parameter with Explicit Methods
void SetValue(String name, int value){
if(name.Equals(“height”)) {
_height = value; return;}if(name.Equals(“width”)){ _width = value; return;}Trace.Assert(false, “Should never reach here”);
}
30
![Page 100: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/100.jpg)
Licensed Under Creative Commons by Naresh Jain
Replace Parameter with Explicit Methods
void SetValue(String name, int value){
if(name.Equals(“height”)) {
_height = value; return;}if(name.Equals(“width”)){ _width = value; return;}Trace.Assert(false, “Should never reach here”);
}
void SetHeight(int value){
_height = value;}
void SetWidth(int value){
_width = value;}
30
![Page 101: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/101.jpg)
Licensed Under Creative Commons by Naresh Jain
Inappropriate Naming
31
![Page 102: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/102.jpg)
Licensed Under Creative Commons by Naresh Jain
Inappropriate NamingNames given to variables (fields) and methods should be clear and meaningful.
31
![Page 103: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/103.jpg)
Licensed Under Creative Commons by Naresh Jain
Inappropriate NamingNames given to variables (fields) and methods should be clear and meaningful.
A variable name should say exactly what it is.
Which is better?
private string s; OR private string salary;
31
![Page 104: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/104.jpg)
Licensed Under Creative Commons by Naresh Jain
Inappropriate NamingNames given to variables (fields) and methods should be clear and meaningful.
A variable name should say exactly what it is.
Which is better?
private string s; OR private string salary;
A method should say exactly what it does.
Which is better?
public double calc (double s)
public double calculateFederalTaxes (double salary)
31
![Page 105: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/105.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring & Patterns
There is a natural relation between patterns and refactorings. Patterns are where you want to be; refactorings are ways to get there from somewhere else. - Martin Fowler
32
![Page 106: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/106.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring to Patterns
33
![Page 107: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/107.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring to Patterns
Creation – creation of objects
33
![Page 108: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/108.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring to Patterns
Creation – creation of objects
Simplification – code simplification
33
![Page 109: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/109.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring to Patterns
Creation – creation of objects
Simplification – code simplification
Generalization – code abstraction
33
![Page 110: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/110.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring to Patterns
Creation – creation of objects
Simplification – code simplification
Generalization – code abstraction
Protection – improve protection of existing code
33
![Page 111: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/111.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring to Patterns
Creation – creation of objects
Simplification – code simplification
Generalization – code abstraction
Protection – improve protection of existing code
Accumulation – information accumulation code
33
![Page 112: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/112.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring to Patterns
Creation – creation of objects
Simplification – code simplification
Generalization – code abstraction
Protection – improve protection of existing code
Accumulation – information accumulation code
Utilities – misc
33
![Page 113: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/113.jpg)
Licensed Under Creative Commons by Naresh Jain
How to refactor Legacy Code?
34
![Page 114: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/114.jpg)
Licensed Under Creative Commons by Naresh Jain
How to refactor Legacy Code?
Identify change points
34
![Page 115: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/115.jpg)
Licensed Under Creative Commons by Naresh Jain
How to refactor Legacy Code?
Identify change points
Find an inflection point
34
![Page 116: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/116.jpg)
Licensed Under Creative Commons by Naresh Jain
How to refactor Legacy Code?
Identify change points
Find an inflection point
Cover the inflection point
34
![Page 117: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/117.jpg)
Licensed Under Creative Commons by Naresh Jain
How to refactor Legacy Code?
Identify change points
Find an inflection point
Cover the inflection point
Break external dependencies
34
![Page 118: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/118.jpg)
Licensed Under Creative Commons by Naresh Jain
How to refactor Legacy Code?
Identify change points
Find an inflection point
Cover the inflection point
Break external dependencies
Break internal dependencies
34
![Page 119: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/119.jpg)
Licensed Under Creative Commons by Naresh Jain
How to refactor Legacy Code?
Identify change points
Find an inflection point
Cover the inflection point
Break external dependencies
Break internal dependencies
Write tests
34
![Page 120: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/120.jpg)
Licensed Under Creative Commons by Naresh Jain
How to refactor Legacy Code?
Identify change points
Find an inflection point
Cover the inflection point
Break external dependencies
Break internal dependencies
Write tests
Make changes
34
![Page 121: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/121.jpg)
Licensed Under Creative Commons by Naresh Jain
How to refactor Legacy Code?
Identify change points
Find an inflection point
Cover the inflection point
Break external dependencies
Break internal dependencies
Write tests
Make changes
Refactor the covered code.
34
![Page 122: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/122.jpg)
Licensed Under Creative Commons by Naresh Jain
Essential Reading
35
![Page 123: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/123.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring Hands-on
We’ll use an open source project to apply refactoring lessons
Make sure your laptops are setup with the project
Form pairs and each pair picks up a module/package of the open source project.
36
![Page 124: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/124.jpg)
Licensed Under Creative Commons by Naresh Jain
Refactoring Hands-on...
You will be introduced to 2-3 code smells at a time
Apply lessons form working with legacy code to write tests around your inflection point
Apply lessons from refactoring and refactoring to patterns to eradicate the code smells
Repeat last 3 steps, till we run out of time
37
![Page 125: Refactoring Fest](https://reader033.vdocuments.us/reader033/viewer/2022060201/559b318d1a28ab570a8b4852/html5/thumbnails/125.jpg)
Licensed Under Creative Commons by Naresh Jain
Thank youRefactoring Fest
Naresh [email protected]
38