简体   繁体   English

VERIFY(...)是C ++编码的好习惯吗?

[英]Is VERIFY(…) a good practice in C++ coding?

此外,与出现问题时抛出异常相比如何?

VERIFY() serves the same purpose as ASSERT() (or the standard library assert() ) - to let you catch things that really shouldn't ever™ be happening (ie a true code bug, something that should be fixed before release). VERIFY()ASSERT() (或标准库assert() )具有相同的目的 - 让您捕获真正应该发生的事情(即真正的代码错误,应该在发布之前修复) 。 The kinds of things that if for some reason the expression is false, there's no point to continuing because something is horribly, horribly wrong. 如果由于某种原因表达式是错误的,那么就没有必要继续下去,因为某些东西是可怕的,可怕的错误。

This is reflected in the fact that VERIFY() only stops the program on a false evaluation when compiling in Debug mode - in Release mode, it's transparent. 这反映在以下事实中: VERIFY()仅在调试模式下编译时停止程序进行错误评估 - 在发布模式下,它是透明的。 The difference between VERIFY() and ASSERT() is that VERIFY() will still evaluate the expression in Release mode, it simply won't care about the result - whereas ASSERT() is completely removed from the program when compiling in Release mode and thus any side-effects of the expression within it won't take place. 之间的差异VERIFY()ASSERT()VERIFY()而-仍将评估在Release模式的表达,它根本不会在意结果ASSERT()在Release模式编译时完全从计划中删除,并因此,表达中的任何副作用都不会发生。

Exceptions are more useful for things that might go wrong, but can be recovered from, since exceptions can be handled by other parts of the program. 异常对于可能出错的事情更有用,但可以从中恢复,因为异常可以由程序的其他部分处理。

The question looks simple, but hides a huge subject : dealing with errors. 问题看起来很简单,但隐藏了一个巨大的主题:处理错误。

Summarilly I would say assert and verify are tools to use in developement process while exceptions and system calls error checking are normal parts of production code to deal with runtime errors . Summarilly我会说断言验证是在开发过程中使用的工具,而异常和系统调用错误检查是生产代码的正常部分,用于处理运行时错误

  • Assertions (it applies to assert and verify) are tools mostly used in defensive programming style. 断言 (它适用于断言和验证)是主要用于防御性编程风格的工具。 Their main goal is to protect against cases that should never occurs and programmer has no idea of what to do if that happen. 他们的主要目标是防止应该永远不会发生的案件,如果发生这种情况,程序员也不知道该怎么做。 Usually it is used to check for programming errors. 通常用于检查编程错误。 When the impossible happen while debugging a program you usually want to detect and report it as fast as possible. 当调试程序时发生不可能的情况时,您通常希望尽快检测并报告它。 It will simplify error correction. 它将简化纠错。 You can use it with some condition or even as assert(false) typically to check that a branch of a switch is never caught. 您可以在某些条件下使用它,或者甚至通常assert(false)来检查交换机的分支是否从未被捕获。

    I mostly use assertions when dealing with third-party code whose behavior I'm unsure. 在处理我不确定的行为的第三方代码时,我主要使用断言。 For checking behavior of my own code code under development I usually prefer unit testing. 为了检查我自己的代码代码在开发中的行为,我通常更喜欢单元测试。

    Now, as Schroëdinger taught us, mesures change things. 现在,正如施罗丁格教导我们的那样,情绪改变了一切。 You may sometime have code that works in debug mode when assert is enabled and stop working in release mode. 当启用断言并停止在释放模式下工作时,您可能有时会在调试模式下运行代码。 It usually is the symptom of an hidden bug inside the assertion condition (side effect, time sensitive code). 它通常是断言条件中隐藏的错误的症状(副作用,时间敏感的代码)。 Verify minimize this kind of risk, but it can be considered a bad practice, like sweeping dust behind the carpet. 验证最小化这种风险,但它可以被认为是一种不好的做法,如在地毯后面扫除灰尘。

    You can also use verify for fast coding as scafolding for places where you didn't set up a real error handling yet. 对于尚未设置真正错误处理的地方,您还可以将验证用于快速编码作为scafolding。 I do not believe you should let any verify in production code. 我不相信你应该在生产代码中进行任何验证。

  • Exceptions are a normal part of program control flow. 例外是程序控制流程的正常部分。 Most often they are used to deal with errors, but it's not their only possible use (those who use other languages like python will know what I'm speaking about). 大多数情况下,它们习惯于处理错误,但它不是唯一可能的用法(使用其他语言如python的人会知道我在说什么)。 Neither should they be considered as the only error management tool. 它们也不应被视为唯一的错误管理工具。 When not wrapped in a c++ aware library, system calls still return an error code instead of raising an Exception. 如果未包含在c ++感知库中,系统调用仍会返回错误代码,而不是引发异常。

    As a rule of thumb Exceptions should be caught by the nearest object that is able to handle them sensibly. 根据经验,异常应该被能够合理处理它们的最近的对象捕获。

    Also not all exceptions should be considered as fatal errors and stop program. 也不是所有的例外都应被视为致命错误并停止计划。 Imagine you are dealing with a network video streaming library returning error cases through exceptions. 想象一下,您正在处理网络视频流库,通过异常返回错误情况。 It the library throw an Exception warning that a frame was dropped, you usually don't want to stop the program but just get it ready for the next frame. 它会抛出一个异常警告,表示帧已被删除,您通常不希望停止该程序,只是为下一帧做好准备。

    Even when the best thing to do is to stop the program you should not let exception do it for you (and you have even less reason to use assert for that purpose in production code). 即使最好的办法是停止程序,也不要让异常为你做(并且你甚至没有理由在生产代码中为此目的使用assert)。 There should always exist a top level exception handler for that purpose that makes explicit calls to exit() of like. 应始终存在一个顶级异常处理程序,用于显式调用exit()。 Not doing it merely show that the programmer didn't care about what may happen and probably didn't thought about it. 不这样做只是表明程序员并不关心可能发生的事情,也可能没有考虑过。

In Visual C++, there are two macros for checking conditions: ASSERT and VERIFY . 在Visual C ++中,有两个用于检查条件的宏: ASSERTVERIFY

In debug mode, they both behave the same: that is, they both evaluate their argunment and if the result is 0, they both halt the program with an assertion failure dialog box. 在调试模式下,它们的行为都相同:即,它们都评估它们的argunment,如果结果为0,它们都会使用断言失败对话框暂停程序。

The difference lies in release mode. 不同之处在于发布模式。 In release, mode, ASSERT is completely removed from the program: it doesn't evaluate it's expression at all. 在发布模式中, ASSERT完全从程序中删除:它根本不评估它的表达式。 VERIFY , on the other hand, still evaluates the expression, it just ignores the result. 另一方面, VERIFY仍然会对表达式求值,它只是忽略了结果。

Personally , my opinion is that if the check is valuable in debug mode then it's still valuable in release mode as well and you probably shouldn't use either of them. 就个人而言 ,我的观点是,如果检查在调试模式下很有价值,那么它在发布模式下仍然很有用,你可能不应该使用它们中的任何一个。 Just do the test and throw an exception (or use a custom macro that expands to assert() in debug mode and an exception in release mode). 只需执行测试并抛出异常(或使用在调试模式下扩展为assert()的自定义宏以及在发布模式下的异常)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM