Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions rules/S8313/groovy/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"title": "Compound assignment operators should be used for counter increments",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant/Issue",
"constantCost": "2 min"
},
"tags": [
"convention",
"readability"
],
"defaultSeverity": "Critical",
"ruleSpecification": "RSPEC-8313",
"sqKey": "S8313",
"scope": "All",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH"
},
"attribute": "CONVENTIONAL"
}
}
51 changes: 51 additions & 0 deletions rules/S8313/groovy/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
This rule raises an issue when a variable is assigned the result of adding a value to itself, instead of using the compound assignment operator.

== Why is this an issue?

Using explicit assignment with addition (like `counter = counter + 1`) instead of compound assignment operators (like `counter += 1`) makes code more verbose and harder to read.

Compound assignment operators are a standard feature in most programming languages, including Groovy. They provide a concise way to perform an operation on a variable and assign the result back to the same variable.

The verbose form requires developers to:

* Read the variable name twice
* Mentally verify that both instances refer to the same variable
* Process more text to understand a simple increment operation

This pattern becomes particularly problematic when:

* The variable name is long, making the line harder to scan
* Multiple similar operations appear in the same code block
* The code is reviewed or maintained by different developers

Using compound assignment operators follows established coding conventions and makes the intent clearer - you're modifying the existing value rather than calculating a new one.

=== What is the potential impact?

This issue affects code readability and maintainability. While it doesn't introduce bugs or security vulnerabilities, it makes code unnecessarily verbose and harder to understand, especially for developers who need to quickly scan through the code.

== How to fix it

Replace the explicit assignment with addition using the compound assignment operator `+=`. This makes the code more concise and clearly expresses the intent to increment the variable.

=== Code examples

==== Noncompliant code example

[source,groovy,diff-id=1,diff-type=noncompliant]
----
session.counter = session.counter + 1 // Noncompliant
----

==== Compliant solution

[source,groovy,diff-id=1,diff-type=compliant]
----
session.counter += 1
----

== Resources

=== Documentation

* Groovy Operators - https://groovy-lang.org/operators.html[Official Groovy documentation covering all operators including compound assignment operators]
2 changes: 2 additions & 0 deletions rules/S8313/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}