你真的需要它吗?——代码精简的实用原则

#Tech

本文介绍了“你真的需要它吗?

”(DYSNI)这一代码精简原则,它与“你不需要它”(YAGNI)类似,旨在鼓励开发者在完成项目后,主动审查并移除不再使用的代码。

作者通过一个 Objective-C 代码清理的实际案例,展示了如何通过反复质疑代码的必要性,逐步简化代码结构,最终减少了代码行数并提升了可维护性。

案例涉及移除冗余的抽象层级、精简回调函数签名(completion handler,例如 `void (^)(NSDictionary *userInfo, BOOL success, NSError *error)`)以及删除不必要的实例变量(ivar)。

DYSNI 是一种持续改进代码库的有效方法,虽然可能会在代码评审中引起争议,但长期来看能减轻维护负担。

查看原文开头(英文 · 仅前 3 段)

29 Oct 2025Most of us are familiar with YAGNI (“You Aren’t Gonna Need It”), that old developer mantra reminding us not to add code or functionality until it’s truly needed.

But if we have a mantra for not adding unnecessary code, it seems only fitting we have one for removing code that’s outlived its usefulness.

That’s where my internal monologue often kicks in: DYSNI - Do Ya Still Need It?

※ 出于版权考虑,仅引用前 3 段。完整内容请阅读原文。

阅读原文 ↗