加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

How Does TDD Affect Design?

发布时间:2020-12-13 23:02:03 所属栏目:百科 来源:网络整理
导读:转自http://www.jamesshore.com/Blog/How-Does-TDD-Affect-Design.html How Does TDD Affect Design? 17 May,2014 (This essay was originally posted to theLet's Code JavaScript blog.) I've heard people say TDD automatically creates good designs. M

转自http://www.jamesshore.com/Blog/How-Does-TDD-Affect-Design.html

How Does TDD Affect Design?

17 May,2014

(This essay was originally posted to theLet's Code JavaScript blog.)

I've heard people say TDD automatically creates good designs. More recently,I've heardDavid Hansson sayit creates design damage. Who's right?

Neither. TDD doesn't create design. You do.

TDD Can Lead to Better Design

There are a few ways I've seen TDD lead to better design:

  1. A good test suite allows you to refactor,which allows you to improve your design over time.

  2. The TDD cycle is very detail-oriented and requires you to make some design decisions when writing tests,rather than when writing production code. I find this helps me think about design issues more deeply.

  3. TDD makes some design problems more obvious.

None of these force you to create better design,but if you're working hard to create good design,I find that these things make it easier to get there.

TDD Can Lead to Worse Design

There are a few ways I've seen TDD lead to worse design:

  1. TDD works best with fast tests and rapid feedback. In search of speed,some people use mocks in a way that locks their production code in place. Ironically,this makes refactoring very difficult,which prevents designs from being improved.

  2. Also in search of speed,some people make very elaborate dependency-injection tools and structures,as well as unnecessary interfaces or classes,just so they can mock out dependencies for testing. This leads to overly complex,hard to understand code.

  3. TDD activates people's desire to get a "high score" by having a lot of tests. This can push people to write worthless or silly tests,or use multiple tests where a single test would do.

None of these are required by TDD,but they're still common. The first two are obvious solutions to the sorts of design problems TDD exposes.

They're also very poor solutions,and you can (and should) choose not to do these things. Itispossible to create fast tests and rapid feedback without these mistakes,and you can see us take that approach inthe screencast.

So Do Your Job

TDD doesn't create good design.Youdo. TDD can help expose design smells.Youhave to pay attention and fix them. TDD can push you toward facile solutions.Youhave to be careful not to make your design worse just to make testing better.

So pay attention. Think about design. And if TDD is pushing you in a direction that makes your code worse,stop. Take a walk. Talk to a colleague. And look for a better way.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读