简体   繁体   English

如何在Java中为声纳设置自定义复杂度限制?

[英]How to set a custom complexity limits for sonar in java?

Is there a way to set complexity limits for sonar? 有没有办法设置声纳的复杂度极限? I prefer very simple codes (at most one if or loop), but I cannot find a setting like sonar.cpd.java.minimumtokens or sonar.cpd.java.minimumLines exist for code duplications (and does not seem to work, but that will be another question). 我喜欢非常简单的代码(最多一个if或loop),但是我找不到像sonar.cpd.java.minimumtokenssonar.cpd.java.minimumLines这样的设置来进行代码重复(并且似乎不起作用,但是那将是另一个问题)。

I would like to reduce the cyclomatic complexity limit for methods, and also the maximum number of methods in one class. 我想减少方法的圈复杂度限制,并减少一类中方法的最大数量。 For the latter, a line limit would be okay. 对于后者,行数限制是可以的。 How can I set those complexity limits? 如何设置这些复杂度限制?

There is a few Java rules targeting cyclomatic complexity provided with SonarQube. SonarQube提供了一些针对循环复杂性的Java规则。 The only way to configure them is to use a dedicated Quality Profile. 配置它们的唯一方法是使用专用的质量配置文件。 By default, the SonarWay quality profile uses fixed thresholds. 默认情况下, SonarWay质量配置文件使用固定阈值。 However, these rules are parameterizable, and you can configure them as soon as you create a quality profile other than the default SonarQube's SonarWay profile. 但是,这些规则是可参数化的,您可以在创建质量配置文件(默认SonarQube的SonarWay配置文件除外)后立即对其进行配置。 You will then have to configure your project to rely on this new Quality Profile, rather than SonarWay . 然后,您将必须配置项目以依赖于此新的Quality Profile,而不是SonarWay

For reference, have a look at the following rules: 作为参考,请查看以下规则:

I would also recommend to have a look at another complexity-related rule: 我还建议您看一下另一个与复杂性有关的规则:

  • squid:S3776 : Cognitive Complexity of methods should not be too high squid:S3776 :方法的认知复杂度不应太高

This rule is relying on the concept of Cognitive Complexity , designed in order to remedy Cyclomatic Complexity's shortcomings and produce a measurement that more accurately reflects the relative difficulty of understanding, and therefore of maintaining methods, classes, and applications. 该规则依赖于认知复杂性的概念,该概念旨在纠正环复杂性的缺点并产生一种度量,以更准确地反映理解的相对难度,从而维护方法,类和应用程序的相对难度。

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

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