简体   繁体   English

我遇到声纳建议问题,即Method有8个参数,大于我的代码中授权的7个

[英]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.

相关问题 方法有8个参数,大于7个已授权 - Method has 8 parameters, which is greater than 7 authorized 该方法的圈复杂度为 13,大于 10 授权 - The Cyclomatic Complexity of this method is 13 which is greater than 10 authorized 该方法的Cyclomatic Complexity大于授权 - The Cyclomatic Complexity of this method is greater than authorized 我正在尝试使用一种方法来查找数组中的哪个数字大于20并返回一个百分比 - I am trying to use a method in order to find which numbers in the array are greater than 20 and return a percentage 当我在ubuntu的摇摆代码中使用DJ浏览器时遇到libwebkitgtk的问题 - When i am using DJ browser in my swing code in ubuntu facing issue with libwebkitgtk 运行我的应用程序时,我在 Flutter 中遇到问题 - I am facing issue in Flutter while run my application 问题解决此方法的循环复杂度大于授权 - Problem solving Cyclomatic Complexity of this method is greater than authorized 运行我的Java代码时,我遇到此错误-> - when running my java code i am facing this error -> 我在将请求参数从一个 servlet 转发到另一个时遇到问题 - I am facing issue in forwarding request parameters from one servlet to another 尽管传递了预期的参数,为什么我仍然面临 junit 5 assertThrows 断言 TimedOutException 的问题? - Why am I facing issue with junit 5 assertThrows for asserting TimedOutException despite passing expected parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM