Created by Roy Osherove http://osherove.com/tdd-kata-1 Ruby ...

0 downloads 98 Views 3MB Size Report
Apr 27, 2011 - Support different delimiters. To change a delimiter, the beginning of the string will contain a separate
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

Basic Requirements •

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”

Wednesday, April 27, 2011

Rules •

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

Wednesday, April 27, 2011

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

The Empty String

What’s a StringCalculator?

`const_missing': uninitialized constant Object::StringCalculator (NameError) Wednesday, April 27, 2011

The Empty String

That’s nicer.

Wednesday, April 27, 2011

The Empty String

This is what we are looking for.

Wednesday, April 27, 2011

The Empty String

“Remember to solve things as simply as possible”

Wednesday, April 27, 2011

Single numbers

Duplication

Wednesday, April 27, 2011

Saw that one coming.

Single numbers

Duplication

Wednesday, April 27, 2011

“Remember to solve things as simply as possible”

Single numbers

What if we could say it like this?

Wednesday, April 27, 2011

Single numbers

Duplication removed. Don’t get distracted by RSpec syntax. Wednesday, April 27, 2011

Single numbers

Expected that, but why don’t I see the actual value returned? Wednesday, April 27, 2011

Single numbers

Still red, but the message is much better. Wednesday, April 27, 2011

Single numbers

Getting smarter.

Wednesday, April 27, 2011

Two numbers

How did it come up with 2? Wednesday, April 27, 2011

Two numbers

“Remember to solve things as simply as possible”

Wednesday, April 27, 2011

Two numbers

Two digit number throws a monkey wrench. Wednesday, April 27, 2011

Two numbers Getting a little more general.

How many times can you say ‘to_i’?

Wednesday, April 27, 2011

Two numbers Still green.

Now there are a lot of details here.

Wednesday, April 27, 2011

Two numbers

Still green and add is a little cleaner now

Wednesday, April 27, 2011

New Requirement

Allow the Add method to handle an unknown amount of numbers

Wednesday, April 27, 2011

Three numbers

Red

Wednesday, April 27, 2011

Three numbers Why is cheating so much fun?

Wednesday, April 27, 2011

Three numbers

Busted.

Wednesday, April 27, 2011

Three numbers More general.

Are there really 3 different cases here?

Wednesday, April 27, 2011

Three numbers Ah, that’s nicer.

Wednesday, April 27, 2011

Many numbers

Passes as-is.

Wednesday, April 27, 2011

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

New lines as delimiters

Red.

Wednesday, April 27, 2011

New lines as delimiters

Green.

Wednesday, April 27, 2011

‘,’ is duplicated.

New lines as delimiters

Hide that literal away in a descriptively named method

Wednesday, April 27, 2011

New lines as delimiters

Passes as-is.

Wednesday, April 27, 2011

New Requirement Support 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

Custom delimiters

Bonus Points: Why did it return 1?

Wednesday, April 27, 2011

Custom delimiters

Hard coding

Wednesday, April 27, 2011

Delimiter abstraction comes in handy - we only changed one method.

Custom delimiters

Red

Wednesday, April 27, 2011

Custom delimiters

Are the concepts clear here?

Wednesday, April 27, 2011

Replace constant with variable

Custom delimiters

No comments needed.

Wednesday, April 27, 2011

Custom delimiters

A little less noise.

Wednesday, April 27, 2011

Custom delimiters

You can memoize the delimiter if you only want to calculate it once.

Wednesday, April 27, 2011

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

Negative Numbers

Red.

Wednesday, April 27, 2011

Negative Numbers

“Remember to solve things as simply as possible”

Wednesday, April 27, 2011

Negative Numbers

add reads like the requirements

Wednesday, April 27, 2011

Negative Numbers

Red.

Wednesday, April 27, 2011

Negative Numbers

Refactor in the green. Wednesday, April 27, 2011

Negative Numbers

Extract a method

Wednesday, April 27, 2011

A little memoization

Negative Numbers

Back to Red.

Wednesday, April 27, 2011

Negative Numbers

Done.

Wednesday, April 27, 2011

Checking your answers git diff master

Starting fresh git reset --hard HEAD

Wednesday, April 27, 2011