|
Post by account_disabled on Jan 28, 2024 23:24:00 GMT -5
本文由Deji Akala和Marco Pivetta进行了同行评审。感谢所有 SitePoint 的同行评审者使 SitePoint 内容达到最佳状态! Pause Next Unmute Current Time 0:07 / Duration 2:00 Fullscreen 当在同一代码库的大中型团队中工作时,有时会很难理解彼此的代码以及如何使用它。有多种解决方案可以帮助解决这个问题。例如,您可以同意遵循一组编码标准,以使代码彼此之间更具可读性,或者使用众所周知的框架(我们在这里有一个很棒的 Laravel 入门高级课程)。 然而,这通常是不够的,特别是当有人必须深入研究不久前编写的应用程序的一部分来修复错误或添加新功能时。很难记住特定类的目的是如何工作的,无论是单独的还是相互结合的。到那时,很容易在没有意识到的情况下意外引入副作用或错误。 检测到的错误 这些错误可能会在质量保证中被发现,但它们也有可能被漏掉。即使他们被抓住,也可能需要很长时间才能将代码发回并修复。 那么我们怎样才能防止这种情况发生呢?输入“防错”。 什么是防错? Poka Yoke 是一个日语术语,大致翻译为“防错”。该术语 起源于精益制造,指的是帮助机器操作员避免错误的任何机制。 除了制造业之外,防错技术也经常用于消费电子产品。以 SIM 卡为例,由于形状不 WhatsApp 号码数据 对称,它只能以一种方式安装在 SIM 卡托盘上。 SIM 卡和托盘,说明 SIM 卡只能以一种方式安装。 Poka Yoke 缺少的硬件示例是 PS/2 端口,该端口的键盘连接器和鼠标连接器的形状完全相同。它们只能通过颜色代码来区分,因此很容易意外地切换连接器并将它们插入错误的端口,因为它们的安装方式相同。 用于键盘和鼠标的 PS/2 端口,说明它们如何使从而很容易意外地将一个端口插入另一个端口。 除了用于硬件之外,Poka Yoke 的概念还可以应用于编程。我们的想法是让代码的公共接口尽可能容易理解,并在代码使用不正确时立即引发错误。这似乎是显而易见的,但实际上我们经常遇到缺乏这些方面的代码。 但请注意,防错并不意味着防止故意滥用。目标只是防止意外错误,而不是保护您的代码免遭恶意使用。只要有人可以访问您的代码,如果他们确实愿意,他们就始终能够绕过您的保护措施。 在讨论可以采取哪些具体措施使代码更加防错之前,重要的是要知道 Poka Yoke 机制通常可以分为两类: 预防错误 错误检测 错误预防技术有助于尽早发现错误。它们的目的是通过使接口和行为尽可能简单,确保没有人会意外地错误地使用我们的代码。以 SIM 卡为例,它只能以一种方式装入 SIM 卡托盘中。 另一方面,错误检测机制位于我们的代码之外。他们监视我们的应用程序以观察潜在的错误并向我们发出警告。例如,软件可以检测连接到 端口的设备类型是否正确,如果不正确,则向用户显示警告,说明其无法工作的原因。这种特殊的软件无法防止错误,因为连接器在插入时是可以互换的,但它可以检测到并警告我们,以便修复错误。 在本文的其余部分中,我们将探讨可用于在应用程序中实现错误预防和错误检测的几种方法。但请记住,此列表只是一个起点。
|
|