From c6e12bbf8d49a2e2da9a06b428dd3ae6f6ec3d6a Mon Sep 17 00:00:00 2001 From: Antonio Aversa Date: Fri, 16 Jun 2023 15:42:52 +0200 Subject: [PATCH] Modify rule S3776: LaYC format (#2215) --- rules/S3776/abap/rule.adoc | 4 +- rules/S3776/apex/rule.adoc | 17 +----- rules/S3776/cfamily/rule.adoc | 4 +- rules/S3776/csharp/metadata.json | 3 +- rules/S3776/csharp/rule.adoc | 73 +++++++++++++----------- rules/S3776/go/rule.adoc | 17 +----- rules/S3776/java/rule.adoc | 18 +----- rules/S3776/javascript/rule.adoc | 17 +----- rules/S3776/kotlin/rule.adoc | 17 +----- rules/S3776/php/rule.adoc | 17 +----- rules/S3776/python/rule.adoc | 17 +----- rules/S3776/{see.adoc => resources.adoc} | 5 +- rules/S3776/rspecator-dotnet.adoc | 36 ++++++++++++ rules/S3776/rspecator.adoc | 13 +++++ rules/S3776/ruby/rule.adoc | 17 +----- rules/S3776/rule.adoc | 6 -- rules/S3776/scala/rule.adoc | 17 +----- rules/S3776/swift/rule.adoc | 17 +----- rules/S3776/vbnet/metadata.json | 7 ++- rules/S3776/vbnet/rule.adoc | 42 +++++++++++--- rules/S3776/why.adoc | 3 + rules/S4487/java/rule.adoc | 1 - 22 files changed, 173 insertions(+), 195 deletions(-) rename rules/S3776/{see.adoc => resources.adoc} (67%) create mode 100644 rules/S3776/rspecator-dotnet.adoc create mode 100644 rules/S3776/rspecator.adoc delete mode 100644 rules/S3776/rule.adoc create mode 100644 rules/S3776/why.adoc diff --git a/rules/S3776/abap/rule.adoc b/rules/S3776/abap/rule.adoc index 7da90f0a86..15cf614bd4 100644 --- a/rules/S3776/abap/rule.adoc +++ b/rules/S3776/abap/rule.adoc @@ -1,4 +1,6 @@ -include::../rule.adoc[] +include::../why.adoc[] +include::../resources.adoc[] + ifdef::env-github,rspecator-view[] ''' diff --git a/rules/S3776/apex/rule.adoc b/rules/S3776/apex/rule.adoc index 2ca5043b15..b0dc260a3c 100644 --- a/rules/S3776/apex/rule.adoc +++ b/rules/S3776/apex/rule.adoc @@ -1,14 +1,3 @@ -include::../rule.adoc[] -ifdef::env-github,rspecator-view[] - -''' -== Implementation Specification -(visible only on this page) - -include::../message.adoc[] - -include::../parameters.adoc[] - -include::../highlighting.adoc[] - -endif::env-github,rspecator-view[] +include::../why.adoc[] +include::../resources.adoc[] +include::../rspecator.adoc[] diff --git a/rules/S3776/cfamily/rule.adoc b/rules/S3776/cfamily/rule.adoc index 55bd39ce4a..cb12ad907c 100644 --- a/rules/S3776/cfamily/rule.adoc +++ b/rules/S3776/cfamily/rule.adoc @@ -1,4 +1,6 @@ -include::../rule.adoc[] +include::../why.adoc[] +include::../resources.adoc[] + ifdef::env-github,rspecator-view[] ''' diff --git a/rules/S3776/csharp/metadata.json b/rules/S3776/csharp/metadata.json index aaa7a4e528..eed8df63b2 100644 --- a/rules/S3776/csharp/metadata.json +++ b/rules/S3776/csharp/metadata.json @@ -3,5 +3,6 @@ "remediation": { "func": "Constant\/Issue", "constantCost": "10min" - } + }, + "quickfix": "infeasible" } diff --git a/rules/S3776/csharp/rule.adoc b/rules/S3776/csharp/rule.adoc index f13dbc3321..3a901a7466 100644 --- a/rules/S3776/csharp/rule.adoc +++ b/rules/S3776/csharp/rule.adoc @@ -1,44 +1,51 @@ == Why is this an issue? -Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain. +https://www.sonarsource.com/docs/CognitiveComplexity.pdf[Cognitive Complexity] Complexity is a measure of how hard the control flow of a method is to understand. -include::../see.adoc[] -ifdef::env-github,rspecator-view[] - -''' -== Implementation Specification -(visible only on this page) - -=== Message - -Refactor this {0} to reduce its Cognitive Complexity from {1} to the {2} allowed. - - -Where {0} is the name of the syntax being analyzed, {1} the cognitive complexity and {2} the maximum authorized complexity. - - -=== Parameters - -.threshold -**** +Methods with high Cognitive Complexity will be difficult to maintain. +[source,csharp,diff-id=1,diff-type=noncompliant] ---- -15 +int Abs(int n) // Noncompliant: cognitive complexity = 5 +{ + if (n >= 0) // +1 + { + return n; + } + else // +2, due to nesting + { + if (n == int.MinValue) // +1 + { + throw new ArgumentException("The absolute value of int.MinValue is outside of int boundaries"); + } + else // +1 + { + return -n; + } + } +} ---- -The maximum authorized complexity. -**** -.propertyThreshold -**** +They should be refactored to have lower complexity: +[source,csharp,diff-id=1,diff-type=compliant] ---- -3 +int Abs(int n) // Compliant: cognitive complexity = 3 +{ + if (n == int.MinValue) // +1 + { + throw new ArgumentException("The absolute value of int.MinValue is outside of int boundaries"); + } + else if (n >= 0) // +1 + { + return n; + } + else // +1 + { + return -n; + } +} ---- -The maximum authorized complexity in a property. -**** - - -include::../highlighting.adoc[] - -endif::env-github,rspecator-view[] +include::../resources.adoc[] +include::../rspecator-dotnet.adoc[] \ No newline at end of file diff --git a/rules/S3776/go/rule.adoc b/rules/S3776/go/rule.adoc index 2ca5043b15..f4b5868e9d 100644 --- a/rules/S3776/go/rule.adoc +++ b/rules/S3776/go/rule.adoc @@ -1,14 +1,3 @@ -include::../rule.adoc[] -ifdef::env-github,rspecator-view[] - -''' -== Implementation Specification -(visible only on this page) - -include::../message.adoc[] - -include::../parameters.adoc[] - -include::../highlighting.adoc[] - -endif::env-github,rspecator-view[] +include::../why.adoc[] +include::../resources.adoc[] +include::../rspecator.adoc[] \ No newline at end of file diff --git a/rules/S3776/java/rule.adoc b/rules/S3776/java/rule.adoc index 00d8e4ffa2..ee76d9f4e1 100644 --- a/rules/S3776/java/rule.adoc +++ b/rules/S3776/java/rule.adoc @@ -4,19 +4,7 @@ Cognitive Complexity is a measure of how hard the control flow of a method is to === Exceptions -``++equals++`` and ``++hashCode++`` methods are ignored because they might be automatically generated and might end up being difficult to understand, especially in presence of many fields. +`equals` and `hashCode` methods are ignored because they might be automatically generated and might end up being difficult to understand, especially in the presence of many fields. -include::../see.adoc[] -ifdef::env-github,rspecator-view[] - -''' -== Implementation Specification -(visible only on this page) - -include::../message.adoc[] - -include::../parameters.adoc[] - -include::../highlighting.adoc[] - -endif::env-github,rspecator-view[] +include::../resources.adoc[] +include::../rspecator.adoc[] diff --git a/rules/S3776/javascript/rule.adoc b/rules/S3776/javascript/rule.adoc index 2ca5043b15..f4b5868e9d 100644 --- a/rules/S3776/javascript/rule.adoc +++ b/rules/S3776/javascript/rule.adoc @@ -1,14 +1,3 @@ -include::../rule.adoc[] -ifdef::env-github,rspecator-view[] - -''' -== Implementation Specification -(visible only on this page) - -include::../message.adoc[] - -include::../parameters.adoc[] - -include::../highlighting.adoc[] - -endif::env-github,rspecator-view[] +include::../why.adoc[] +include::../resources.adoc[] +include::../rspecator.adoc[] \ No newline at end of file diff --git a/rules/S3776/kotlin/rule.adoc b/rules/S3776/kotlin/rule.adoc index 2ca5043b15..f4b5868e9d 100644 --- a/rules/S3776/kotlin/rule.adoc +++ b/rules/S3776/kotlin/rule.adoc @@ -1,14 +1,3 @@ -include::../rule.adoc[] -ifdef::env-github,rspecator-view[] - -''' -== Implementation Specification -(visible only on this page) - -include::../message.adoc[] - -include::../parameters.adoc[] - -include::../highlighting.adoc[] - -endif::env-github,rspecator-view[] +include::../why.adoc[] +include::../resources.adoc[] +include::../rspecator.adoc[] \ No newline at end of file diff --git a/rules/S3776/php/rule.adoc b/rules/S3776/php/rule.adoc index 2ca5043b15..f4b5868e9d 100644 --- a/rules/S3776/php/rule.adoc +++ b/rules/S3776/php/rule.adoc @@ -1,14 +1,3 @@ -include::../rule.adoc[] -ifdef::env-github,rspecator-view[] - -''' -== Implementation Specification -(visible only on this page) - -include::../message.adoc[] - -include::../parameters.adoc[] - -include::../highlighting.adoc[] - -endif::env-github,rspecator-view[] +include::../why.adoc[] +include::../resources.adoc[] +include::../rspecator.adoc[] \ No newline at end of file diff --git a/rules/S3776/python/rule.adoc b/rules/S3776/python/rule.adoc index 2ca5043b15..f4b5868e9d 100644 --- a/rules/S3776/python/rule.adoc +++ b/rules/S3776/python/rule.adoc @@ -1,14 +1,3 @@ -include::../rule.adoc[] -ifdef::env-github,rspecator-view[] - -''' -== Implementation Specification -(visible only on this page) - -include::../message.adoc[] - -include::../parameters.adoc[] - -include::../highlighting.adoc[] - -endif::env-github,rspecator-view[] +include::../why.adoc[] +include::../resources.adoc[] +include::../rspecator.adoc[] \ No newline at end of file diff --git a/rules/S3776/see.adoc b/rules/S3776/resources.adoc similarity index 67% rename from rules/S3776/see.adoc rename to rules/S3776/resources.adoc index 269a888d65..30919a58d8 100644 --- a/rules/S3776/see.adoc +++ b/rules/S3776/resources.adoc @@ -1,3 +1,6 @@ == Resources -* https://www.sonarsource.com/docs/CognitiveComplexity.pdf[Cognitive Complexity] \ No newline at end of file +=== Documentation + +* https://www.sonarsource.com/docs/CognitiveComplexity.pdf[Cognitive Complexity] + diff --git a/rules/S3776/rspecator-dotnet.adoc b/rules/S3776/rspecator-dotnet.adoc new file mode 100644 index 0000000000..3c5ad35b10 --- /dev/null +++ b/rules/S3776/rspecator-dotnet.adoc @@ -0,0 +1,36 @@ +ifdef::env-github,rspecator-view[] + +''' +== Implementation Specification +(visible only on this page) + +=== Message + +Refactor this {0} to reduce its Cognitive Complexity from {1} to the {2} allowed. + +Where {0} is the name of the syntax being analyzed, {1} is the cognitive complexity and {2} is the maximum authorized complexity. + +=== Parameters + +.threshold +**** + +---- +15 +---- + +The maximum authorized complexity. +**** +.propertyThreshold +**** + +---- +3 +---- + +The maximum authorized complexity in a property. +**** + +include::highlighting.adoc[] + +endif::env-github,rspecator-view[] diff --git a/rules/S3776/rspecator.adoc b/rules/S3776/rspecator.adoc new file mode 100644 index 0000000000..19e59f5673 --- /dev/null +++ b/rules/S3776/rspecator.adoc @@ -0,0 +1,13 @@ +ifdef::env-github,rspecator-view[] + +''' +== Implementation Specification +(visible only on this page) + +include::message.adoc[] + +include::parameters.adoc[] + +include::highlighting.adoc[] + +endif::env-github,rspecator-view[] \ No newline at end of file diff --git a/rules/S3776/ruby/rule.adoc b/rules/S3776/ruby/rule.adoc index 2ca5043b15..f4b5868e9d 100644 --- a/rules/S3776/ruby/rule.adoc +++ b/rules/S3776/ruby/rule.adoc @@ -1,14 +1,3 @@ -include::../rule.adoc[] -ifdef::env-github,rspecator-view[] - -''' -== Implementation Specification -(visible only on this page) - -include::../message.adoc[] - -include::../parameters.adoc[] - -include::../highlighting.adoc[] - -endif::env-github,rspecator-view[] +include::../why.adoc[] +include::../resources.adoc[] +include::../rspecator.adoc[] \ No newline at end of file diff --git a/rules/S3776/rule.adoc b/rules/S3776/rule.adoc deleted file mode 100644 index 77ca9b6d7a..0000000000 --- a/rules/S3776/rule.adoc +++ /dev/null @@ -1,6 +0,0 @@ -== Why is this an issue? - -Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain. - - -include::see.adoc[] diff --git a/rules/S3776/scala/rule.adoc b/rules/S3776/scala/rule.adoc index 2ca5043b15..f4b5868e9d 100644 --- a/rules/S3776/scala/rule.adoc +++ b/rules/S3776/scala/rule.adoc @@ -1,14 +1,3 @@ -include::../rule.adoc[] -ifdef::env-github,rspecator-view[] - -''' -== Implementation Specification -(visible only on this page) - -include::../message.adoc[] - -include::../parameters.adoc[] - -include::../highlighting.adoc[] - -endif::env-github,rspecator-view[] +include::../why.adoc[] +include::../resources.adoc[] +include::../rspecator.adoc[] \ No newline at end of file diff --git a/rules/S3776/swift/rule.adoc b/rules/S3776/swift/rule.adoc index 2ca5043b15..f4b5868e9d 100644 --- a/rules/S3776/swift/rule.adoc +++ b/rules/S3776/swift/rule.adoc @@ -1,14 +1,3 @@ -include::../rule.adoc[] -ifdef::env-github,rspecator-view[] - -''' -== Implementation Specification -(visible only on this page) - -include::../message.adoc[] - -include::../parameters.adoc[] - -include::../highlighting.adoc[] - -endif::env-github,rspecator-view[] +include::../why.adoc[] +include::../resources.adoc[] +include::../rspecator.adoc[] \ No newline at end of file diff --git a/rules/S3776/vbnet/metadata.json b/rules/S3776/vbnet/metadata.json index 1797133380..9541c1fdef 100644 --- a/rules/S3776/vbnet/metadata.json +++ b/rules/S3776/vbnet/metadata.json @@ -1,3 +1,8 @@ { - + "title": "Cognitive Complexity of methods should not be too high", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "quickfix": "infeasible" } diff --git a/rules/S3776/vbnet/rule.adoc b/rules/S3776/vbnet/rule.adoc index 2ca5043b15..75f8911cd7 100644 --- a/rules/S3776/vbnet/rule.adoc +++ b/rules/S3776/vbnet/rule.adoc @@ -1,14 +1,38 @@ -include::../rule.adoc[] -ifdef::env-github,rspecator-view[] +== Why is this an issue? -''' -== Implementation Specification -(visible only on this page) +https://www.sonarsource.com/docs/CognitiveComplexity.pdf[Cognitive Complexity] Complexity is a measure of how hard the control flow of a method is to understand. -include::../message.adoc[] +Methods with high Cognitive Complexity will be difficult to maintain. -include::../parameters.adoc[] +[source,vbnet,diff-id=1,diff-type=noncompliant] +---- +Function Abs(ByVal n As Integer) As Integer ' Noncompliant: cognitive complexity = 5 + If n >= 0 Then ' +1 + Return n + Else ' +2, due to nesting + If n = Integer.MinValue Then ' +1 + Throw New ArgumentException("The absolute value of int.MinValue is outside of int boundaries") + Else ' +1 + Return -n + End If + End If +End Function +---- -include::../highlighting.adoc[] +They should be refactored to have lower complexity: -endif::env-github,rspecator-view[] +[source,vbnet,diff-id=1,diff-type=compliant] +---- +Function Abs(ByVal n As Integer) As Integer ' Compliant: cognitive complexity = 3 + If n = Integer.MinValue Then ' +1 + Throw New ArgumentException("The absolute value of int.MinValue is outside of int boundaries") + Else If n >= 0 Then ' +1 + Return n + Else ' +1 + Return -n + End If +End Function +---- + +include::../resources.adoc[] +include::../rspecator-dotnet.adoc[] \ No newline at end of file diff --git a/rules/S3776/why.adoc b/rules/S3776/why.adoc new file mode 100644 index 0000000000..b5a27aea65 --- /dev/null +++ b/rules/S3776/why.adoc @@ -0,0 +1,3 @@ +== Why is this an issue? + +https://www.sonarsource.com/docs/CognitiveComplexity.pdf[Cognitive Complexity] Complexity is a measure of how hard the control flow of a function is to understand. Functions with high Cognitive Complexity will be difficult to maintain. diff --git a/rules/S4487/java/rule.adoc b/rules/S4487/java/rule.adoc index 10ecf157b0..f1087a3a2d 100644 --- a/rules/S4487/java/rule.adoc +++ b/rules/S4487/java/rule.adoc @@ -19,7 +19,6 @@ public class Rectangle { } ---- - {outro} [source,java,diff-id=1,diff-type=compliant]