惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
Latest news
Latest news
博客园 - 三生石上(FineUI控件)
美团技术团队
aimingoo的专栏
aimingoo的专栏
Google Online Security Blog
Google Online Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threatpost
Y
Y Combinator Blog
T
Tailwind CSS Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
C
Cyber Attacks, Cyber Crime and Cyber Security
小众软件
小众软件
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Tenable Blog
W
WeLiveSecurity
L
LINUX DO - 热门话题
D
Docker
Cyberwarzone
Cyberwarzone
量子位
A
About on SuperTechFans
The Last Watchdog
The Last Watchdog
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Full Disclosure
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
T
The Exploit Database - CXSecurity.com
Engineering at Meta
Engineering at Meta
O
OpenAI News
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
IT之家
IT之家
S
Secure Thoughts
MongoDB | Blog
MongoDB | Blog
L
Lohrmann on Cybersecurity
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News

博客园 - laolaowhn

jQuery in Action 中文版 第一章 jQuery简介 如翻译的不好请原谅 jQuery in Action 中文版 简介 专用卸载工具 Windows Installer Clean Up Uninstall 临时表 中文转拼音源代码 ([^<]*) - laolaowhn - 博客园 IIS6架设网站常见问题及症状举例答疑 - laolaowhn - 博客园 jquery验证表单,可以独立与文框,利jquery动态控制,源代码 vs2005 js 没有反应 - laolaowhn {JS}JavaScript使用正则表达式 JavaScript 正则表达式 验证网址 JavaScript下正则表达式密码强度验证 悟透JavaScript引子 prototype学习心得 prototype源码分析—enumerable Prototype 框架分析(一) Prototype.js的分析 我为什么选择mootools,抛弃了prototype. (mootools 与 prototype 核心代码分析) prototype源码分析
Clean code that works (simplicity is more complicity than you think 朴素比你想象的更复杂)
laolaowhn · 2011-01-26 · via 博客园 - laolaowhn

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


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.

    posted @ 2011-01-26 15:17  laolaowhn  阅读(369)  评论()    收藏  举报