是否有这个“图案”的名称? [英] Is there a name for this "pattern"?

查看:108
本文介绍了是否有这个“图案”的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个这种模式的名称,其中方法签名称为TrySomething,例如。 int.TryParse decimal.TryParse 等。

I'm wondering if there is a name for this "pattern" where a method signature is called TrySomething, e.g. int.TryParse, decimal.TryParse, etc.

我的同事经常使用这个命名约定,而不是返回值或抛出异常,他们会调用方法 TryDoSomething ,如果在处理过程中遇到异常通过输出参数返回。

A coworker of mine uses this naming convention frequently -- rather than returning a value or throwing an exception, they will call a method TryDoSomething and if an exception is caught during processing it gets returned via out param.

编辑:我明白隐藏的示例不是TryParse方法的工作原理。这是发布这一点的点...我不知道该怎么说。我同意它似乎更像一个命名约定,而不是一个模式。感谢所有的输入。

I understand the example privided is not how the TryParse methods work. That was the point of posting this... I wasn't sure what to call it. I agree it seems more like a naming convention and not a pattern. Thanks for all the input.

编辑:有趣的... ...

Interesting...

考虑可能在常见情况下抛出异常的成员的TryParse模式,以避免与异常相关的性能问题。 p>

Consider the TryParse pattern for members that may throw exceptions in common scenarios to avoid performance problems related to exceptions.


为了实现TryParse模式,您可以提供两种不同的方法来执行在常见场景中抛出异常的操作。第一种方法X,在适当时进行操作并抛出异常。第二种方法TryX不会抛出异常,而是返回一个指示成功或失败的布尔值。通过使用out(ByRef in Visual Basic)参数返回成功调用TryX返回的任何数据。 Parse和TryParse方法是这种模式的例子。

To implement The TryParse pattern, you provide two different methods for performing an operation that can throw exceptions in common scenarios. The first method, X, does the operation and throws the exception when appropriate. The second method, TryX, does not throw the exception, but instead returns a Boolean value indicating success or failure. Any data returned by a successful call to TryX is returned using an out (ByRef in Visual Basic) parameter. The Parse and TryParse methods are examples of this pattern.


推荐答案

我可能会称之为 错误隐藏模式 。当您有代码通常会产生异常时,您可以创建一个 TryX ,而不用早期使用布尔值。如果您查看框架提供的方法,您会注意到 TryX 变体存在,只要它是不平凡的或容易出错的(或经常应该是在框架中)编写自己的 IsValidX 方法。

I would likely call that the Error Hiding Pattern. You benefit from creating a TryX when you have code that would usually produce an exception that instead cuts out early with a boolean. If you look at the methods provided by the framework you'll notice the TryX variants exist whenever it would be non-trivial or error-prone (or done so often it should be in the framework) to write your own IsValidX method.

如果你必须捕获一个异常,没有感觉在一个方法中包装它。您正在做的是使调试输入问题变得更加困难。用户可能只看到失败方法的副作用,而不是跟踪输入错误跟踪跟踪踪迹。更糟糕的是,当调试手头的问题时,开发人员可能需要重新创建钝的程序状态才能实现故障模式。

If you have to catch an exception, there is no sense wrapping this in a method. All you are doing is making it harder to debug input problems. Instead of having a nice stack trace to follow to trace down an input failure, the user may only see side effects of the failed method. Worse still, when debugging the issue at hand the developer potentially has to recreate obtuse program state in order to effect the failure mode.

基本上,如果先验你知道一个操作将失败并且有一个例外,提供一个伴侣 TryX 操作是合乎逻辑的。这就是所谓的 Tester-Doer 模式。 事件分析操作不是 Tester-Doer ,而只是简单的异常处理

Basically, if a priori you know an operation will fail with an exception, it is logical and proper to provide a companion TryX operation. This is the so-called Tester-Doer pattern. Post hoc analysis of an operation is not Tester-Doer, but rather just simple Exception Handling.

这篇关于是否有这个“图案”的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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