简体   繁体   English

拥有“这不应该发生”的陈述是一种好习惯吗

[英]Is it good practice to have a “this should never happen” statement

In python (and coding in general, really) is it good practice to have an error that should never happen, or is it just code clutter? 在python中(以及一般而言,实际上是在编码中),是否有永远不应该发生的错误是一种好习惯,还是仅仅是代码混乱?

eg 例如

if thing:
   num = 1
elif thing2:
   num = 2
else:
   num = 3

#Lots of other code goes here interacting with num, but not modifying it

if num == 1:
   option1()
elif num == 2:
   option2()
elif num == 3:
   option3()
else:
   #this should never happen
   print("Instead of being 1, 2, or 3, num was " + str(num))
   raise Exception("Error! num was an unexpected value!")

So basically, is including the final else (which should never happen) good practice, or just cluttering up the code? 因此,基本上,是包括最后的其他方法(永远不要发生)吗?还是仅仅是弄乱代码?

My real code is more complicated than this -- specifically, num is assigned in a main loop, then passed to a function. 我的实际代码比这复杂得多-具体来说,num是在主循环中分配的,然后传递给函数。 Maybe it's worth including then because the function might be called from somewhere else with a bad value for num? 也许值得包括在内,因为该函数可能是从其他位置调用的,而num的值不正确?

It is generally considered good practice to handle default conditions, even if unreachable in current code. 即使在当前代码中无法达到默认条件,通常也被视为处理默认条件的良好实践。 The reason for this is that code grows and transfers ownership with time, and what currently seems like an unreachable condition may well happen in the future. 原因是代码会随着时间的增长而增长并转移所有权,而当前看来无法达到的条件将来很可能会发生。 In this case it is better to have an exception raised, as you do, rather than have unspecified behavior. 在这种情况下,最好像您一样引发异常,而不是具有未指定的行为。

If there is a possibility the error ever could happen, due to another part of the system behaving incorrectly, or due to future maintainers making a mistake, then handling the error has value. 如果由于系统的另一部分行为不正确,或者由于将来的维护人员犯错,则有可能发生该错误,那么处理该错误就很有价值。

If the error is not possible by design, unchangeable system properties, or is guaranteed by previous processing that cannot change in a way that would lead to the error (without being obvious) then the check is clutter. 如果错误是无法通过设计,无法更改的系统属性或无法通过会导致错误(不会很明显)的方式更改的先前处理来保证的,则检查会很混乱。

If you make a change to the first chunk of code, and forget to update the second, you could get mildly undesirable results without a catastrophic, obvious effect indicating a problem. 如果您对第一段代码进行更改,而忘记更新第二段代码,则可能会得到不希望有的结果,而没有灾难性的明显影响,表明存在问题。

It would be best for it to fail entirely if you get bad data. 如果您收到错误的数据,最好完全失败。 If something must be done with the data in the second part, and receiving bad data alone wouldn't cause a massive failure, it would be safer to manually ensure failure so you can fix the code instead of allowing it to go unnoticed. 如果必须对第二部分中的数据进行某些处理,并且仅接收错误数据不会导致严重故障,那么手动确保故障会更安全,以便您可以修复代码而不是让代码不被注意。

Massive failure is better than silent failure in most circumstances. 在大多数情况下,大规模故障要比无声故障好。

Depends. 要看。 You don't necessarily want a user-facing message like that, but it helps to log that kind of thing. 您不一定想要这样的面向用户的消息,但是它有助于记录这种情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能在Python的TRY块中使用IF语句,如果可能的话,这是不错的做法? - Why is it not possible to have an IF statement in a TRY block in Python, and if somehow possible, is it good practice? 以...作为声明依赖python是一种好习惯 - Is it good practice to depend on python's with…as statement 为什么要使用 --user 标志,这是一个好习惯吗? - Why should I use --user flag, is it a good practice? 在if语句中调用函数/通过print()调用-好的做法? - Call Function in if-Statement/ evoke call with print() - good practice? 我有一个保存在 .py 中的 Python 代码,但命令没有按预期发生 - I have a Python code saved in .py but the commands dont happen as they should 如果我运行了fcgi服务器并使用浏览器访问它,应该怎么办? - If I have an fcgi server running and visit it with a browser, what should happen? 返回语句是否应该带有括号? - Should a return statement have parentheses? AssertionError:pygame 出了点问题。这永远不会发生。(导入健身房时) - AssertionError: Something went wrong with pygame. This should never happen.(When import gym) 在python中拥有一个抽象的父类和一个抽象的子类是一个好习惯吗? - Is it a good practice to have an abstract parent and an abstract child classes in python? 在 python 类中使用密集的 __init__ 方法是一个好习惯吗? - Is it a good practice to have an intensive __init__ method in a python class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM