• Presentation is available online at– http://www.slideshare.net/jasonjnoble/tdd_rspec_palindrome
Test Driven Designto learn Recursion and PalindromesJason Nobleinstructor@jasonnoble.org
Test Driven Design• TDD is a software development practice thatrelies on the repetition of a very shortdevelopment cycle– Write a failing test that defines a desiredimprovement or new function– Write the minimum code possible to make thetest pass– Refactor the code to acceptable standards
Red Green Refactor• Write a failing test (Red)• Make the test pass (Green)• Refactor tests or code (NOT both!)
Why use TDD?• Helps you break problems down into smallmanageable tasks• Writing tests takes the fear out ofprogramming• Writing tests helps you communicate whatyour code SHOULD do/accomplish
Lets Talk about Recursion• "In order to understand recursion, you mustfirst understand recursion." – Anonymous• Lets ask Google
What is recursion?• Recursion is simply:– A simple base case (or base cases)– A set of rules that reduce all other cases towards aknown base case• Example:– Your parents are your ancestors (base case)– The ancestors of your ancestors are also yourancestors (recursion step)
Another example• Factorial– 1! equals 1 (Base case)– For any number n greater than 1, the factorial ofthat number is simply n * n – 1– Factorial of 2 is 2 * 1! = 2 * 1 (base case) = 2– Factorial of 3 is 3 * 1! = 3 * 2 = 6– Factorial of 4 is 4 * 3! = 4 * 6 = 24– Factorial of 5 is 5 * 4! = 5 * 24 = 120
Palindrome• A palindrome is defined as "Aword, phrase, number or other sequence ofsymbols or elements, whose meaning may beinterpreted the same way in either forward orreverse direction".• dad == dad.reverse• 10022001• radar
Lets code it!• git email@example.com:jasonnoble/tdd_rspec_palindrome.git• Whats in this repo?
Palindrome recursive definition• A given string is a palindrome if the string readforward is the same as the string readbackwards• Is an empty string a palindrome?• Is a single character a palindrome?