The only way to survive in a codebase is by refactoring continuously. We know that since the Extreme Programming days. But what stops us from doing so today? In this talk, Victor summarizes what he learned discussing Clean Code and Refactoring with hundreds of teams throughout the world. You'll find ideas to tackle a broad spectrum of factors: technical, cultural, psychological, emotional, social, and even political. Using these you might unlock the freedom to refactor for you and your colleagues.
After this roundtrip, the talk briefly overviews several of the most dangerous code smells in the projects today: God Class, Duplicated Code vs Divergent Code, Temporal Coupling, Middle Man, Speculative Generality, Mutable Long-Lived State, Comments, and more. For each of them weโll discuss a typical workaround, plus several subtleties and variations.
2. Victor Rentea
VictorRentea.ro
Recorded Talks, Blog, Video Courses, ...
Independent Trainer
Founder of
Bucharest Software Craftsmanship Community
Java Champion
โค๏ธ Simple Design, Refactoring, Unit Testing โค๏ธ
3. Technical Training
500 days
(200+ online)
2000 devs
8 years 50+ companies
Reach out
to me:
Hibernate
Spring Func Prog
Java Performance
Reactive Prog
Design Patterns
Pragmatic DDD
Clean Code
Refactoring
Unit Testing
TDD
any
lang
@victorrentea
Intense
More: VictorRentea.ro
Workshops: victorrentea.teachable.com
8. 29 ยฉ VictorRentea.ro
a training by
I'm afraid to refactor
because we don't have tests
It's impossible to test a
200-lines method!
The Vicious Circle of Legacy Code
Pair Programming
(80% less bugs)
Coding Katas!
TDD Practice
9. 30 ยฉ VictorRentea.ro
a training by
Safe Refactoring Tips
Next IDE Refactoring Fails Stress
Don't Break Compilation
(unless exploring)
Automated Refactoring
as much as you can
Decompose Large Changes
into small, safe moves
Use Quick-fix
to keep your focus on the design
(Alt-Enter / โฅ-Enter)
11. 32 ยฉ VictorRentea.ro
a training by
"I don't have time. I'm too busy"
Being busy is a form of laziness
- Tim Ferriss
Habits of Efficient Developers
by Daniel Lebrero
Stop! and Reflect.
Focus on what matters.
12. 33 ยฉ VictorRentea.ro
a training by
Improve Your Focus
Turn off desktop/mobile notifications
Automate Stuff
Pomodoro 25/5
No morning inbox
Max 30 min meetings
๏ง Digital Detox
Master your IDE โ
Get your brain to focus
Chris Bailey
IntelliJ Productivity Tips
Victor Rentea
26. 49 ยฉ VictorRentea.ro
a training by
Exploratory Refactoring
Discover
Timeboxed: eg 15 min.
Revert.
Ctrl -
Missing from our Practice:
27. 50 ยฉ VictorRentea.ro
a training by
refactoring idea
Long-lived Feature Branch
Source Control
28. 51 ยฉ VictorRentea.ro
a training by
Merge Conflicts
Top annoying things in Dev's life:
โข Interruptions
โข Refactoring Tests
โข Long meetings, and
29. 52 ยฉ VictorRentea.ro
a training by
Trunk Based Development
(requires maturity & discipline)
Git Flow
(merge conflicts, redo-refactor)
Long Branches Inhibit Refactoring
if (easyBoxReturnFeature) {
// feature under develop
}
Feature Toggle
+ incremental DB migrations
Preliminary Refactoring
(separate Pull-Request)
31. 54 ยฉ VictorRentea.ro
a training by
Micro-commits
Break a large change set into:
Large + Safe Automated Refactoring
Manual Refactoring
Adding Behavior
+ Easier Code Review + Less Bugs
- Prior Exploration Needed
to identify the steps
A Commit Message Notation: https://github.com/RefactoringCombos/ArlosCommitNotation
Separate
Commits, or
Preparatory
refactoring PR
32. 56 ยฉ VictorRentea.ro
a training by
What Stops Refactoring: Recap
Fear
โ tests, pair programming
โ practice
Time
โ Proper estimates
โ Improve Effectiveness
Rushing
even without time pressure
Lack of Consensus
โ team design brainstorm
Huge PR - Rejected
โ Trust
โ Micro-Commits
Fear of Breaking some Eggs
to make the omelet
Merge Conflicts
โ short-lived branches
โ trunk-based
YOLO-driven developer
A. Make it fun, play together
B. Legacy Experience TM
Lack of Skills
extract a method ?!!
โ Learn Code Smells
Unknown Code
Refactoring = Learning
36. VictorRentea.ro
61
Large Method
God Class
Too Many Parameters
Data Clumps
(String, String, Long) โ Address
> 20 lines
> 200 lines
> 4
All numbers are arbitrary. Find your own comfort zone
37. 65 ยฉ VictorRentea.ro
a training by
Enrich your Domain Language
by Discovering Value Objects
Simplify your code
Constraints Methods Reuse in larger Entities
38. VictorRentea.ro
66
Speculative Generality
Middle Man
person.getAge()
int getAge() {
return bio.getAge();
}
person.getBio().getAge()
Data Classes
get/set mania โ OOP
Feature Envy
Function โค๏ธ the state of an object
Primitive Obsession
โ PhoneNumber, OrderId, enum
โ ๐ KISS Principle
40. 68 ยฉ VictorRentea.ro
a training by
Map<Long, List<Long>> customerIdToOrderIds
Map<CustomerId, List<OrderId>> orders
redeemCoupon(Long, Long, String)
redeemCoupon(CustomerId, CouponId, PhoneNumber)
Micro-Types
41. VictorRentea.ro
69
Primitive Obsession
Make concepts explicit
by introducing new small classes
Even if it's a single
String โ PhoneNumber
Long โ CustomerId โ ID type:
@Value
class CustomerId {
Long id;
}
Escape the
42. 70 ยฉ VictorRentea.ro
a training by
Map<Long, List<Long>> customerIdToOrderIds
If the only usage of a map is:
for (Long customerId : map.keySet()) {
List<Long> orderIds = map.get(customerId);
...
}
Code Smell: Iterating over Map Keys
List<CustomerOrderIds>
You might be missing an abstraction (class):
class CustomerOrderIds {
private Long customerId;
private List<Long> orderId;
...
}
(or entries)
48. 76 ยฉ VictorRentea.ro
a training by
Code Smells Sheet
Defeating the Evil starts with Naming It
https://sourcemaking.com/refactoring/smells
Long Method
God Class
Data Clumps
Long Parameter List
Primitive Obsession
Data Class
Feature Envy
Middle Man
Duplicated Code
Shotgun Surgery
Divergent Code
Repeated Switches
Loops
Temporary Field
Long-Lived Mutable Data
Speculative Generality
Comments
Chapter 3
Many more + Solutions in
or: https://sourcemaking.com/refactoring/smells
49. VictorRentea.ro
blog, best talks, training offer
@VictorRentea
The End
victorrentea.ro/community
Join me:
Stay into
The Light
sourcemaking.com/refactoring
First Half + Ch. 17 Ch.3 Code Smells For rookies Coding Katas
kata-log.rocks/refactoring
cleancoders.com
refactoring.guru
Share your thoughts
The Beginning