直接返回对象数组是否是不良实践?

huangapple go评论104阅读模式
英文:

Is it bad practice to return an array of objects directly?

问题

例如,如果我们有一个类,其中的一个方法返回一个对象,我们应该返回该对象的深拷贝。

例如,在返回整数数组之前,我们也应该在返回之前对数组进行深拷贝/克隆。

对于对象数组,是否也是同样的情况呢?例如,我有一个类型为"packages"的对象数组:

// trunk[]是一个实例变量数组,其中包含此类中类型为Package的对象

// 返回数组的方法
public Package[] getTrunk() {
    Package[] temp = new Package[trunk.length];
    
    for(int i = 0; i < trunk.length; i++) {
        temp[i] = trunk[i];
    }
    return temp;
}

在这里使用for循环是否有用,因为对象是引用类型?如果没有,应该如何正确地返回数组?

英文:

For example, if we have a class and one of its method returns an object, we should return a deep copy of the object.

For example, before returning an array of integers, we should also make a deep copy/clone the array before returning it.

Is this also the case for an array of Objects? For example I have an array of objects of type packages:

//trunk[] is an instance variable array that contains objects of type Package in this class

    //method to return array
    public Package[] getTrunk() {
        Package[] temp = new Package[trunk.length];
        
        for(int i = 0; i &lt; trunk.length; i++) {
            temp[i] = trunk[i];
        }
        return temp;
    }

Is there any use of using the for loop here since objects are reference types? If not, how should I return the array correctly?

答案1

得分: 2

如果返回主干数组,调用者可以修改主干数组。如果返回主干数组的副本,调用者将无法修改主干数组。

您是否关心调用者是否可以修改主干数组?

部分地,这取决于调用者是谁。如果是我的代码调用我的代码,并且我知道我不会修改数组,只是查看它甚至不保留副本,那么返回实际数组可能是可以的。并非每个接口都需要做到百分之百安全;实现百分之百安全是有代价的。

(我看到这是一个“公共”接口,但这可能只是多包预模块化产品结构)

在我看来,您不应该担心某些神奇的“最佳实践”,而应该担心您希望软件如何行为。

英文:

If you return the trunk array, the caller can modify the trunk array. If you return a copy of the trunk array, the caller cannot modify the trunk array.

Do you care whether the caller can modify the trunk array?

The answer to that in part depends on who the caller is. If it's my code calling my code, and I know I'm not modifying the array, just looking at it and not even retaining a copy, then returning the actual array may be fine. Not every interface needs to be idiot-proof; idiot-proofing has a price tag.

(I see this is a 'public' interface but that might just mean a multi-package pre-module product structure)

To my mind, you shouldn't worry about some mythical "best practice", you should worry about how you want the software to behave.

答案2

得分: 0

原始数据类型的数组和复杂类型(某种对象)的数组在这个问题上没有区别。因此通常情况下,你可以说,是的,你应该只返回一个副本。

但也有一些例外情况,其中返回副本可能是有意的,因为:

  • 你实际上希望调用者能够操作后备数组

  • 复制需要时间。一些核心 Java 类不返回副本以避免开销(例如ByteBuffer#array())。

英文:

There is no difference between an array of primitives and an array of complex types (Objects of some kind) regarding this question. So in general you can say, yes, you should only return a copy.

But there are some exceptions where not returning a copy may be the intended, because:

  • You actually want to caller to be able to manipulate the backed array

  • Copying takes time. Some core Java classes don't return a copy to avoid the overhead (e.g. ByteBuffer#array())

huangapple
  • 本文由 发表于 2020年7月24日 08:03:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63064873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定