TW洞见 | 用TDD影响设计

在听过Martin Fowler、Kent Beck和DHH关于TDD的讨论以后,我想也把我关于TDD的想法记录下来——我为什么要遵从它,以及为什么我会发现它做为一种思考过程,在解决设计问题的时候特别有帮助。

直到我完全理解了用户案例(也即测试案例)之前,我无法设计或者编写生产代码。因此,我从思考用户案例开始,我为它编写代码。现在这样的思考方式本身对我来说,就是“测试驱动”的。并且基于已经确认的测试案例,我开始仔细考虑我的设计。我需要在运行代码前编写测试代码;为了确认我的代码能工作,我需要测试它。

While building test cases on the whiteboard or as pending test cases, I identify the design patterns to be implemented. Now should I start writing my test and code, without design pattern abstraction and refactor to the desired design pattern, or jump directly into defining classes and tests using my desired design pattern? Both approaches work for me. I like to start small, so I write my first test case and write code to implement that. However, sometimes I like thinking of design upfront to avoid immediate test cases implementation rework, but will not code for it. And so I draft my test cases with the design already in mind.

Lots of times I don’t have a clue, can’t think of abstraction and design upfront, in that case starting flat is good. And after implementation of a few test cases, my code starts shaping up and I get more insight to find better abstractions. Sometimes it is difficult to write tests without even having a ‘code structure skeleton’ in place. Even though I feel drafting a ‘code structure skeleton’ is OK at times, you can avoid it by avoiding doing a 1-1 mapping of your “test class” with your “code class”. TDD also helps me with good naming, because I started with the use-case. Since I code the client (call) first, that helps me in naming it from the usage perspective and not from implementation perspective.

To get the most out of TDD, don’t be too dogmatic about its implementation; instead use it as a technique to influence the way you design. For instance, TDD is really helpful in identifying smells in design. I have seen code all test driven, but full of switch case constructs without abstractions, which entirely misses the point of TDD. In my view, if I am following TDD, it does not mean my design is good, but rather I should use it as a technique to drive my design. Here are few obvious TDD test smells that help me identify bad design,

  • Difficulty in writing tests: Especially when there are too many dependencies that require too much setup code. In the Rails world for example - ‘Fat Controllers’ without services or helper classes.
  • Excess tests for a single unit: This happens when classes have too much responsibility. In the Rails world, the example would be ‘Fat Models’.
  • Excess assertions: This happens when one method is doing too many things or tests are written at the wrong level. One test is trying to test too many things at once.
  • Excess tests for a component: While the individual tests are very fast, it takes very long to run all tests. This may be an architectural smell of a monolithic application. Break it down into small components. On my last project we had the UI as a separate repo with only Views (templates), CSS and JS, including unit tests for Views and JavaScript.
  • Cascading effects of code modification on unrelated tests: This is the case when a change in code, leads to changes required in other unrelated tests. This happens due to unnecessary mocking or testing at the wrong level. For example a change in the code for the model requires changes in the controller tests without any of the controller code having changed.

Practicing TDD also helps me with slicing and dicing stories, because TDD is a technique that helps one think of the minimal use-case that should be implemented to get started.

What are your thoughts on using TDD?

原文发布于微信公众号 - 思特沃克(ThoughtWorks)

原文发表时间:2015-01-22

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏编程

浅谈Web前端工程师的必备技能

一名优秀的Web前端工程师应该具备以下技能,看看你是否符合吧! 【必备】 PhotoShop/Fireworks Design 配合美工将草图形成具体的符合W...

19450
来自专栏互联网杂技

2017年前端开发工具趋势

你有两年以上的前端开发经验吗?你会用 Sass 和 Autoprefixer 等高级的CSS辅助技能吗?你的 JavaScript 知识是否融汇贯通,你是否喜欢...

33970
来自专栏PPV课数据科学社区

数据可视化分析工具大集合

俗话说“巧妇难为无米之炊”。数据时代,没有一款好的数据可视化分析工具,光有团队怎么行? 商场如战场,数据是把枪。亚马逊运用大数据为客户推荐商品信息,阿里用大数据...

54850
来自专栏MixLab科技+设计实验室

5个用法,关于Gif。

今天趁有点小空,分享下gif动图的5个用法~ 1、做教程。回复网友analog关于imovie的设置logo的问题。 由于最近太忙了,没来的及看留言,导致超过...

32730
来自专栏互联网杂技

2015 年 JavaScript 开发者调查报告

年底将至,JavaScript 开发者调查也已经结束,此次调查总共有 5000 份回复,我真的迫不及待要分享这次调查的细节,感谢所有的参与者,这是 JavaSc...

32370
来自专栏腾讯社交用户体验设计

Windows 10 新特性变化研究 - 腾讯ISUX

16320
来自专栏web前端教室

【图片简历】Vue.js在线简历编辑器;生成图片简历(二)

今天是春节之后上班第一天,许多同学们都会觉得有些不习惯吧?没关系,明天就是第二天,后天就是第三天了,慢慢的你就习惯了...跟大家开个玩笑哈,春节已过,让我们继续...

53850
来自专栏木子昭的博客

刷课(刷剧)神器!给网页视频加个速(最快可达16倍!)

油猴脚本地址:https://greasyfork.org/zh-CN/scripts/30879-bye-flash-hello-html...

58030
来自专栏腾讯数据中心

你必须认识的数据中心安全标识

行话说“年头出事白干一年,年尾出事一年白干”。临近年关,数据中心“安全”也变得热门了起来。数据中心的人身、物理及信息安全有千头万绪。今天,我们就从数据中心标识系...

50050
来自专栏守候书阁

2018前端值得关注的技术

2017悄然过去,2018已经来到。人在进步,技术在发展。2018年前端有哪些领域,技术值得关注,哪些技术会兴起,哪些技术会没落。下面就我个人的判断进行一个预测...

643150

扫码关注云+社区

领取腾讯云代金券