将对象的引用传递给 java 中的方法是一个好习惯吗?

发布于 2024-11-19 13:55:26 字数 69 浏览 1 评论 0原文

将对象引用传递给方法(作为参数)并修改该方法中的对象是否可以接受,或者该方法应该返回对新对象的引用?最佳实践是什么?为什么?

Is it acceptable style to pass an object reference to a method (as a parameter) and modify the object in that method, or should that method return a reference to a new object? What is the best practice and why?

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

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

发布评论

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

评论(4

孤芳又自赏 2024-11-26 13:55:26

默认情况下,当您将对象传递给方法时,您是通过引用来执行此操作的。如果对象应该是不可变的,那么就这样设计它们(例如String)。

By default, when you pass objects to a method, you are doing so by reference. If objects should be immutable, design them as such (e.g. String).

不知在何时 2024-11-26 13:55:26

我想说最好的做法是修改方法的 this 对象。最好不要修改作为参数传递的对象,即使它们是可变的。如果要返回一个对象进行链接,通常最好return this;而不是参数。

I would say its best practice to modify the this object of the method. It is better not to modify the objects passed as arguments even if they are mutable. If you going to return an object for chaining, its usually best to return this; rather than an argument.

So要识趣 2024-11-26 13:55:26

是否应该将对象引用传递给方法并在方法中修改它们,或者让 >方法返回新引用?

它没有用..为什么要返回?

它应该主要在不可变对象的情况下返回。

例如

bigDecimalOb3 = bigDecimalOb1.add(bigDecimalOb2);

Should once pass the object reference to methods and modify them in methods or have the >methods return new references?

Its of no use.. why to return ??

It should return mostly in the case of immutable objects.

For example

bigDecimalOb3 = bigDecimalOb1.add(bigDecimalOb2);
永不分离 2024-11-26 13:55:26

避免“广泛”参数(引用透明度/意图清晰)

我会比接受的答案更进一步,并说(一般来说)不要将对象(或严格来说是对象引用)传递给方法作为参数。

在实践中,您最终会得到不明确的代码,正如我将尝试在以下业界工作人员所熟悉的示例中进行说明:

示例

比较这个:

Employee emp = ...;
Validator.checkIsEligibleForPPCTP(emp);

与这个:

Employee emp = ...;
Validator.checkIsEligibleForPPCTP(emp.getEmailAddress());

PPCTP< 到底是什么? /代码>?您需要了解 PPCTP 的领域知识。您所知道的是,它将查看员工对象并执行某些操作。

另一方面,如果您避免传递整个对象,而只传递实际相关的内容,则读者可以了解更多正在发生的情况,而无需检查 checkIsEligibleForPPCTP 的定义。我敢打赌,由于我们向验证方法传递了一个电子邮件地址,您可以更容易地想象该验证方法的作用(事实上,即使您的想象略有错误,当涉及到代码时,叙述通常就足够好了理解)。


进一步的想法

(弱)反驳

但是,如果我们将来需要这个广泛对象的另一个领域怎么办?您必须修改签名。

哦,太糟糕了,我不想让别人承担这个负担,没有人比我聪明(好吧,改变签名需要一些小心,但有比仅仅将宇宙传递给一个简单方法更好的解决方案)。

这条推文是“为未来”进行“预分解”/编程的答案(并且它适用到方法级设计):

错误的抽象比没有抽象更具破坏性。
每次等待都胜过猜测。

澄清

是的,我知道 java.lang.String 是一个对象。通常您必须传递java.util.Collection。它们与“自定义对象”不同,因为:

  • 字符串是不可变的
  • 集合作为参数传递时可以(并且应该)是不可变的副本
    • 如果不是,您的代码将更难理解,因为您的方法变得有状态/有副作用(即它是一个不纯的函数)

如果你坚持传递一个对象,至少让它不可变。但是,如果您的 emp 对象是 TLOYDEmployee,那么您再次无法理解代码的意图,并且您所要做的就是共同理解业务术语的含义(使用-重用悖论)。

Avoid "broad" parameters (referential transparency / clarity of intent)

I would go a step further than the accepted answer and say (in general) do not pass objects (or object references, strictly speaking) to methods as parameters.

In practice what happens is you end up with unclear code as I will attempt to illustrate in the following all-too-familiar example people working in the industry see:

Example

Compare this:

Employee emp = ...;
Validator.checkIsEligibleForPPCTP(emp);

with this:

Employee emp = ...;
Validator.checkIsEligibleForPPCTP(emp.getEmailAddress());

What the hell is a PPCTP? You need domain knowledge of whatever a PPCTP is. All you know is that it will look at the employee object and do something.

If, on the other hand, you avoid passing the whole object but just pass what is actually relevant, readers know more of what is happening without having to examine the definition of checkIsEligibleForPPCTP. I bet you can more easily imagine what that validation method does by virtue of the fact we are passing an email address to it (in fact, even if you are slightly wrong in your imagination, a narrative is often good enough when it comes to code comprehension).


Further thoughts

(Weak) Counterargument

But what if we in future we need another field from this broad object? You'll have to modify the signature.

Oh how terrible, I wouldn't want to saddle someone else with that, no one else is smart as me (ok changing signatures requires some care, but there are better solutions than just passing the universe into a simple method).

This tweet is the answer to "prefactoring" / programming "for the future" (and it applies to method-level design):

The wrong abstraction is far more damaging than no abstraction at all.
Waiting trumps guessing every time.

Clarifications

Yes I know that a java.lang.String is an object. And often you have to pass java.util.Collection. These are different to "custom objects" because:

  • Strings are immutable
  • Collections can (and should) be immutable copies when passed as parameters
    • if they are not, your code will be harder to understand because your method becomes stateful / has side effects (i.e. it is an impure function)

If you insist on passing an object, at least make it immutable. But if your emp object is instead a TLOYDEmployee, again you are handicapped in your effort to understand the intent of the code and all you have to go on is shared understanding of the meaning of business jargon (the use-reuse paradox).

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