[英]I am facing sonar suggestion issue that Method has 8 parameters, which is greater than 7 authorized in my code
I am facing sonar issue in my below code. 我在下面的代码中遇到声纳问题。
Where sonar suggests that Method has 8 parameters, which is greater than 7 authorized. 声纳表明Method具有8个参数,大于7个授权参数。 So how can we solve this issue by putting this parameters in array or map?
那么,如何通过将此参数放入数组或映射中来解决此问题?
private void appendSchemeSpecificPart(StringBuilder sb,
String opaquePart,
String authority,
String userInfo,
String host,
int port,
String path,
String query
)
I am not able to make array and pass it to this method appendSchemeSpecificPart()
. 我无法制作数组并将其传递给此方法
appendSchemeSpecificPart()
。
Can someone help on this? 有人可以帮忙吗?
Don't use an array to pass several objects that have different semantics. 不要使用数组来传递具有不同语义的多个对象。
It is very very error prone. 这非常容易出错。
Instead of, extracting parameters with high cohesion in a class : SchemeSpecificPart
and define it as a parameter such as : 取而代之的是,在类:
SchemeSpecificPart
提取具有高内聚力的参数,并将其定义为参数,例如:
void appendSchemeSpecificPart(StringBuilder sb, SchemeSpecificPart schemeSpecificPart){..}
The StringBuilder
is not a part of the SchemeSpecificPart
concept. StringBuilder
不是SchemeSpecificPart
概念的一部分。 So we will not define it in. 因此,我们将不再对其进行定义。
Now you can invoke your method : 现在您可以调用您的方法:
StringBuilder sb = ...;
SchemeSpecificPart part = ...;
appendSchemeSpecificPart(sb, part);
Not your question but note that modifying the parameters state (here StringBuilder sb
)through invoked methods is a unsafe approach. 这不是您的问题,但请注意,通过调用的方法修改参数状态(此处为
StringBuilder sb
)是不安全的方法。
It gives to the appendSchemeSpecificPart()
method more responsibilities than it needs. 它给
appendSchemeSpecificPart()
方法带来了比其所需更多的责任。 For example, the method could remove existing content for sb
while it should not be able) and it also may make harder to understand what each method that accepts the StringBuilder
does really since all methods can overwrite the things done by the previous. 例如,该方法可能会删除
sb
现有内容,而该方法本不应该这样做),并且由于所有方法都可以覆盖前一个方法完成的工作,因此也可能使得很难理解接受StringBuilder
每个方法的实际作用。
Returning a String appears more clear and robust : 返回一个字符串看起来更加清晰和健壮:
String computeSchemeSpecificPart(SchemeSpecificPart schemeSpecificPart){...}
And use it : 并使用它:
SchemeSpecificPart part = ...;
StringBuilder sb = ...;
sb.append(computeSchemeSpecificPart(part));
The combination of host and port is called a socket address. 主机和端口的组合称为套接字地址。 There is already a class for holding this pair of values:
InetSocketAddress
. 已经有一个用于保存这对值的类:
InetSocketAddress
。
By combining 2 parameters into 1, you will not exceed the threshold. 通过将2个参数组合为1,您将不会超过阈值。 You could also see whether other related parameters could be combined.
您还可以查看是否可以组合其他相关参数。
It almost looks like String host, int port, String path, String query
are in fact just a URL, but hard to say without seeing the implementation. 它几乎看起来像
String host, int port, String path, String query
实际上只是一个URL,但是很难看到它的实现。
It's best to understand why you have that issue. 最好了解为什么会有这个问题。
From sonar, this is classified under brain overload
- the goal is to make the code easier to read and understand. 从声纳来看,这归类为
brain overload
-目标是使代码更易于阅读和理解。
A long parameter list can indicate that a new structure should be created to wrap the numerous parameters or that the function is doing too many things.
较长的参数列表可能指示应创建一个新结构来包装大量参数,或者该函数执行了太多操作。
Try encapsulating some of those fields in a different class and pass an instance of that class to your method. 尝试将某些字段封装在不同的类中,然后将该类的实例传递给您的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.