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/S8209/go/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"title": "Consecutive function parameters with the same type should be grouped",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant/Issue",
"constantCost": "5 min"
},
"tags": [
"idiom",
"style"
],
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-8209",
"sqKey": "S8209",
"scope": "All",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW"
},
"attribute": "CONVENTIONAL"
}
}
49 changes: 49 additions & 0 deletions rules/S8209/go/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
This is an issue when two or more consecutive function parameters have the same type but are declared with separate type annotations.

== Why is this an issue?

Go provides a convenient syntax feature that allows you to group consecutive parameters of the same type together. Instead of repeating the type for each parameter, you can omit the type from all but the last parameter in the group.

This feature serves multiple purposes:

* *Reduces visual clutter*: Eliminating redundant type declarations makes function signatures cleaner and easier to read.
* *Follows Go idioms*: The Go community and official documentation consistently use this syntax pattern, making it the expected style.
* *Improves maintainability*: When you need to change the type of grouped parameters, you only need to update it in one place.

The Go language designers included this feature specifically to make code more concise while maintaining clarity. Using the grouped syntax demonstrates familiarity with Go conventions and produces code that feels natural to other Go developers.

=== What is the potential impact?

This issue affects code readability and consistency. While it doesn't impact functionality, using non-idiomatic Go syntax can make code appear less professional and may slow down code reviews as developers notice the style inconsistency.

== How to fix it

Group consecutive parameters of the same type by removing the type annotation from all but the last parameter in the group.

=== Code examples

==== Noncompliant code example

[source,go,diff-id=1,diff-type=noncompliant]
----
func add(x int, y int) int { // Noncompliant
return x + y
}
----

==== Compliant solution

[source,go,diff-id=1,diff-type=compliant]
----
func add(x, y int) int {
return x + y
}
----

== Resources

=== Documentation

* A Tour of Go - Functions continued - https://go.dev/tour/basics/5[Official Go tutorial explaining how to group consecutive parameters of the same type]

* Go's Declaration Syntax - https://go.dev/blog/gos-declaration-syntax[Blog post explaining the reasoning behind Go's declaration syntax design]
2 changes: 2 additions & 0 deletions rules/S8209/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}