如何避免副作用?

发布于 2024-12-12 01:12:22 字数 823 浏览 0 评论 0原文

为避免副作用: 我不想在我的方法中更改 pramaeter 对象属性值。我更喜欢创建一个新的来表示结果并返回它。

避免副作用仅在多线程环境中才真正重要,但仍然是一个好的实践。 f(X)=Y ,能够比较两个对象(作为参数接收的对象和作为结果给出的对象)是件好事。

这意味着,我需要将 IN 参数对象中的所有属性映射到创建的 OUT 新实例对象。这将生成一些代码,例如:

groundCertResult.setExpirationDate(pGroundCertificate.getExpirationDate());
groundCertResult.setValidationDate(pGroundCertificate.getValidationDate());
groundCertResult.setId(pGroundCertificate.getId());

它可以在我的方法中生成很多行:$

我也可以不映射那么多值,只需添加一行

//use findByID without mapping that much 
final GroundCertificate groundCertResult =
    groundCertificateDao.findById(pGroundCertificate.getId());

但这最终会在对数据库的访问,以及更多的处理。

是否使用反射进行一些通用方法,将一个对象映射到另一个对象?在性能方面,这将是昂贵的。

你会选择什么?根据项目需要而定。考虑到可维护性、可扩展性......

To avoid side effect:
I don't want to change the pramaeter object attribute values, in my method. I prefer to create a new one that will represent the result and return it.

Avoiding side effect is only really important in multithread environment, but still good practice to have. f(X)=Y ,is good to be able to compare both objects, the one u received as argument and the one u giving as result.

That means, I need to map all the attribute in my IN paramater object to my OUT new instance object created. This will make some code like:

groundCertResult.setExpirationDate(pGroundCertificate.getExpirationDate());
groundCertResult.setValidationDate(pGroundCertificate.getValidationDate());
groundCertResult.setId(pGroundCertificate.getId());

that it can make a lot of lines in my method :$

I can also do without mapping that much values, just adding a line

//use findByID without mapping that much 
final GroundCertificate groundCertResult =
    groundCertificateDao.findById(pGroundCertificate.getId());

but this will end up in an access to DB, and more processing.

Do some generic method using reflection, to map an object to an other? That will be costy, in performance.

What will u choose? Depnding on the project needs. Taking into consideration maintainability, sacalability...

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

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

发布评论

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

评论(2

渔村楼浪 2024-12-19 01:12:22
  1. 您可能想看看 Cloneable 和 Object.clone()。如果对象的浅拷贝就足够了,则不需要编写任何额外的代码。 Object.clone() 将为您完成此操作。
  2. 确保您确实想要/需要这种方法。如果您不确定否则会发生什么/是否会产生副作用,请检查您的代码以找出答案。
  3. 保持一致。不要在实现中混合复制和修改操作。
  4. 考虑一下如果将来您或某人想要使用您的 GroundCertificate 的后代,需要做什么。
  1. You may want to have a look at Cloneable and Object.clone(). If a shallow copy of your object is sufficient you don't need to write any additional code. Object.clone() will do that for you.
  2. Make sure you really want/need this approach. If you are not sure what/if side effects might occur otherwise, review your code to find out.
  3. Be consistent. Don't mix copying and modifying operations in your implementation.
  4. Think about what needs to be done if, in the future, you or someone wants to use a descendant of your GroundCertificate.
不顾 2024-12-19 01:12:22

它不仅适用于多线程环境,而且大大降低了导致问题的副作用风险。

副作用增加了推理代码实际作用的难度。您需要确定所有可以更改状态的位置,如果不费力地检查所有内容并确定这些位置,就无法做到这一点。 “远距离的幽灵行动。”

有几个库可以执行各种类型的 bean 属性复制,例如 Apache 的 BeanUtils。从效率的角度来看是否值得取决于具体情况。

虽然这不是一种“纯粹”的方法,但我也不强烈反对返回修改后的参数对象,只要清楚原始对象正在被修改即可。通常情况下,我都会将参数对象设置回该值。当我不这样做时,我尝试通过链接某种复制功能来使代码指示。

最终,对我来说,一切都与清晰度有关,当我被迫这样做时,我会尽可能地选择清晰度而不是纯粹性,而且这是安全的。

It's not only good in a multi-threaded environment, although the risk of side-effects causing problems are greatly reduced.

Side-effects increase the difficulty of reasoning about what code actually does. You need to identify all the places state can be changed, and there's no way to do that without slogging through everything and identifying those places. "Spooky action at a distance."

There are several libraries that do various types of bean property copies, like Apache's BeanUtils. Whether not not it's worth it from an efficiency standpoint depends on the situation.

While it's not a "pure" approach, I'm also not violently opposed to returning a modified parameter object, as long as it's clear that the original object is being modified. More often than not, I'm setting the parameter object back to that value anyway. When I'm not, I try to make the code indicate either by chaining in some sort of copy functionality.

Ultimately, for me, it's all about clarity, and I'll take clarity over purity when I can when I'm forced to do so, and it's safe.

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