- About me
- The world's fastest explanation of refactoring
- Some examples of refactoring from real-life applications
- Hairier examples of refactoring from real-life applications
- My refactoring algorithm
- Open mic
Last active
November 8, 2019 03:24
-
-
Save campezzi/e9a0360ba421d9420f57220f11e2ef1a to your computer and use it in GitHub Desktop.
Refactoring
Redability: Capture BFF business listing
Readability again: in-tray-service OCR results handler
Remove duplication: Capture screen onboarding completion
Performance improvement: document-storage-service gathering number of attachments
What makes these examples safe?
Tests verify implementation, not behaviour: Capture text fields
Excessive mocking: document-storage-service document upload endpoints
Tests that are too DRY: in-tray-service email endpoints - actual tests are here
No tests at all: Capture API client
Having tests is not enough - you need good tests to refactor with confidence.
So, you want to refactor some code. Here's how I do it.
- Do you really want to refactor? Measure pros and cons. Sometimes it's OK to leave it for later.
- Have a good sense of what you want to do - extract methods, create classes, apply a pattern...
- Refactoring Guru may give you some ideas
- Make sure you understand what the code currently does
- Tests are the best source of information
- If the tests are heavily tied to the implementation, go cry in a corner somewhere. Then change them to verify a behaviour instead.
- Add any missing tests to ensure the current behaviour is maintained
- What if the current behaviour is wrong?
- Apply your refactoring
- Run tests until they're all passing
- Repeat until you're satisfied with your code
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment