简体   繁体   English

ABAP 中的断言

[英]Assertions in ABAP

Over the years I've written code in a variety of languages and environments, but one constant seemed to be the consensus on the use of assertions.多年来,我用各种语言和环境编写代码,但一个常数似乎是对断言使用的共识。 As I understand it, they are there for the development process when you want to identify "impossible" errors and other situations to which your first reaction would be "that can't be right" and which cannot be handled gracefully, leaving the system in a state where it has no choice but to terminate.据我了解,当您想识别“不可能的”错误和其他情况时,它们会在开发过程中出现,您的第一反应是“不可能是正确的”并且无法正常处理,从而使系统处于它别无选择只能终止的状态。 Assertions are easy to understand and quick to code but due to their fail-fast nature are unsuitable for development code.断言易于理解和快速编码,但由于其快速失败的性质不适合开发代码。 Ideally, assertions are used to discover all development bugs and then removed or turned off when shipping the code.理想情况下,断言用于发现所有开发错误,然后在交付代码时删除或关闭。 Input or program states that are wrong, but possible (and expected to occur) should instead be handled gracefully via exceptions or other error handling techniques.应该通过异常或其他错误处理技术优雅地处理错误但可能(并且预计会发生)的输入或程序状态。

However, none of this seems to hold true for writing ABAP code for SAP.然而,对于为 SAP 编写 ABAP 代码,这一切似乎都不成立。 I've just spent the better part of an hour trying to track down the precise location where an assert was giving me an unintelligible error.我刚刚花了一个小时的大部分时间试图追踪断言给我带来难以理解的错误的精确位置。 This turned out to be five levels down in standard SAP code, which is apparently riddled with ASSERT statements.结果证明这在标准 SAP 代码中低了五个级别,显然充满了ASSERT语句。 I now know that a certain variable identifying a table IS NOT INITIAL while its accompanying variable identifying a field is.我现在知道标识表的某个变量IS NOT INITIAL而其伴随的标识字段的变量是IS NOT INITIAL值。

This tells me nothing.这没有告诉我什么。 The Web Dynpro component running this code actually "catches" this assert, showing me a generic error message, which only serves to prevent the debugger from launching when the assert is tripped.运行此代码的 Web Dynpro 组件实际上“捕获”了此断言,向我显示了一条通用错误消息,该消息仅用于在断言被触发时阻止调试器启动。

My question therefore is what the guidelines or best practices are for the use of assertions in ABAP.因此,我的问题是在 ABAP 中使用断言的指南或最佳实践是什么。 Is this SAP writing bad code?这是 SAP 写的糟糕代码吗? Is it an accepted practice to fill your custom code with asserts and leave them in when shipping the code?使用断言填充自定义代码并在交付代码时保留它们是否是公认的做法? If so, how would we go about handling these asserts in runtime so that the application doesn't crash and burn while still being able to identify the cause of the error?如果是这样,我们将如何在运行时处理这些断言,以便应用程序不会崩溃和烧毁,同时仍然能够识别错误的原因?

The guidelines and best practices are virtually the same in ABAP development as in any other language. ABAP 开发中的指南和最佳实践实际上与任何其他语言中的相同。 Assertion should be used as internal guidance checks only, exceptions for regular input validation errors and other stuff.断言应仅用作内部指导检查、常规输入验证错误和其他内容的异常。 It might be sensible to leave the assertions in the code - after all, you'd probably rather want your program to crash in a controlled fashion than continue in an unforeseen way and probably damage some critical data in the process without anyone noticing.将断言留在代码中可能是明智的 - 毕竟,您可能希望程序以受控方式崩溃,而不是以不可预见的方式继续运行,并且可能会在没有任何人注意到的情况下损坏过程中的某些关键数据。 Take a look at checkpoint groups if you don't want your program to abort in a production environment - but in my opinion: What's the use of a sanity check (as a last line of defense) if it's disabled in the environment where it matters most?如果您不希望您的程序在生产环境中中止,请查看检查点组- 但在我看来:如果在重要的环境中禁用了健全性检查(作为最后一道防线)有什么用大多数?

Of course I'm assuming that the input is validated properly (so that crashes are prevented) and that all APIs are used according to the intended use and documentation.当然,我假设输入已正确验证(以便防止崩溃),并且所有 API 均根据预期用途和文档使用。 Unfortunately - as with every other programming language - it's up to the developer to live up to these standards.不幸的是 - 与其他所有编程语言一样 - 由开发人员来实现这些标准。

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

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