MATLAB 消息 ID 的最佳实践?

发布于 2024-09-06 20:48:51 字数 298 浏览 3 评论 0 原文

创建 MATLAB 异常(MException 对象)或打印警告或错误消息时,MATLAB 允许您提供定义要抛出的异常的消息 ID。

消息ID的格式为:

component:mnemonic

例如,MATLAB自己的未定义变量消息ID为:

MATLAB:dispatcher:nameConflict

那么当您在自己的代码中使用异常时,您使用什么消息ID?您是否重用 MATLAB 的默认值?自己编吗?组件和助记符字符串使用什么?

When creation a MATLAB exception (MException object) or printing a warning or error message, MATLAB lets you supply a message ID that defines the except you're throwing.

The message ID is in the format:

component:mnemonic

For example, MATLAB's own undefined variable message ID is:

MATLAB:dispatcher:nameConflict

So when you use exceptions in your own code, what do you use for a message ID? Do you reuse MATLAB's default ones? Make up your own? What do you use for the component and mnemonic strings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傻比既视感 2024-09-13 20:48:51

我通常遵循这种错误(或警告)模式 消息标识符,其中括号中的内容可能存在也可能不存在:

(className):(parentFunction):functionWhereErrorOccurs:descriptiveMnemonic

组成部分是:

  • className:类的名称,如果发生错误的函数是方法/构造函数。

  • parentFunction:如果发生错误的函数是 m 文件中的子函数嵌套函数,这将是 主 m 文件函数 或嵌套函数的父函数。因此,您可以有多个 parentFunction 组件。

  • functionWhereErrorOccurs:该组件的名称非常不言自明。 ;)

  • descriptiveMnemonic:我强调描述性。例如,inputError 并没有真正告诉我任何信息,但是 notEnoughInputs 清楚地表明我没有传递足够的参数。我总是使用小驼峰式大小写作为助记符,其中单词的第一个字母除了第一个单词外都大写。

classNameparentFunction 组件可以被认为有些多余,因为stack 属性 .mathworks.com/access/helpdesk/help/techdoc/matlab_prog/bq9l448-1.html#bq9l5ac" rel="noreferrer">MException 类 已标识父级的完整路径m 文件和错误的行号。但是,消息的目的之一标识符的优点是它允许您唯一识别错误,而不仅仅是为了追查错误的来源。

假设您有一个函数 myFcn 和一个重载 myFcn 的类 myClass。如果您将第一个错误消息标识符设置为 myFcn:maxIterationsReached 并将第二个错误消息标识符设置为 myClass:myFcn:maxIterationsReached,这将允许您,例如,使用 DBSTOP 设置一个断点来停止仅当此错误由 myClass\myFcn 而不是 myFcn 产生时才执行。同样,唯一的警告消息标识符很有用,因为您可以专门选择忽略来自特定函数的警告,同时显示其他函数。

此外,您还可以在标识符中包含组件,指示发生错误的函数位于 包文件夹私人文件夹(但这可能会导致标识符相当长)。

I generally follow this pattern for error (or warning) message identifiers, where things in parentheses may or may not be present:

(className):(parentFunction):functionWhereErrorOccurs:descriptiveMnemonic

The components are:

  • className: The name of the class, if the function where the error occurs is a method/constructor.

  • parentFunction: If the function where the error occurs is a subfunction in an m-file or a nested function, this would be the primary m-file function or the parent of the nested function, respectively. You could therefore have multiple parentFunction components.

  • functionWhereErrorOccurs: The name of this component is pretty self-explanatory. ;)

  • descriptiveMnemonic: I stress descriptive. For example inputError doesn't really tell me anything, but notEnoughInputs makes it clear that I didn't pass enough arguments. I always use lower camel case for the mnemonic, where the first letter of a word is capitalized except for the very first word.

The className and parentFunction components could be considered somewhat redundant, since the stack property of the MException class already identifies a complete path to the parent m-file and the line number of the error. However, one of the purposes of a message identifier is that it allows you to uniquely identify an error for purposes other than just hunting down the source of the error.

Let's say you have a function myFcn and a class myClass that overloads myFcn. If you make an error message identifier for the first one be myFcn:maxIterationsReached and an error message identifier for the second one be myClass:myFcn:maxIterationsReached, this would allow you to, for example, set a breakpoint with DBSTOP that halts execution only when this error is produced by myClass\myFcn and not myFcn. Likewise, unique warning message identifiers are useful in that you can specifically choose to ignore warnings from specific functions while letting others be displayed.

Additionally, you could also include components in the identifier indicating that the function where the error occurs is located in a package folder or a private folder (but this might make for a rather long identifier).

茶色山野 2024-09-13 20:48:51

在我的工作中,我使用 YMA:(mainFunctionName):(descriptiveMnemonic),其中 YMA 只是我的姓名缩写。例如,我的 UIInspect 实用程序 的 ID 类似于 YMA:uiinspect:XXX

In my work I use YMA:(mainFunctionName):(descriptiveMnemonic), where YMA are simply my initials. For example, all the warnings and errors invoked in my UIInspect utility have IDs similar to YMA:uiinspect:XXX.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文