What do you do when you need to work with some really bad code? It seems like a no-win situation: whether you rewrite it, do a ""refactoring sprint,"" or just live with it, you have to pay a big cost. But these aren't the only options. There's a way to get control of your code so that you can make whatever improvements you need in a low-stress, low-cost way. To explain how, this talk is split into two parts. In the first part we'll look at a messy React Native component and see how effortless it is to make improvements to the code if you have comprehensive tests. In the second part we'll tackle that big ""if:"" how in the world can you get comprehensive tests like that? By the end of this talk you'll have the information to decide if this approach to testing and refactoring will help your project, and if so, you'll have next steps to hone your skills in this approach.
Getting Better All the Time: How to Escape Bad Code
1. Getting Better All the Time
How to Escape Bad Code
Josh Justice
Big Nerd Ranch
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
2. I have to work with
some bad code.
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
3. (to the code)
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
4. What do I do about
bad code?
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
5. What do you do about
bad code?
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
6. Work around bad code?
• Development will be slow
• Workarounds mean the next change will be even slower
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
7. Rewrite bad code?
• A big chunk of work that doesn't deliver features to the user and
that can't be interrupted
• If I break it, I own it
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
8. So what do we do about
bad code?
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
9. Wait…
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
10. Wait…
I just remembered something…
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
11. Wait…
I just remembered something…
…that changes everything
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
12. This component has
thorough tests!
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
13. I can make it better
as I go.
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
14. (to the code)
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
15. That was pretty fun!
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
16. Refactoring
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
17. Refactoring
Small changes that improve the
arrangement of the code without
changing its functionality.
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
18. Why Small Changes?
• Value: making the improvements that pay off right away
• Delivery: code is shippable after each refactoring
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
19. What to do about bad code?
Make it better all the
time.
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
20. What to do about bad code?
Make it better all the
time, if…
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
21. What to do about bad code?
Make it better all the
time
IF…
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
22. Make it better all the time, if…
you have
comprehensive tests
you can trust.
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
23. "Trust our tests?"
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
24. "Trust our tests?"
"I can hardly stand our
tests!"
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
25. Is it even possible to
get comprehensive
tests you can trust?
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
26. Test-Driven Development (TDD)
1. Write a test and see it fail ("red")
2. Write just enough code to make the test pass ("green")
3. Refactor
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
27. (to the code)
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
28. reactnativetesting.io/gettingbetter
• The rest of the TDD sequence
• More refactors
• Pointers to more resources on TDD in React Native
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
29. The biggest benefit of TDD:
TDD equips you to
make your code better
all the time.
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
30. Write whichever kinds
of tests give you
confidence to make it
better all the time.
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
31. "TDD is too much work."
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
32. Know what else is a lot of work?
Living with bad code
forever.
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
33. You're going to deal with bad code
somehow.
You don't want to work around it.
You often can't rewrite it.
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
34. The way I know to escape bad code:
Make it better all the time.
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
35. Imagine…
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
36. Imagine coming into a new codebase
totally confident you
can trust the tests.
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
37. TDD
→ great tests
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
38. TDD
→ great tests
→ making it better all the time
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
39. TDD
→ great tests
→ making it better all the time
→ great code!
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh Justice - reactnativetesting.io/gettingbetter
40. About Me
Principal Architect at Big Nerd Ranch
• Mobile app consulting
• Corporate training
Get in touch:
reactnativetesting.io/gettingbetter
Getting Better All the Time: How to Escape Bad Code - React Native EU 2022 - Josh
Justice - reactnativetesting.io/gettingbetter