| name | interface-pollution |
| description | Detect and avoid unnecessary interface abstractions |
Interface Pollution Detection
Interface pollution occurs when interfaces are created before they're needed. Define interfaces at consumption point, not production.
Pollution Patterns
WRONG - Producer defines interface prematurely
type UserRepository interface {
GetUser(id int) (*User, error)
SaveUser(u *User) error
}
type PostgresRepo struct{}
func (p *PostgresRepo) GetUser(id int) (*User, error) { ... }
func (p *PostgresRepo) SaveUser(u *User) error { ... }
CORRECT - Consumer defines minimal interface
type UserGetter interface {
GetUser(id int) (*User, error)
}
func HandleRequest(repo UserGetter) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, _ := repo.GetUser(123)
})
}
Detection Checklist
You have interface pollution if:
Refactoring Strategy
Before:
type DataService interface {
Read() error
Write() error
Validate() error
Transform() error
}
After:
type Reader interface { Read() error }
type Writer interface { Write() error }
type ReadWriter interface {
Reader
Writer
}