3. CODE REFACTORING
âProcess to change the existing code without changing
its external behavior.
Refactoring improves nonfunctional attributes
of the softwareâ
12. WHEN REFACTORING?
Ideally, when we identify:
â Code Smells: Things are not right
â WTFs: Code is not understandable
â It should be different: Product evolved
14. WHEN REFACTORING?
Keep the balance between:
refactoring vs new features
Don't refactor unless you think it will pay off later by
allowing faster feature delivery
16. HOW TO REFACTOR?
From the methodology point of view, my experience
tells me that:
â Revolutionary design is evil
â Evolutionary design is good
17. HOW TO REFACTOR?
The most common approaches about refactoring are:
â âBig Bangâ
â âDivide and Conquerâ
â Planned refactoring
18. HOW TO REFACTOR?
The âBig Bangâ approach:
â Define a new structure for your code
â Move the code to the new structure
â Rebuild the puzzle
â Make tests green again!
19. HOW TO REFACTOR?
The âBig Bangâ approach:
â Define a new structure for your code
â Move the code to the new structure
â Rebuild the puzzle
â Make tests green again!
â High risks!
â Almost like rewrite from scratch
20. HOW TO REFACTOR?
The âDivide and Conquerâ approach:
â Define a Vision
â Break the application into pieces
â Rebuild the puzzle
â Make tests green again!
21. HOW TO REFACTOR?
The âDivide and Conquerâ approach:
â Define a Vision
â Break the application into pieces
â Rebuild the puzzle
â Make tests green again!
â Difficult estimation (like bugs)
â Code could be âundeliverableâ for
long period
22. HOW TO REFACTOR?
Planned Refactoring also called as Stabilization
periods:
â No new features developed (Code freeze period)
â Work without adding Value
â Not an improvement for the customer
23. HOW TO REFACTOR?
Planned Refactoring also called as Stabilization
periods:
â No new features developed (Code freeze period)
â Work without adding Value
â Not an improvement for the customer
â Itâs a Methodology Smell!
â No real âvalueâ added to the
application
24. HOW TO REFACTOR?
What a Good Team does is:
â Define a Vision
â Break it into smaller tasks
â Refactoring all the time (part of regular work)
â Responsible (Diligently keep the code clean)
25. HOW TO REFACTOR?
What a Good Team does is:
â Define a Vision
â Break it into smaller tasks
â Refactoring all the time (part of regular work)
â Responsible (Diligently keep the code clean)
â Code can be delivered (often)
â New features benefits from the
refactor (faster to develop)
27. HOW MUCH REFACTOR?
We need to always consider the benefits of
refactoring
â Tolerance level (metrics could help)
â Donât clean up everything
â Make refactoring part of our daily work
â Multiple small improvements
â Donât refactor what is not likely to change
â Consider the economic factor:
Cleaner code âš Quicker and Cheaper changes
28. How can we obtain
Refactoring with Agile
Methodologies?
29. AGILE & REFACTOR?
Agile Methodologies focuses on continuous (or at
least frequent) Value delivery.
The Value (for the customer) is realized when the
product is shipped in production, not before.
30. AGILE & REFACTOR?
In order to use Agile Methodologies for big
refactoring, we need to enforce some practices:
â Define and communicate the Vision
â Start with small tasks
â Iterate and Learn
35. TOOLS & TECHNIQUES
There are different tools and techniques that can help
us to be more agile and deliver more often:
â Trunk Based Development
â Branch By Abstraction
â Feature Toggles/Flags
37. TRUNK BASED DEV.
Branches act as an Isolation from the âreal worldâ.
38. TRUNK BASED DEV.
Multi branches
can make the
code
undeployable
for long
periods
â Undeployable
â Deployable
39. TRUNK BASED DEV.
What is really wrong with Branches?
â Branching is not bad, long lived branches are bad
â âIsolationâ for prolonged periods of time is bad
â Multi-branches can lead to undeployable code
41. TRUNK BASED DEV.
What are the benefits of TBD?
â Work on a single shared trunk
â Always work against the latest code
â Merge and integration pain is minimised
Note:
â TBD does not prohibit branching
42. TRUNK BASED DEV.
F FIX TBD Optimistic Branching
R D ?
R RELEASED
DEVELOPED NO COMMITS YET
D ?
209 210 211 TRUNK
209.x
FIX
R
209.1
R D F
?
209 210 211 TRUNK
43. TRUNK BASED DEV.
TBD Pessimistic Branching
R RELEASED
F FIX DEVELOPED NO COMMITS YET
D ?
209.x
?
R D ?
209 210 211 TRUNK
209.x
R D
R
211.x
?
209 210 211 TRUNK
209.x
R D
FIX
R
209.1
F
R
211.x
?
209 210 211 TRUNK
45. BRANCH BY ABSTRACTION
1. Introduce an abstraction layer
2. Update all the code to rely on the abstraction
3. Make new implementation of the abstraction
4. Remove the old implementation
48. TOGGLES & FLAGS?
What are the advantages of using Feature Toggles:
â Decouples code deployment from feature
â No need to rollback (disable the feature)
â A/B testing
â Deliver features to specific users only
â Continuous delivery