Dev.to•Jan 29, 2026, 3:43 AM
Go devs ditch bloated userService interfaces: no more mocks with 10 empty methods just to test one damn function

Go devs ditch bloated userService interfaces: no more mocks with 10 empty methods just to test one damn function

Software engineer Ethan was refactoring the User Service interface in his code, making it comprehensive to handle various user-related tasks, but encountered difficulties with unit testing due to the interface's large size. His colleague Eleanor explained that in languages like Go, interfaces can be defined where they are used, not where they are implemented, allowing for more flexibility. She demonstrated how to apply the Interface Segregation Principle by defining a smaller UserGetter interface that only includes the necessary method, making testing easier. This approach, known as consumer-defined interfaces, allows the consumer to define the interface they need, rather than the producer. Eleanor advised returning concrete structs from service packages and letting the consumer define small, precise interfaces. This technique, "Accept Interfaces, Return Structs," enables more efficient testing and code maintenance. By applying this principle, developers can write more efficient and testable code.

Viral Score: 78%

More Roasted Feeds

No news articles yet. Click "Fetch Latest" to get started!