- Validate the environment
- Run all tests (
Ctrl-U, L
)
- Run all tests (
- Open NCrunch menu (
Alt-U
)- Enable NCrunch
- Ensure Risk/Progress window is open and visible and out of the way.
We are looking at a large method that we don't understand, and we want to clean up. We are going to "follow our nose", leaning into the places we don't like and pulling out chunks and improving them.
- Automated refactorings "only"
- Commit after each refactoring
- Every refactoring commit prefixed with
r -
- Rotate typist on commit
- No context menu
- Identify a smell
- Refactor
- Decide if you like the result
- Commit (or revert)
-
## Activity 1: Extract Paragraphs to methods
- Select a paragraph (a
case
) [CTRL-W
] - Extract a method to a Obvious Nonsense name [
CTRL-SHIFT-R
] - Decide if you like the call (ignore the body)
- Fix and commit, or revert and try again
- Select a paragraph (a
-
## Activity 2: Move behaviors onto new classes
- Move each method onto a new class x 4 [
CTRL-SHIFT-R
] - Convert
switch
toif
/else
[ALT-ENTER
] - Remove redundant
else
in method [ALT-ENTER
] - Select the condition
- Extract to a method with a Nonsense name
- Move the method to the corresponding class, commit
- Repeat x 3
- Move each method onto a new class x 4 [
-
## Activity 3: Remove duplication
- Make similar things identical
- Make each class non-static [recipe]
- Apply Refactoring Combo: Undo Make Static
- Surround each
if
with a block [ALT-ENTER
] - Introduce local variables [
CTRL-SHIFT-R
] - Extract interface x 1 [
CTRL-SHIFT-R
] - Apply interface x 3
- Specify type explicitly
- Use base type x 4
- Apply Refactoring Combo: Many-of-one (two, three, four)
- Extract variable
scorers
- Extract method
ApplyScorers()
-
Missing/Misleading -> (Obvious) Nonsense
-
Nonsense -> Honest (but incomplete)
-
Honest -> Honest-and-complete
-
Honest-and-Complete -> Does the right thing
-
Does the right thing -> Intent
-
Intent -> Domain Abstraction
Insight: A proven-correct refactoring means that the resulting code is the same as the beginning code. Therefore undoing a proven-correct refactoring is also a proven-correct refactoring.
- Verify that Git shows no changes
- Execute manual, unproven refactoring
- Execute automated refactoring to undo #1
- Save All
- Verify that Git shows no changes
- Undo the automated refactoring
- Commit
This is not a proven-correct refactoring.
Given:
SOMETYPE somevar = ...;
// do something with somevar
Becomes:
foreach (SOMETYPE somevar in new SOMETYPE[]{ ... })
{
// do something with somevar
}
- Add curly braces (
{}
) around the eventual body - Prefix variable declaration with
foreach (
- Replace
=
within new SOMETYPE[]{
- Replace
;
with})
-
Redo today’s coding exercises on your own computer
- Start from the code on the
main
branch - If you want to see the code we wrote together, check the other branches
- Pay attention to:
- how often you're able to execute a single refactoring - more is better
- how many times you are able to pull out a paragraph into a new method
- Start from the code on the
-
Print out this sheet and keep it on your desk. Over the next week as you're doing your normal work:
- note when you were able to improve the code with a single, committed refactoring
- note when you extract a paragraph
- Make a TODO list of code you wish was better but couldn't improve at this time