简体   繁体   English

声纳问题-阵列直接存储

[英]Sonar Issue - Array is directly stored

I have a method wherein i'm directly assigning the method argument to a class variable. 我有一种方法,其中我直接将方法参数分配给类变量。

public void sample(String[] targets)
{
    this.objects = Targets;
}

One solution i found out is creatig a new copy of the array using Arrays.copy(targets). 我发现的一个解决方案是使用Arrays.copy(targets)创建数组的新副本。 But i'm worried if this would effect the performance of the system. 但是我担心这是否会影响系统的性能。

I have two questions : 我有两个问题:

Firstly, how big the Impact is if i use Arrays.copy(). 首先,如果我使用Arrays.copy(),影响会是多大。

If the Impact is big(if the array length is high), then is there any other solution to fix this sonar issue without impacting the Performance. 如果影响很大(如果阵列长度很大),那么有没有其他解决方案可以解决此声纳问题而不影响性能。

The Sonar hint advises you to make a defensive copy of the given array. 声纳提示建议您对给定阵列进行防御性复制 Generally speaking, you should follow that advise, because otherwise, your code could produce bugs, which are caused by unwanted side-effects and therefor hard to localize. 一般来说,您应该遵循该建议,因为否则,您的代码可能会产生错误,这些错误是由不希望有的副作用引起的 ,因此很难本地化。

Usually, the performance impact by defensive copying is neglectible, due to it's unnoticeable overhead. 通常,防御性复制对性能的影响可忽略不计,因为它的开销很小。 More important for the decision pro/against defensive copy is its influence on other aspects of code quality, such as readibility, usability and stability. 对于赞成/反对防御性复制而言,更重要的是它对代码质量的其他方面的影响,例如可读性,可用性和稳定性。

As long as sample() is called infrequently, you don't need to hesitate making array copies. 只要不经常调用sample() ,就无需犹豫制作数组副本。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM