Dev Skills: Refactoring
Learning Goals
- Identify areas of code that need to be refactored
- Utilize arguments and parameters to DRY up code
Vocabulary
DRY
Don’t Repeat Yourself!WET
Write Everything Twicerefactor
Rewriting working code for readability, maintainability, or performance without changing it’s behavior
Warm Up
Talk with your partner: If someone says, “this needs to be refactored”, what might they see that makes them say that? Be ready to share your list!
Modeling a Refactor
As you watch a real person refactor in real time, take note of the questions they ask themselves and the things they consider.
It’s Not A Race
Refactoring is more of an art. It should not be rushed.
You should be following the red-green-refactor flow, just as you do with TDD. Refactoring is the step we take after things work. The code is ok, but the goal of this process is to make it great. To do this well, great developers are methodical and thorough.
Just because you spot a change you could make, doesn’t mean it’s a good or necessary refactor. For example - changing every if/else statement into a ternary would be a waste of your time, and would actually make your code harder to read. Be selective about the code you choose to refactor.
Your Turn
With the repo you select based on the level of heat you want, take these steps:
- Make sure you are familiar with the current functionality (i.e., run the app in the browser!)
- There are many refactoring opportunities here. List all the ones you see out.
- With your partner, decide on one area to focus your energy.
- Rubber duck it - what do you know? Which parts seem almost
impossible
right now? Takes notes in notebook or with comments to start chipping away at the challenge. - Once you feel like you are done, ensure that the app is behaving as it originally was in the browser. Make sure to clean up any old comments, old code, whitespace, etc.
Mild: Clone down this repo. Medium: Use your pair project repo Spicy: Ask another pair for the link to their repo
Wrap Up
In your notes, reflect on the following prompts:
- How can I spot a potential refactor?
- When I elect to refactor something, what is my goal?