简体   繁体   English

在方法(在Java中)中更改对象参数是不好的做法吗?

[英]Is mutating object-parameters in a method(in Java) a bad practice?

I have a question about mutating method-paramaters(which are objects) in a method. 我有一个关于在方法中改变方法参数(是对象)的问题。

I read and heard multiple times that it is a bad practice to mutate a object in a method which was passed in as a paramater. 我多次听到并听到,以参数形式传入的方法对对象进行变异是一种不好的做法。 As example: 例如:

public void modifyList(List<Object> list) {
    list.add(new Object());
}

Instead, the passed in Object should be copied, the mutation should be performed on the copied object and the copied object should be returned. 相反,应该复制传入的Object,应该对复制的对象执行变异,并应返回复制的对象。 As example: 例如:

public List<Object> getModifiedList(List<Object> list) {
    List copy = new List<Object();
    //Make a deep copy if it would be necessary
    for(Object o : list) {
        copy.add(o.clone());
    }
    //Modify the copy
    copy.add(new Object());
    //return the copy
    return copy;
}

I understand that the second method has less potential for side effects because it doesn't mutate the input parameter. 我知道第二种方法产生副作用的可能性较小,因为它不会改变输入参数。

But is this really the way to go? 但这真的是要走的路吗? The performance would suffer because a lot of deep copys have to be created. 由于必须创建许多深层副本,因此性能会受到影响。 Also it would cost a lot of time implement Copy-Constructors and implement clone-methods for all classes. 同样,为所有类实现Copy-Constructors和实现克隆方法将花费大量时间。 Also it would increase the LOC immense. 同样,它将增加LOC的数量。

In practice I don't see this pattern(copy the method-parameter) often. 在实践中,我通常不会看到这种模式(复制方法参数)。

Could somebody with a lot of experience(working as a programmer/software developer for years) answer this? 有经验的人(作为程序员/软件开发人员多年)可以回答这个问题吗?

Greetings Maverin 问候小牛

Both methods are fine and could be the correct choice depending on your use case. 两种方法都很好,根据您的用例,可能是正确的选择。 Just make sure that you name them in a way that makes the intent clear and write some javadoc. 只需确保以明确意图的方式命名它们并编写一些javadoc。

It's then up to the developer to decide whether having the original list mutated is ok or not, and if not, pass a copy or use a different method. 然后由开发人员决定是否可以对原始列表进行更改,如果不行,则传递副本或使用其他方法。

For example, this method from the JDK mutates an existing list, but its intent and documentation are very clear. 例如,来自JDK的此方法会更改现有列表,但是其意图和文档非常清楚。

It's up to your use-case, as you have already implied. 正如您已经暗示的,这取决于您的用例。 Making your components immutable brings many advantages such as better encapsulation , thread safety , avoiding having invalid state etc.. Of course, in this way you implement a performance hit. 使您的组件不可变带来许多优点,例如更好的封装线程安全性避免出现无效状态等。当然,通过这种方式,您可以实现性能提升。 But someone with great experience wrote a chapter about this, which I can only recommend: 但是经验丰富的人为此写了一章,我只能推荐:

Effective Java - 有效的Java-

Item 50: Make defensive copies when needed. 项目50:在需要时制作防御性副本。

There he recommends: 他在那里推荐:

You must program defensively, with the assumption that clients of your class will do their best to destroy its invariants. 您必须进行防御性编程,并假设班级的客户将尽最大努力销毁其不变式。

and also: 并且:

In summary, if a class has mutable components that it gets from or returns to its clients, the class must defensively copy these components. 总之,如果类具有从其客户端获取或返回给其客户端的可变组件,则该类必须防御性地复制这些组件。 If the cost of copy would be prohibitive and the class trusts its clients not to modify the components inappropriately, then the defensive copy may be replaced by documentation outlining the client's responsibility not to modify the affected components. 如果复制成本过高,并且该类信任其客户不要不适当地修改组件,则可以用概述客户不修改受影响组件职责的文档来代替防御性副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中以void方法修改输入对象是否是错误的做法? - Is it bad practice in Java to modify input object in void method? Java抽象方法:这是不好的做法吗? - Java abstract method: is this bad practice? 将异常作为参数传递给 java 中的方法是不好的做法吗 - Is it bad practice to pass exception as argument to a method in java 在Clojure的循环中使用副作用方法来变换Java对象 - Mutating Java object by using side-effecting method in a loop in Clojure Java对象和方法参数 - Java object and method parameters 在 Java 中将方法作为参数传递给另一个方法是不好的做法吗? - In Java Is it bad practice to pass a method as an argument in another method? spring http请求映射单个uri用不同的请求参数到不同的方法是好的做法还是不好的做法 - spring http request mapping single uri with different request parameters to different method is good practice or bad practice 是否通过改变其参考错误做法来更新 Map 的值? - Is updating the value of a Map by mutating its reference bad practice? 在Java中将子对象用作父对象属性是不好的做法吗? - Is it bad practice to use child object as a parent object attribute in Java? 在java中使用getter方法将元素添加到List是一种不好的做法吗? - Is it a bad practice to add elements to List using getter method in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM