带检查的显式行为与隐式行为
我不确定如何提出这个问题,但我很想知道你们对以下情况有何看法以及您更喜欢哪一种。
我们正在使用 winforms 开发客户端-服务器应用程序。我们有一个控件,它在填充另一个字段时自动计算一些字段。因此,我们有一个字段货币,当用户填写时,它会确定自动填充另一个字段,也许更多字段。
当用户填写货币字段时,将根据用户引入的字符串从缓存中检索货币对象。如果在缓存中未找到输入的货币,则缓存对象将返回空引用。再往下,当要求应用程序层根据货币计算其他字段时,如果货币为空,则将返回空特定字段。这样,默认的隐式行为就是清除所有字段。这是预期的行为。为了更清楚地说明,当用户输入“不可用的货币”时,他当然会收到通知,但依赖于输入的货币的字段也应该被清除。这是通过将特定控制值设置为空来完成的。
我所说的显式实现是验证货币对象是否为空,在这种情况下,依赖字段将被显式清除。
我认为后一个版本更清晰,更不容易出错并且更易于测试。但这意味着某种形式的冗余。 前一个版本不太清楚,它暗示了应用程序层的某些行为,而这些行为在测试中没有表达。也许在较低层的测试中,但是当需要修改较低层时,以便给定一个空货币,应该返回其他东西,我不认为一个没有动机的测试会成为一个障碍在上层引入一个错误。
你们觉得怎么样?
I'm not sure how to construct the question but I'm interested to know what do you guys think of the following situations and which one would you prefer.
We're working at a client-server application with winforms. And we have a control that has some fields automatically calculated upon filling another field. So we're having a field currency which when filled by the user would determine an automatic filling of another field, maybe more fields.
When the user fills the currency field, a Currency object would be retrieved from a cache based on the string introduced by the user. If entered currency is not found in the cache a null reference is returned by the cache object. Further down when asking the application layer to compute the other fields based on the currency, given a null currency a null specific field would be returned. This way the default, implicit behavior is to clear all fields. Which is the expected behavior. Just to make it more clear, when the user enters a "not available currency" he gets notified, of course, but also the fields that depend on the currency entered should be cleared out. This is done by setting the specific control values to null.
What i would call the explicit implementation would be to verify that the Currency object is null in which case the depending fields are cleared explicitly.
I think that the latter version is more clear, less error prone and more testable. But it implies a form of redundancy.
The former version is not as clear and it implies a certain behavior from the application layer which is not expressed in the tests. Maybe in the lower layer tests but when the need arises to modify the lower layers, so that given a null currency something else should be returned, i don't think a test that says just that without a motivation is going to be an impediment for introducing a bug in upper layers.
What do you guys think?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我了解,通过明确的方式,您可以通过一点冗余来获得可读性,但我总是更喜欢这种方式,而不是一些晦涩的、神奇的行为(乍一看并不明显的东西)。
As I understand the whole thing, with the explicit way you get readability with a little redundancy, but I always prefer this over some obscure, magic behavior (something not obvious on first sight).
首先,迪努·弗罗林(Dinu Florin)是对的,明确的通常比隐含的要好(但一刀切不能放之四海而皆准,所以也有例外)。
其次,我不确定我是否正确理解你的问题,但你可能想看看 Null -对象模式。这个想法是,即使没有可用数据,您的后端也应该始终返回有效的数据对象(在您的情况下为货币对象)。在这种情况下,将返回一个特殊的空对象。该对象实现与普通数据对象相同的接口,但它可能包含字段的特殊值。它的设计方式应使您的应用程序不必区分有效数据对象和空对象。
我不知道这是否适合您的需求,也可能不适用于您的情况,但您必须自己确定。
First of, Dinu Florin is right, explicit usually is better than implicit (but one size does not fit all, so there are exceptions).
Second, I am not sure if I understood your problem correctly, but you may want to take a look at the Null-Object Pattern. The idea is, that your backend should always return a valid data-object (Currency-object in your case), even if no data is available. In that case, a special Null-Object is returned. This object implements the same interfaces as a normal data-object, but it may contain special values for the fields. It should be designed in such a way, that your application does not have to distinguish between a valid data-object and a Null-object.
I do not know if that may suit your needs, and it may just not be applicable in your case, but you have to determine that for yourself.
我理解你的问题的方式是你正在比较是否应该依赖 NullReferenceException 还是 null 对象来确定下一步操作。
我的想法是:
希望有帮助
The way I understand your question is you are comparing whether you should rely on NullReferenceException or null object to determine the next action.
Here are my thoughts:
Hope that helps