|
Post by account_disabled on Jan 29, 2024 18:31:00 GMT 10
这是 2017 年 5 月 30 日的 最近发布了一个非常有趣的非 Laravel 视频,内容涉及视觉债务。它只有 3 分钟长,所以请在继续阅读之前花点时间观看。 参考视频中的静态屏幕 完毕?好吧,下面有剧透。 在视频中,Jeff 首先编写了一些冗长的基于事件的代码,其中包含一些侦听器、触发方法、基于各种类的接口,以及随处可见的大量类型和返回提示。 当他滚动浏览代码时,他声称其中充满了视觉债务。Jeff 然后继续解释说,虽然 PHP 确实提供了类型和返回提示,但它们并不是必需的,并立即将它们从所有方法签名中删除以减少代码量。“没有他们,我们在很长一段时间内都繁、太早地接触它们,给他们的代码增加了不必要的重量。 在看似最后的剪辑中,他从类中删除了“final”关键字,认为保护开发人员免受自己的伤害是愚蠢的,但出乎意料的是,他甚至还删除了public其余方法前面的关键顺便说一句,这是隐含的public。 最终结果?在这个简单的示例中,代码大约减少了 25%。但债务消失了吗? 如果你在某种程度上听过我的咆哮,你就会知道我非常反对传统,并且赞成冗长。因此,当 PHP 中出现正确的类型提示时,我感到非常兴奋。 在处理其他人的代码时,我遇到的错误数量和浪费的时间是巨大的,因为他们没有明确表明期望哪种类型的值作为方法的输入或输出,并且只是拥有对我来说,能够在工作时完成良好的自动静态检查以及正确的、详细的 IDE 自动 WhatsApp 号码数据 完成提示,值得全面过渡到最新的 PHP 版本。 在团队中工作时,对代码的更多控制意味着更少的人为错误,我完全赞成这一点。时间是宝贵的,将时间浪费在可预见的错误上是彻头彻尾的罪恶。我不同意杰夫从他的代码中删除这些内容。很长一段时间里,没有它们,我们也过得很好,但即使没有很多现在让我们的生活变得更好的东西,我们也过得很好。 对我来说,它们不会带来视觉债务——没有它们我会感到欠债,因为我知道我可以通过防止技术债务的方式从它们中受益。 我确实同意他删除界面的观点。在视频中的情况下,它没有提供任何切实的好处,尽管这可能是由于示例代码本身造成的。他选择了 Events 作为示例,视频中描述的 Event 类实际上本身就是相当不错的界面。 一般来说,我认为如果您正在构建一些旨在作为扩展或直接替代某些内容进行开发的东西,或者当您使用的包使用接口来确保签名匹配时,接口是可以的,但除此之外,它们确实增加了相当多的代码重量。我记得我的旧 Zend Framework 1 天时,框架目录结构的 20 层深处有核心接口,没有主体 - 只有一个类和空大括号。简而言之,代码官僚主义。 删除final是完全合理的。阻止人们扩展课程(无论如何很容易规避)似乎毫无意义,如果它是作为团队内的预防措施来控制同事,那么它就像备忘录一样有效。
|
|