TDD Kata 03: Prime Factors

This is my weekly Kata post. Read the first one to learn what it is all about.

Last week: String Calculator

The Kata was some training in regular expressions, although that’s not really the intention. What is more interesting is to come up with edge cases to break the code. This was my data provider for valid data the first time I did the Kata in PHP:

    public static function dataValidAddInput()
    {
        return [
            'empty_string_returns_zero' => ['', 0],
            'single_number_returns_self' => ['1', 1],
            'two_numbers_separated_by_comma' => ['1,2', 3],
            'more_numbers_separated_by_comma' => ['4,2,5,6,8,10,0', 35],
            'numbers_separated_by_newline' => ["1\n2", 3],
            'numbers_separated_by_newline_and_comma' => ["1\n2,3", 6],
            'specified_delimiter' => ["//;\n1;2", 3],
            'specified_long_delimiter' => ["//DELIM\n0DELIM2DELIM3", 5],
            'slash_delimiter' => ["///\n3/4", 7],
            'number_0_delimiter' => ["//0\n10203", 6],
            'number_9_delimiter' => ["//9\n192939", 6],
        ];
    }

But when I really restricted myself to 15 minutes, I only got to step 6.

Following a friend’s suggestion, I implemented a simplified version the next time, which just parses the input for valid numbers, ignoring the delimiters. This deliberately fails the “number as delimiter” tests, but that was a sacrifice I was willing to make. It felt like cheating but “not doing more than necessary” is actually a useful skill as well!

Suddenly, the method to extract all numbers was really simple (here in Ruby):

    def integer_array numbers
        numbers.scan(/-?\d+/).map(&:to_i)
    end

This way, many of the additional requirements solved themselves and I was able to make my “timeboxing” goal.

Third Kata: Prime Factors

The Kata is described here: http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata (Power Point with step by step instructions for Java)

My personal goals this week

  • Finally try out Behat