Improve Code by Removing It

本文探讨了一种通过去除代码中不必要的功能来简化软件实现、提高产品性能和减少全局代码熵的方法。作者分享了代码冗余产生的原因,并提供了一些避免过度设计和遵循YAGNI原则的建议。

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

Improve Code by Removing It

Pete Goodliffe

Less is more. It’s a quite trite little maxim, but sometimes it really is true. One of the improvements I’ve made to our codebase over the last few weeks is
to remove chunks of it.
We’d written the software following XP tenets, including YAGNI (that is, You Aren’t Gonna Need It). Human nature being what it is, we inevitably fell short in a few places.
I observed that the product was taking too long to execute certain tasks— simple tasks that should have been near instantaneous. This was because they were overimplemented—festooned with extra bells and whistles that were not required, but at the time had seemed like a good idea.
So I’ve simplified the code, improved the product performance, and reduced the level of global code entropy simply by removing the offending features from the codebase. Helpfully, my unit tests tell me that I haven’t broken any- thing else during the operation.
A simple and thoroughly satisfying experience.
So why did the unnecessary code end up there in the first place? Why did one programmer feel the need to write extra code, and how did it get past review or the pairing process? Almost certainly something like:
78 97 Things Every Programmer Should Know

• It was a fun bit of extra stuff, and the programmer wanted to write it. (Hint: Write code because it adds value, not because it amuses you.)
• Someone thought that it might be needed in the future, so felt it was best to code it now. (Hint: That isn’t YAGNI. If you don’t need it right now, don’t write it right now.)
• It didn’t appear to be that big an “extra,” so it was easier to implement it rather than go back to the customer to see whether it was really required. (Hint: It always takes longer to write and to maintain extra code. And the customer is actually quite approachable. A small, extra bit of code snow- balls over time into a large piece of work that needs maintenance.)
• The programmer invented extra requirements that were neither docu- mented nor discussed in order to justify the extra feature. The requirement was actually bogus. (Hint: Programmers do not set system requirements; the customer does.)
What are you working on right now? Is it all needed?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值