MD5码kata和BDD

发布于 2024-08-07 15:49:49 字数 328 浏览 4 评论 0原文

我正在考虑将 MD5 实现为代码类型,并希望使用 BDD 来驱动设计(我是 BDD 新手)。

然而,我能想到的唯一测试就是传入一个空字符串,最简单的方法是将哈希值嵌入到我的程序中并返回它。

其逻辑扩展是,我最终将哈希嵌入到每个测试的解决方案中,并打开输入来决定返回什么。这当然不会产生有效的 MD5 程序。

我的困难之一是应该只有一个公共函数:

public static string MD5(input byte[])

而且我不知道如何测试内部结构。

我的方法是否完全有缺陷,或者 MD5 不适合 BDD?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牛↙奶布丁 2024-08-14 15:49:49

我相信您为 BDD 代码型选择了一项相当困难的练习。关于 code-kata 的事情,或者到目前为止我对它的理解,是你必须以某种方式以小的增量步骤看到问题,这样你就可以在红色、绿色、重构迭代中执行这些步骤。

例如,查找数组内元素位置的练习可能如下所示:

  1. 如果数组为空,则位置为 0,无论 Needle 元素如何
  2. 写入测试。执行。重构
  3. 如果数组不为空,且元素不存在,则位置为 -1
  4. 写入测试。执行。重构
  5. 如果数组不为空,并且元素是列表中的第一个,则位置为 1
  6. 写入测试。执行。重构

我真的不明白如何以这样的步骤破坏 MD5 算法。但这可能是因为我并不是一个真正的算法专家。如果你更好地理解MD5算法所涉及的步骤,那么你可能会有更好的机会。

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.

日久见人心 2024-08-14 15:49:49

这取决于您对不合适...的意思:-) 如果您想记录一些描述您的实现的示例,那么它是合适的。如果您为每个测试添加一个字符,也应该可以让算法从您的规范中出现。

通过添加一个 switch 语句,您只是想“欺骗系统”。使用 BDD/TDD 并不意味着您必须实施愚蠢的事情。此外,您的代码中具有硬编码的哈希值和 switch 语句这一事实是明显的代码味道,应该重构和删除。这就是您的算法应该如何出现的原因,因为当您看到硬编码值时,您首先将它们删除(通过计算值),然后您会发现它们都是相同的,因此您删除了 switch 语句。

另外,如果您的问题是关于寻找好的 kata,我建议您查看 Kata 目录

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文