tags:

views:

91

answers:

2

I was thinking to implement MD5 as a code kata and wanted to use BDD to drive the design (I am a BDD newb).

However, the only test I can think of starting with is to pass in an empty string, and the simplest thing that will work is embedding the hash in my program and returning that.

The logical extension of this is that I end up embedding the hash in my solution for every test and switching on the input to decide what to return. Which of course will not result in a working MD5 program.

One of my difficulties is that there should only be one public function:

public static string MD5(input byte[])

And I don't see how to test the internals.

Is my approach completely flawed or is MD5 unsuitable for BDD?

A: 

It depends on what you mean with unsuitable... :-) It is suitable if you want to document a few examples that describes your implementation. It should also be possible to have the algorithm emerge from your specifciation if you add one more character for each test.

By just adding a switch statement you're just trying to "cheat the system". Using BDD/TDD does not mean you have to implement stupid things. Also the fact that you have hardcoded hash values as well as a switch statement in your code are clear code smells and should be refactored and removed. That is how your algorithm should emerge because when you see the hard coded values you first remove them (by calculating the value) and then you see that they are all the same so you remove the switch statement.

Also if your question is about finding good katas I would recommend lokking in the Kata catalogue.

Cellfish
+2  A: 

I believe you chose a pretty hard exercise for a BDD code-kata. The thing about code-kata, or what I've understood about it so far, is that you somehow have to see the problem in small incremental steps, so that you can perform these steps in red, green, refactor iterations.

For example, an exercise of finding an element position inside an array, might be like this:

  1. If array is empty, then position is 0, no matter the needle element
  2. Write test. Implementation. Refactor
  3. If array is not empty, and element does not exist, position is -1
  4. Write test. Implementation. Refactor
  5. If array is not empty, and element is the first in list, position is 1
  6. Write test. Implementation. Refactor

I don't really see how to break the MD5 algorithm in that kind of steps. But that may be because I'm not really an algorithm guy. If you better understand the steps involved in the MD5 algorithm, then you may have better chances.

Ionuț G. Stan