# Code Kata 2 Day 2

So here is Code Kata 2 Day 2, wraith of recursion. Too much? Anyway, after taking a iterative approach to solving the problem, I am looking a a recursive approach. It took me about 15 minutes to slop together. I arranged it in a Day2Chop object to keep everything clean and separate. Once again, I rely on the provided unit tests to confirm functionality.

## So you had no problems with Day 2?

Oh, I did have an annoying as hell problem. I put a catch all return at the end of the recursive function. I could not figure out for the life of me while my test kept failing despite my logic appearing to be good. It was not until I actually ran it and realized I had left the “return -1;” statement in my final product. I had it because that is how I fixed the first problems, I ran into the tests. I had to create it and have it return something so I returned -1.

It took about 2 seconds to find and cleanup once I ran the code.

Enough of that, let’s get to the code.

### Day2Chop.cs

```using System;
using System.Collections.Generic;
namespace CodeKata2
{
public class Day2Chop
{
public int chop(int numberToSeek, List intList)
{
if (intList.Count == 0)
{
return -1;
}
return recursive(numberToSeek,intList, -1);
}

`private int recursive(int numberToSeek, List<int> intList, int count) `
`{ `
`count++; `
`if (numberToSeek == intList[0]) `
`{ `
`return count; `
`} `
`else `
`{ `
`intList.RemoveAt(0); `
`} `
`if(intList.Count == 0) `
`{ `
`return -1; `
`} `
`return recursive(numberToSeek, intList, count); `
`} `
`}`
}```

In the main chop function, I check to intList for being empty and return not found if that is true then send it off the recursive function.

This function is pretty straightforward. I send in the count function which acts as a counter for the index location. This is why the chop function gives a -1 in the parameter. We want the first thing this recursive function to do is to increment. So the first pass becomes 0.

We do a simple check of it matches the number we are seeking in the first element of the array. If yes then we return the count if no we remove that from the list. Once remove, we check the array for still having values then call it again with the increment count and the shorten array going into it.

## So what do you think about this Code Kata?

I really like the simplicity of the function. I was surprised when I looked at some other solutions, how complicated they were compared to mine. The only issue I feel I have is bad naming. One of the things I would do if I refactored this class would be to change the naming. It was not as clear as it should have been and I am annoyed by that. I am happy with the white space and function lengths. I would refactor the empty list check to its own function since I am calling it twice but otherwise, I am happy with it.

## You can not end Code Kata 2 Day 2 like that. Where is the unit test.

Well, the unit test is almost exactly the same as day 1. I changed day1 to day2 and rename d1 to d2. Otherwise it is exactly the same. I probably should have considered a way to bring them all together so I would not be so repetitive but my Unit Test training says one test class for every class. If you want to see the test class, just make sure to change the object being changed and the rest is just human stuff, rather than machine stuff. You can find it in the previous post.