string calculator tdd kata - github•create a simple string calculator with a method add(numbers)...

51
String Calculator TDD Kata Created by Roy Osherove http://osherove.com/tdd-kata-1 Ruby Solution based on performance by Corey Haines http://katas.softwarecraftsmanship.org/?p=80 Wednesday, April 27, 2011

Upload: others

Post on 12-Mar-2020

15 views

Category:

Documents


0 download

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

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

• 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

• 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

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

`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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Checking your answersgit diff master

Starting fresh

git reset --hard HEAD

Wednesday, April 27, 2011