-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add links to guided learning hour videos.
Add new LHs for split variable and test double mock
- Loading branch information
1 parent
9171c05
commit b2c63f1
Showing
19 changed files
with
183 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
theme: refactoring | ||
title: Split Variable | ||
name: split_variable | ||
kata: tennis | ||
difficulty: 3 | ||
author: emilybache | ||
--- | ||
|
||
# Split Variable | ||
|
||
Practice this refactoring. | ||
|
||
There is a video on Emily Bache's YouTube channel for this learning hour ["Prepare a Code Paragraph for Extraction with Split Variable"](https://youtu.be/wPmJz2ynb3k) | ||
|
||
## Session Outline | ||
|
||
* 5 min connect: Identify Paragraphs | ||
* 5 min concept: Split Variable | ||
* 10 min demo: show the patterns and the shape of the refactoring | ||
* 25 min concrete: practice Split Variable | ||
* 5 min conclusions: note down learnings | ||
|
||
## Connect: Identify Paragraphs | ||
If the group is not familiar with code paragraphs then you should do a learning hour on that before this one. The purpose of this connect exercise is to remind them what they already know about code paragraphs by having them review some code and identify paragraphs. | ||
|
||
For example, show them [Theatrical Players](https://github.com/emilybache/Theatrical-Players-Refactoring-Kata). | ||
|
||
## Concept: Split Variable Refactoring | ||
The reason to do split variable is often so that you can more easily extract a method for a code paragraph. Refer to [Split Variable]({% link _refactorings/split_variable.md %}) for a description of this refactoring. There are several code katas I commonly use that use it. It's a small refactoring move that's hard to practice in isolation so I usually show it several times in these different situations. | ||
|
||
## Demo: show the patterns and the shape of the refactoring | ||
Depending on how confident your group will be able to do these refactorings, you may or may not need to show all of these as demos. | ||
|
||
1. **Plain re-use**. In Tennis 3 there is a variable 's' that is re-used both as a score name and a player name. The variable is re-used so you need to redeclare it for each new use. | ||
|
||
2. **Accumulating variable** In Tennis 7 there is a variable 'result' that is appended to throughout the long method 'getScore'. Before you can extract methods for each part, it is convenient to split the 'score' variable into for example 'tieScore' and 'regularScore'. This kind of accumulating variable can also be an integer like the 'volumeCredits' in Theatrical Players. | ||
|
||
3. **Calculated Datamember** In Office Cleaner 9 there is a method 'parseInput' that calculates two datamembers - coordHashSet and position. If you want to extract parts of this calculation to a testable pure function then it will need to be passed the current state of these datamembers explicitly. You split the assignment of the datamember from the calculation of its value by introducing a new variable for the calculation, then assigning it at the end. | ||
|
||
## Concrete: do split variable | ||
Set the group loose on the refactoring in all the examples you've shown. Make sure they have access to the list of the safe steps for all variants of this refactoring, and remind them to use their tools. | ||
|
||
## Conclusions: when will you use this? | ||
How will you notice you need 'split variable'? What will you do then? This is a [Write important takeaway]({% link _activities/conclusions/write_important_takeaway.md %}) conclusion. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
theme: test_doubles | ||
title: When you need a mock | ||
kata: auction | ||
difficulty: 3 | ||
author: emilybache | ||
--- | ||
|
||
# When you need a mock | ||
|
||
In this exercise you need to use a mock or spy to test the code. | ||
|
||
There is a video on Emily Bache's YouTube channel that goes through this learning hour ["Mock objects: the Police Enforcer of Unit Testing"](https://youtu.be/4bCuxMhtf_E). | ||
|
||
## Session Outline | ||
|
||
* 10 min connect: How can you test this? | ||
* 5 min concept: Explanation of test doubles | ||
* 30 mins concrete: Write a test using a test double | ||
* 5 min conclusion: Do you like your design? | ||
|
||
### Connect: How can you test this? | ||
|
||
Today's exercise is [Auction Test Design Kata](https://github.com/emilybache/Auction-TestDesign-Kata). Review the code in | ||
"AuctionMessageTranslator". It is supposed to parse text messages it receives from an external source, and notify a listener. The messages are about the progress of an online auction. | ||
|
||
There are bugs, marked with comments. The listener interface is under development. This listener wants to know what is happening in the auction so it can decide whether to make a bid. | ||
|
||
How could you write tests that would fail because of each bug? Discuss in pairs. Don't write any code at this point, just talk. | ||
|
||
### Concept: Test Doubles | ||
|
||
Explain what a test double is and how we could use one in this problem. You could explain the various different kinds of test double including spy and mock. | ||
|
||
### Concrete: Write a test using a test double | ||
|
||
Ask people to write tests for each of the bugs using a mock. Each test should fail because of a bug, and pass if you change the code to fix the bug. Through this work you should design the methods in the "AuctionEventListener" interface. | ||
|
||
When you have fixed the bugs, add support for the additional message outlined in the README file in the repo. | ||
|
||
### Conclusions: Do you like your design? | ||
|
||
Compare the designs you have come up with for the AuctionEventListener interface. Why did we use a test double to develop this code? Ask [When should you use this]({% link _activities/conclusions/when_to_use_this.md %})? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--- | ||
layout: refactoring | ||
title: Split Variable | ||
source: Emily Bache | ||
source_url: https://refactoring.com/catalog/splitVariable.html | ||
code_smells: variable_with_long_scope | ||
learning_hours: split_variable | ||
--- | ||
|
||
# Split Variable | ||
This is one of the refactorings in Martin Fowler's book although I think his definition is unnecessarily narrow and I usually extend it to additional situations. | ||
|
||
## Examine | ||
There are three common situations to use split variable: | ||
|
||
1. The same variable is re-used for different purposes. This is the first form of split variable, and the one that Martin Fowler discusses in his book. | ||
2. An accumulating variable where you want to split off part of the calculation to another scope. | ||
3. A global or class data member that is used in many places but only calculated in one place, and you want to split the calculation to a different (smaller) scope. | ||
|
||
## Prepare | ||
Identify the variable you want to split, and make sure your tests are all passing. | ||
|
||
## Implement | ||
Steps for each of the three situations: | ||
|
||
1. Same variable is re-used for different purposes | ||
* Tests all passing. | ||
* Identify a variable that needs to be split, 'x'. | ||
* Within the lines of code you select for the first thing it does, use textual search and replace - rename 'x' to something better. | ||
* After those lines, re-declare 'x' as a new variable. | ||
* Tests all passing. | ||
|
||
2. An accumulating variable | ||
* Tests all passing | ||
* Identify a variable that needs to be split, 'x' | ||
* Create a new local variable 'initial_x' and initialize it to 'x' | ||
* Create a new local variable 'increment_x' and initialize it to an empty value eg zero | ||
* Within the lines of code you select use textual search and replace - rename 'x' to 'increment_x' | ||
* After those lines, re-assign x = initial_x + increment_x | ||
* Tests all passing | ||
|
||
3. A global or class variable | ||
* Tests all passing | ||
* Identify a variable that needs to be split, 'x' | ||
* Create a new variable 'x_local' - copy the declaration of 'x' and copy the same initial value as 'x' | ||
* Within the lines of code you select for the new scope, use textual search and replace to replace x with x_local | ||
* After that section of code, re-assign 'x' to the value of 'x_local' | ||
* Tests all passing | ||
|
||
## Clear | ||
It usually makes sense to review the names of the new variables and adjust them now you can see how each is used. | ||
|
||
## Follow up | ||
Often you now want to extract a method to calculate one or more of the new variables. |