Skip to content
Merged
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/S8311/groovy/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"title": "Method names should not use reserved keywords",
"type": "CODE_SMELL",
"status": "beta",
"remediation": {
"func": "Constant/Issue",
"constantCost": "5 min"
},
"tags": [
"confusing",
"naming"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-8311",
"sqKey": "S8311",
"scope": "Main",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH"
},
"attribute": "CLEAR"
}
}
57 changes: 57 additions & 0 deletions rules/S8311/groovy/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
This rule raises an issue when a method is defined with a reserved keyword name surrounded by quotes.

== Why is this an issue?

While Groovy allows defining methods with reserved keyword names by surrounding them with quotes, this practice creates several problems:

* *Reduced readability*: Quoted method names look unusual and can confuse developers who are not familiar with this Groovy feature.
* *Special calling syntax*: Methods with quoted keyword names require qualified calls using the `this.` prefix, making the calling code more verbose and inconsistent.
* *Maintenance burden*: Code becomes harder to understand and maintain when method names don't clearly express their purpose.
* *Inconsistent style*: Mixing quoted and unquoted method names creates inconsistent code style within a project.

The Groovy documentation itself states that "using such names might be confusing and is often best to avoid." This feature was primarily intended for specific Java integration scenarios and Domain Specific Language (DSL) use cases, not for general application development.

=== What is the potential impact?

Using reserved keywords as method names can lead to:

* *Confusion for team members*: Developers may not understand why certain methods require special calling syntax
* *Increased cognitive load*: Reading and understanding code becomes more difficult
* *Maintenance issues*: Future developers may struggle to work with such code
* *Inconsistent codebase*: Mixed naming conventions reduce overall code quality

== How to fix it

Replace the quoted reserved keyword with a descriptive method name that clearly indicates the method's purpose.

=== Code examples

==== Noncompliant code example

[source,groovy,diff-id=1,diff-type=noncompliant]
----
def "abstract"() { // Noncompliant
return true
}

this.abstract()
----

==== Compliant solution

[source,groovy,diff-id=1,diff-type=compliant]
----
def isAbstract() {
return true
}

this.isAbstract()
----

== Resources

=== Documentation

* Groovy documentation - https://groovy-lang.org/syntax.html#_keywords[Keywords]

* Groovy documentation - https://groovy-lang.org/syntax.html#_identifiers[Identifiers]
2 changes: 2 additions & 0 deletions rules/S8311/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Loading