|
| 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] |
0 commit comments