string calculator tdd kata - github•create a simple string calculator with a method add(numbers)...
TRANSCRIPT
![Page 1: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/1.jpg)
String Calculator TDD Kata
Created by Roy Osherovehttp://osherove.com/tdd-kata-1
Ruby Solution based on performance by Corey Haineshttp://katas.softwarecraftsmanship.org/?p=80
Wednesday, April 27, 2011
![Page 2: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/2.jpg)
• Create a simple string calculator with a method Add(numbers) that takes a string
• The method can take 0, 1 or 2 numbers, and will return their sum (for an empty string it will return 0) for example “” or “1” or “1,2”
Basic Requirements
Wednesday, April 27, 2011
![Page 3: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/3.jpg)
• Start with the simplest test case of an empty string and move to one and two numbers
• Remember to solve things as simply as possible so that you force yourself to write tests you did not think about
• Remember to refactor after each passing test
Rules
Wednesday, April 27, 2011
![Page 4: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/4.jpg)
Getting Start from project shell on Github
git clone [email protected]:calebphillips/string_calculator.git
cd string_calculator
gem install bundler
bundle install
git checkout shell
autotest
Wednesday, April 27, 2011
![Page 5: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/5.jpg)
`const_missing': uninitialized constant Object::StringCalculator (NameError)
The Empty String
What’s a StringCalculator?
Wednesday, April 27, 2011
![Page 6: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/6.jpg)
The Empty String
That’s nicer.
Wednesday, April 27, 2011
![Page 7: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/7.jpg)
The Empty String
This is what we are looking for.
Wednesday, April 27, 2011
![Page 8: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/8.jpg)
The Empty String
“Remember to solve things as simply as
possible”
Wednesday, April 27, 2011
![Page 9: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/9.jpg)
Single numbers
Saw that one coming.
Duplication
Wednesday, April 27, 2011
![Page 10: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/10.jpg)
Single numbers
Duplication “Remember to solve things as simply as
possible”
Wednesday, April 27, 2011
![Page 11: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/11.jpg)
Single numbers
What if we could say it like this?
Wednesday, April 27, 2011
![Page 12: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/12.jpg)
Single numbers
Duplication removed. Don’t get distracted by RSpec
syntax.
Wednesday, April 27, 2011
![Page 13: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/13.jpg)
Single numbers
Expected that, but why don’t I see the
actual value returned?
Wednesday, April 27, 2011
![Page 14: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/14.jpg)
Single numbers
Still red, but the message is much
better.
Wednesday, April 27, 2011
![Page 15: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/15.jpg)
Single numbers
Getting smarter.
Wednesday, April 27, 2011
![Page 16: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/16.jpg)
Two numbers
How did it come up with 2?
Wednesday, April 27, 2011
![Page 17: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/17.jpg)
Two numbers
“Remember to solve things as simply as
possible”
Wednesday, April 27, 2011
![Page 18: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/18.jpg)
Two numbers
Two digit number throws a monkey
wrench.
Wednesday, April 27, 2011
![Page 19: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/19.jpg)
Two numbersGetting a little more general.
How many times can you say ‘to_i’?
Wednesday, April 27, 2011
![Page 20: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/20.jpg)
Two numbersStill green.
Now there are a lot of details here.
Wednesday, April 27, 2011
![Page 21: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/21.jpg)
Two numbers
Still green and add is a little cleaner now
Wednesday, April 27, 2011
![Page 22: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/22.jpg)
New Requirement
Allow the Add method to handle an unknown amount of numbers
Wednesday, April 27, 2011
![Page 23: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/23.jpg)
Three numbers
Red
Wednesday, April 27, 2011
![Page 24: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/24.jpg)
Three numbersWhy is cheating
so much fun?
Wednesday, April 27, 2011
![Page 25: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/25.jpg)
Three numbers
Busted.
Wednesday, April 27, 2011
![Page 26: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/26.jpg)
Three numbersMore general.
Are there really 3 different cases
here?
Wednesday, April 27, 2011
![Page 27: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/27.jpg)
Three numbersAh, that’s
nicer.
Wednesday, April 27, 2011
![Page 28: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/28.jpg)
Many numbers
Passes as-is.
Wednesday, April 27, 2011
![Page 29: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/29.jpg)
New Requirement
Allow the Add method to handle new lines between numbers (instead of commas).
the following input is ok: “1\n2,3” (will equal 6)the following input is NOT ok: “1,\n” (not need to
prove it - just clarifying)
Wednesday, April 27, 2011
![Page 30: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/30.jpg)
New lines as delimiters
Red.
Wednesday, April 27, 2011
![Page 31: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/31.jpg)
New lines as delimiters
Green. ‘,’ is duplicated.
Wednesday, April 27, 2011
![Page 32: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/32.jpg)
New lines as delimiters
Hide that literal away in a descriptively named
method
Wednesday, April 27, 2011
![Page 33: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/33.jpg)
New lines as delimiters
Passes as-is.
Wednesday, April 27, 2011
![Page 34: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/34.jpg)
New RequirementSupport different delimiters
To change a delimiter, the beginning of the string will contain a separate line that looks like this:
“//[delimiter]\n[numbers…]” for example “//;\n1;2” should return three where
the default delimiter is ‘;’ .
The first line is optional - all existing scenarios should still be supported
Wednesday, April 27, 2011
![Page 35: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/35.jpg)
Custom delimiters
Bonus Points: Why did it return 1?
Wednesday, April 27, 2011
![Page 36: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/36.jpg)
Custom delimiters
Delimiter abstraction comes in handy - we only
changed one method.
Hard coding
Wednesday, April 27, 2011
![Page 37: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/37.jpg)
Custom delimiters
Red
Wednesday, April 27, 2011
![Page 38: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/38.jpg)
Custom delimiters
Replace constant with variable
Are the concepts clear
here?
Wednesday, April 27, 2011
![Page 39: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/39.jpg)
Custom delimiters
No comments needed.
Wednesday, April 27, 2011
![Page 40: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/40.jpg)
Custom delimiters
A little less noise.
Wednesday, April 27, 2011
![Page 41: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/41.jpg)
Custom delimiters
You can memoize the delimiter if you only
want to calculate it once.
Wednesday, April 27, 2011
![Page 42: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/42.jpg)
New Requirement
Calling Add with a negative number will throw an exception “negatives not allowed” - and the negative
that was passed
If there are multiple negatives, show all of them in the exception message
Wednesday, April 27, 2011
![Page 43: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/43.jpg)
Negative Numbers
Red.
Wednesday, April 27, 2011
![Page 44: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/44.jpg)
Negative Numbers
“Remember to solve things as simply as
possible”
Wednesday, April 27, 2011
![Page 45: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/45.jpg)
Negative Numbers
add reads like the requirements
Wednesday, April 27, 2011
![Page 46: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/46.jpg)
Negative Numbers
Red.
Wednesday, April 27, 2011
![Page 47: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/47.jpg)
Negative Numbers
Refactor in the green.
Wednesday, April 27, 2011
![Page 48: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/48.jpg)
Negative Numbers
Extract a method A little
memoization
Wednesday, April 27, 2011
![Page 49: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/49.jpg)
Negative Numbers
Back to Red.
Wednesday, April 27, 2011
![Page 50: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/50.jpg)
Negative Numbers
Done.
Wednesday, April 27, 2011
![Page 51: String Calculator TDD Kata - GitHub•Create a simple string calculator with a method Add(numbers) that takes a string • The method can take 0, 1 or 2 numbers, and will return their](https://reader030.vdocument.in/reader030/viewer/2022040323/5e6b8f89165d745c7c11f1ea/html5/thumbnails/51.jpg)
Checking your answersgit diff master
Starting fresh
git reset --hard HEAD
Wednesday, April 27, 2011