Clean code that works (simplicity is more complicity than you think 朴素比你想象的更复杂)...

本文深入探讨了通过自动化测试驱动代码开发的过程,阐述了编写清晰、可靠代码的重要性及其带来的益处。从减少错误、提高可预测性、促进团队协作到提升用户体验,自动化测试成为软件开发不可或缺的一部分。文章还详细介绍了实施测试驱动开发(TDD)的两大原则,并解释了如何通过'红绿重构'循环来优化代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

your job when writing the code should be to satisy the requirements no less and no more

 

I l@ve RuBoardPrevious Section Next Section

Preface

Clean code that works, in Ron Jeffries' pithy phrase, is the goal of Test-Driven Development (TDD). Clean code that works is a worthwhile goal for a whole bunch of reasons.

  • It is a predictable way to develop. You know when you are finished, without having to worry about a long bug trail.

  • It gives you a chance to learn all of the lessons that the code has to teach you. If you only slap together the first thing you think of, then you never have time to think of a second, better thing.

  • It improves the lives of the users of your software.

  • It lets your teammates count on you, and you on them.

  • It feels good to write it.

But how do we get to clean code that works? Many forces drive us away from clean code, and even from code that works. Without taking too much counsel of our fears, here's what we do: we drive development with automated tests, a style of development called Test-Driven Development (TDD). In Test-Driven Development, we

  • Write new code only if an automated test has failed

  • Eliminate duplication

These are two simple rules, but they generate complex individual and group behavior with technical implications such as the following.

  • We must design organically, with running code providing feedback between decisions.

  • We must write our own tests, because we can't wait 20 times per day for someone else to write a test.

  • Our development environment must provide rapid response to small changes.

  • Our designs must consist of many highly cohesive, loosely coupled components, just to make testing easy.

The two rules imply an order to the tasks of programming.

  1. Red?/span> Write a little test that doesn't work, and perhaps doesn't even compile at first.

  2. Green?/span> Make the test work quickly, committing whatever sins necessary in the process.

  3. Refactor?/span> Eliminate all of the duplication created in merely getting the test to work.

Red/green/refactor梩he TDD mantra.

Assuming for the moment that such a programming style is possible, it further might be possible to dramatically reduce the defect density of code and make the subject of work crystal clear to all involved. If so, then writing only that code which is demanded by failing tests also has social implications.

  • If the defect density can be reduced enough, then quality assurance (QA) can shift from reactive work to proactive work.

  • If the number of nasty surprises can be reduced enough, then project managers can estimate accurately enough to involve real customers in daily development.

  • If the topics of technical conversations can be made clear enough, then software engineers can work in minute-by-minute collaboration instead of daily or weekly collaboration.

  • Again, if the defect density can be reduced enough, then we can have shippable software with new functionality every day, leading to new business relationships with customers.

So the concept is simple, but what's my motivation? Why would a software engineer take on the additional work of writing automated tests? Why would a software engineer work in tiny little steps when his or her mind is capable of great soaring swoops of design? Courage.

I l@ve RuBoardPrevious Section Next Section

 

转载于:https://www.cnblogs.com/laolaowhn/archive/2011/01/26/1945514.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值