Hasty Briefsbeta

Revisiting Interface Segregation in Go

21 days ago
  • #Interface Segregation Principle
  • #SOLID
  • #Go
  • Object-oriented (OO) patterns are often criticized in the Go community, but principles like SOLID can still be useful.
  • The Interface Segregation Principle (ISP) in SOLID suggests that clients should not depend on methods they do not use.
  • In Go, small interfaces, implicit implementation, and consumer-defined contracts make ISP feel natural and improve code maintainability.
  • Example shows how a `FileStorage` type with `Save` and `Load` methods can be refactored to use smaller interfaces (`Saver`).
  • Consumer-side interfaces reduce coupling and make tests easier by allowing minimal fakes.
  • AWS SDK example demonstrates avoiding large interfaces by defining small, consumer-side interfaces like `Uploader`.
  • General workflow: Insert a seam between tightly coupled components using a consumer-side interface exposing only needed methods.