如何处理无效的执行状态? [英] How to handle an invalid execution state?

查看:217
本文介绍了如何处理无效的执行状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有以下代码块:

if (Light.On) {
    // do something...
} else if (Light.Off) {
    // do something else...
} else {
    // this state should never be reached
}

现在假设应用程序逻辑规定在这部分代码中,最后一个状态应该是永远不会到达,但它不是可以在编译时确定的东西。也许还有其他状态(例如 Light.Broken ),可以由应用程序的其他部分设置,但这里没有使用。

Now assume that the applications logic dictates that in this part of the code, the last state should never be reached, but it is not something that can be determined at compile time. And maybe there ARE other states (e.g. Light.Broken) which could be set by other parts of the application, but which are not used here.

您在最后一个 else 区块中添加了哪些代码?

What code do you add in the last else block?


  1. 不添加任何代码,因为无论如何都不应该到达。

  2. 添加一些日志记录功能,以便您作为开发人员知道某些非法状态已经到达。

  3. 抛出一个异常,因为一定不能达到状态,无论如何都达到了,其他东西一定是错的。

第一种选择对我来说似乎不合理,希望正确的事情似乎不是正确的选择。选项二的优势在于您的应用程序不会立即崩溃,因此如果在极少数情况下发生这种情况而未在测试中发现,则客户可以继续使用该应用程序,并通知开发人员该问题。选项三会导致应用程序崩溃,这显然不是您希望客户体验的内容,但它确实表明某些内容有问题。

The first option doesn't seem reasonable to me, hoping something goes right seems hardly the right choice. Option two has the advantage that your app doesn't crash right away, so if this happens in a rare occasion which was not caught in testing, the customer can continu using the application and the developer is notified of the problem. Option three causes the app to crash, which obviously is not something you want your customers to experience, but it does make it very clear that something is wrong.

处理这种情况的最佳方法是什么?

What is the best way to handle such a situation?

编辑,基于评论:

引导讨论的一些额外考虑因素:

Some additional consideration to steer the discussion:


  • 包含上述代码的方法的合同不允许在此时设置任何其他值,然后打开和关闭。

  • 假设代码位于应用程序的一个不太关键的部分。

推荐答案

在开发中 - 努力工作并快速失败。抛出某种运行时异常,或者只是Assert(false)。

In development - fail hard and fail fast. Throw some kind of runtime exception, or just Assert(false).

在发布中 - 优雅地关闭。您的应用程序处于无法使用的状态,并且您无法确定您通常会遇到的任何事情,例如类不变量等。让用户有机会保存他们的工作,例如,尝试并记录可能会发送回的错误开发团队然后关闭。

In a release - shutdown gracefully. Your app is in an unusable state, and you cant realy on anything that you normally would, such as class invariants etc. Give the user a chance to save thir work, for example, try and log an error that could be sent back to the dev team and then shutdown.

编辑:根据添加的评论。

Based on comments added.

如果函数的合同声明在功能输入处打开或关闭灯光然后任何其他状态都是错误。根据我原来的答案中概述的原则,该函数应该失败。

If the contract of the function states that the light shouold be on or off at function entry then any other state is an error. The function should fail, according to the principles outlined in my original answer.

关于'非关键'aspaect - 如果未满足函数前置条件,则意味着你的申请被打破了。无论在非关键代码段中检测到错误,这并不意味着问题本身并不重要 - 您无法知道创建无效状态的错误也不会影响代码的关键区域。

Regarding the 'non-critical' aspaect - if a function precondition is not being met that means that your application is broken. Regardless of wether the error is detected in a non-critical piece of code, that does not imply that the problem itself is non-critical - you have no way of knowing that the bug that creates the invalid state doesnt also affect critical areas of code.

这篇关于如何处理无效的执行状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆