Weitere ähnliche Inhalte Ähnlich wie Unleashing the Power of Automated Refactoring with JDT (20) Mehr von Naresh Jain (20) Kürzlich hochgeladen (20) Unleashing the Power of Automated Refactoring with JDT1. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
Unleashing the Power of
Automated Refactoring with JDT
Law of Diversity
Distrust all claims of ONE true way!
Naresh Jain
@nashjain
http://nareshjain.com
naresh@agilefaqs.com
2. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
Plan
What and Why... Refactor?
Automated Refactoring Demo 1 with JDT
When and How... Refactor?
Automated Refactoring Demo 2 with JDT
4. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
What is Refactoring?
A series of small steps, each of which changes the
program’s internal structure without changing its
external behaviour - Martin Fowler
8. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
What if you hear...
We’ll just refactor the code to support logging
Can you refactor the code so that it authenticates against LDAP instead
of Database?
9. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
What if you hear...
We’ll just refactor the code to support logging
Can you refactor the code so that it authenticates against LDAP instead
of Database?
10. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
What if you hear...
We’ll just refactor the code to support logging
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
11. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
What if you hear...
We’ll just refactor the code to support logging
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
12. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
What if you hear...
We’ll just refactor the code to support logging
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
13. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
What if you hear...
We’ll just refactor the code to support logging
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
14. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
What if you hear...
We’ll just refactor the code to support logging
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
In-Memory Object Caching?
15. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
What if you hear...
We’ll just refactor the code to support logging
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
In-Memory Object Caching?
16. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
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
17. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
Readability
Which 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);
}
28. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
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
31. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
2 Piece of Advice before Refactoring
Baby Steps
The Hippocratic Oath
First Do No Harm!
34. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
Code Smells Lab
Long Method
Lazy class and
Speculative Generality Smell
Refused Bequest
Black Sheep
Duplicate Code
Switch Smell
Dead Code
Alternative Classes with different interface
Odd Ball solution
Primitive Obsession
37. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
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
41. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
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
42. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
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
43. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
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
45. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
When should you refactor?
To add new functionality
refactor existing code until you understand it
refactor the design to make it simple to add
46. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
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
47. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
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
48. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
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
49. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
The Two Hats
Adding Function
Add new capabilities to the system
Adds new tests
Get the test working
Refactoring
Does not add any new features
Does not add tests (but may change some)
Restructure the code to remove redundancy
50. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
The Two Hats
Adding Function
Add new capabilities to the system
Adds new tests
Get the test working
Refactoring
Does not add any new features
Does 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
55. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the Test
Fail
Pass
56. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the Test
Make a little change
Fail
Pass
57. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the Test
Make a little change
Run the Test
Fail
Pass
58. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
Refactoring and TDD
TDD Rhythm - Test, Code, Refactor
Add a Test
Run the Test
Make a little change
Run the Test
Fail
Fail
Pass
59. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
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
60. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
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
61. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
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
62. http://agilefaqs.com | Copyright © 2016, AgileFAQs. All Rights Reserved.
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
63. T H A N K Y O U !
Q U E S T I O N S ?
Naresh Jain
@nashjain
http://nareshjain.com