Skip to content

Commit 71ba82d

Browse files
github-actions[bot]denis-trolleryassin-kammoun-sonarsourcesebastien-marichal
authored
Create rule S8196: Single-method interface names should follow Go naming conventions (#5757)
* Create rule S8196 * Update rules/S8196/go/rule.adoc in PR #5757 * Update rules/S8196/go/metadata.json in PR #5757 * Update metadata * Update rule.adoc --------- Co-authored-by: denis-troller <[email protected]> Co-authored-by: denis-troller <[email protected]> Co-authored-by: yassin-kammoun-sonarsouce <[email protected]> Co-authored-by: Sébastien Marichal <[email protected]>
1 parent b337410 commit 71ba82d

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

rules/S8196/go/metadata.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"title": "Single-method interface names should follow Go naming conventions",
3+
"type": "CODE_SMELL",
4+
"status": "ready",
5+
"remediation": {
6+
"func": "Constant/Issue",
7+
"constantCost": "5 min"
8+
},
9+
"tags": [
10+
"convention"
11+
],
12+
"defaultSeverity": "Minor",
13+
"ruleSpecification": "RSPEC-8196",
14+
"sqKey": "S8196",
15+
"scope": "All",
16+
"defaultQualityProfiles": [
17+
"Sonar way"
18+
],
19+
"quickfix": "unknown",
20+
"code": {
21+
"impacts": {
22+
"MAINTAINABILITY": "LOW"
23+
},
24+
"attribute": "CONVENTIONAL"
25+
}
26+
}

rules/S8196/go/rule.adoc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
This is an issue when a single-method interface doesn't use the method name plus an "-er" suffix or similar modification to construct an agent noun.
2+
3+
== Why is this an issue?
4+
5+
Go has established naming conventions for interfaces, particularly single-method interfaces. According to the official Go documentation "Effective Go", single-method interfaces should be named by taking the method name and adding an "-er" suffix or similar modification to create an agent noun.
6+
7+
This convention serves several important purposes:
8+
9+
* **Predictability**: Developers can quickly understand what an interface does just by looking at its name
10+
* **Consistency**: Following established patterns makes codebases more uniform and easier to navigate
11+
* **Readability**: Agent nouns like `Reader`, `Writer`, `Formatter` immediately convey the interface's purpose
12+
* **Community standards**: Adhering to Go conventions makes code more familiar to other Go developers
13+
14+
When interfaces don't follow this pattern, they become less intuitive. For example, `FileRead` doesn't immediately suggest it's an interface, while `Reader` clearly indicates both the action and that it's likely an interface following Go conventions.
15+
16+
=== What is the potential impact?
17+
18+
Not following Go naming conventions reduces code readability and makes it harder for developers to understand the codebase quickly. It can also make the code feel inconsistent with standard Go libraries and community practices, potentially confusing team members who expect conventional naming patterns.
19+
20+
== How to fix it
21+
22+
Rename the interface to follow Go conventions by using the method name plus an "-er" suffix or similar agent noun construction.
23+
24+
=== Code examples
25+
26+
==== Noncompliant code example
27+
28+
[source,go,diff-id=1,diff-type=noncompliant]
29+
----
30+
type FileRead interface {
31+
Read([]byte) (int, error)
32+
} // Noncompliant
33+
----
34+
35+
==== Compliant solution
36+
37+
[source,go,diff-id=1,diff-type=compliant]
38+
----
39+
type FileReader interface {
40+
Read([]byte) (int, error)
41+
}
42+
----
43+
44+
== Resources
45+
46+
=== Documentation
47+
48+
* Effective Go - Interface names - https://go.dev/doc/effective_go#interface-names[Official Go documentation explaining interface naming conventions]

rules/S8196/metadata.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

0 commit comments

Comments
 (0)