“Any fool can write code that a computer can
understand.
Good programmers write code that humans can
understand.”
Martin Fowler
Slide 5
Slide 5 text
Refactoring makes your code
easier to understand.
Slide 6
Slide 6 text
70 to 90% of our time we spend
on understanding code.
Robert C. Martin, Carola Lilienthal and The Mythical Man Month
Slide 7
Slide 7 text
Refactoring
!
Clean Code
!
Faster Delivery
Slide 8
Slide 8 text
Risky Code
Slide 9
Slide 9 text
Risky Code
• Has no tests
• Is not testable
• Can break a lot
• Can make users angry
National Geographic
Slide 10
Slide 10 text
Bluetooth on Android
can be risky
Slide 11
Slide 11 text
Bluetooth is difficult for
test automation
Slide 12
Slide 12 text
To know it is working you need
to test lots of different devices.
Slide 13
Slide 13 text
We had a stable bluetooth stack.
Slide 14
Slide 14 text
Make it
extendable
and
changeable.
Slide 15
Slide 15 text
Refactor
Slide 16
Slide 16 text
Rewrite != Refactor
Slide 17
Slide 17 text
Things You Should Never Do Part 1
Slide 18
Slide 18 text
Refactoring
Slide 19
Slide 19 text
“Refactoring is the process of changing a software system
in a way that does not alter the external behaviour of the
code yet improves its internal structure.”
Martin Fowler
Slide 20
Slide 20 text
How to change code without
breaking anything?
Slide 21
Slide 21 text
Tests.
Slide 22
Slide 22 text
Loads of Tests.
Slide 23
Slide 23 text
It’s easy to refactor when having
good tests.
Slide 24
Slide 24 text
If you have testable code.
Add tests then refactor.
Slide 25
Slide 25 text
Refactoring Risky Code ?
Slide 26
Slide 26 text
There are no tests.
Neither is the code testable.
Slide 27
Slide 27 text
One false move and …
Slide 28
Slide 28 text
No content
Slide 29
Slide 29 text
?
Slide 30
Slide 30 text
make it testable
risky code
add tests
refactor
Slide 31
Slide 31 text
make it testable
risky code
Slide 32
Slide 32 text
Hyperaware coding.
Slide 33
Slide 33 text
Get rid of not testable
dependencies.
Slide 34
Slide 34 text
Adding abstractions.
Slide 35
Slide 35 text
No content
Slide 36
Slide 36 text
/**
* Scans for Bluetooth Classic devices.
*/
interface ClassicScanner {
/**
* Starts a scan when subscribed,
* stops the scan when unsubscribed.
*/
fun scan(): Observable
}
Slide 37
Slide 37 text
class AndroidClassicScanner(
private val bluetoothProvider:
BluetoothProvider,
private val context: Context
) : ClassicScanner {
// … All the Android stuff
}
Slide 38
Slide 38 text
is stupid.
contains logic.
Slide 39
Slide 39 text
Refactoring Survival Guide
Those tips can save your career.
Slide 40
Slide 40 text
1. Only wear one hat at a time.
Slide 41
Slide 41 text
No content
Slide 42
Slide 42 text
When refactoring we don’t
change the behaviour of the code.
Slide 43
Slide 43 text
We improve the structure.
Slide 44
Slide 44 text
I know what I am
doing.
Slide 45
Slide 45 text
No you don’t.
Slide 46
Slide 46 text
2. Keep your code green.
Slide 47
Slide 47 text
3. Keep your
tests green.
Slide 48
Slide 48 text
Don’t continue
with failing tests.
Slide 49
Slide 49 text
4. Tests. Tests. Tests.
Slide 50
Slide 50 text
Why do I need to add tests when I
know my code works?
Slide 51
Slide 51 text
What am I testing then?
Slide 52
Slide 52 text
One thing is certain.
Slide 53
Slide 53 text
Change.
Slide 54
Slide 54 text
Tests allow you to change your
code.
Slide 55
Slide 55 text
5. Small Tasks
Slide 56
Slide 56 text
No content
Slide 57
Slide 57 text
Don’t go crazy.
Slide 58
Slide 58 text
Clearly define a goal.
Slide 59
Slide 59 text
Small Tasks
• Merge often.
• Avoid huge PRs.
Slide 60
Slide 60 text
6. Commit a lot
Slide 61
Slide 61 text
When you reach a clear state,
make a commit.
Slide 62
Slide 62 text
git reset HEAD --hard
Slide 63
Slide 63 text
7. Use the tools.
Slide 64
Slide 64 text
Android Studio is your friend.
Slide 65
Slide 65 text
Don’t do stuff manually.
Slide 66
Slide 66 text
Find & Replace can help.
Slide 67
Slide 67 text
8. Minimise The Damage
Slide 68
Slide 68 text
Staged rollout.
Slide 69
Slide 69 text
Use alpha testing.
Slide 70
Slide 70 text
Put your refactoring behind a
feature switch.
Slide 71
Slide 71 text
You can roll back within a minute.
Slide 72
Slide 72 text
We duplicated our code and
made small changes to it.
Slide 73
Slide 73 text
Secret status view for
QA
Slide 74
Slide 74 text
9. Ship.
Slide 75
Slide 75 text
It only counts when it’s shipped.
Slide 76
Slide 76 text
Make sure you get enough QA
power.
Slide 77
Slide 77 text
Conclusion
Slide 78
Slide 78 text
Refactoring != Rewrite
Slide 79
Slide 79 text
Refactoring is improving the structure
of code without changing its behaviour.
Slide 80
Slide 80 text
make it testable
risky code
Slide 81
Slide 81 text
Hyperaware coding.
Slide 82
Slide 82 text
Refactoring Survival Guide
1. Only wear one hat at a time
2. Keep your code green.
3. Keep your tests green.
4. Tests. Tests. Tests.
5. Small tasks.
6. Commit a lot.
7. Use the tools.
8. Minimise the damage.
9. Ship.
Slide 83
Slide 83 text
Further Readings
• Refactoring: Improving the Design of Existing Code
• Working Efficiently With Legacy Code
• Workflows of Refactoring
• Opportunistic Refactoring
• Clean Code: A Handbook of Agile Software Craftmanship
Slide 84
Slide 84 text
Slides on Speakerdeck tomorrow.
Check my twitter account @muffls