3 ms·
I am fairly consistent in my criticism of Go. I am also fairly consistent in my insistence that the Dependency Inversion Principle is one of the harder concept
by kasey_junk 10y ago
I am fairly consistent in my criticism of Go. I am also fairly consistent in my insistence that the Dependency Inversion Principle is one of the harder concepts to wrap your head around of the SOLID principles (not least because several other concepts use terms similar).
But golang's structural typing actually allows for some of the easiest demonstrations of the implementation of the DIP of any of the strongly typed languages I've used.
I find Martin's definition of the DIP counterintuitive. A description that makes more sense to me is "interfaces should be defined by the 'users' of the interface, not by the implementers".
With a language like C++ or Java, to achieve this requires either lots of adapter layers or compiler tricks. With golang it is simple.
As a for instance, let's say I'm writing a restful client library for a specific application. It is tempting to to hard code a dependency on the std lib http.Client.Do method. If instead with 3 lines of code, I define my own interface for Do that matches the std library definition. I can use the std library, or any other library that matches it (one that is faster say or implements back off).
That is one of the few areas where the golang type system is pretty cool in comparison to its peers.
- pcwalton 10y agoBut with Go's system you can't provide an implementation of an interface for a type unless you defined that type in your package. You have to use an adapter layer for that. (For example, say I want to make an interface with a function "func Show() string" that prints debug information about a type--I can't implement that function directly on http.Client, only on an e.g. "type MyClient = http.Client" wrapper.) So that makes it harder to achieve loose coupling, because you can't make your own interfaces that unify standard library types and your types unless you make your types take exactly the same methods with exactly the same signatures (problematic if you want your types to support slightly different functionality, etc.) In a system that allows you to implement interfaces for types that you didn't define in your package, there is a little more boilerplate necessary compared to Go, but only a few lines necessary to achieve the exact same effect. In pseudo-Go: type Do interface { func Do(req *Request) (*Response, error) } // boilerplate implement Do for http.Client { func (c *Client) Do(req *Request) (*Response, error) { return c.Do(req) // call stdlib implementation } } // custom implementation of Do implement Do for MockClient { ... } So Go makes a tradeoff with its structural typing: it doesn't require the small amount of boilerplate code to achieve the pattern you describe, but it prevents you from providing new implementations for types you didn't define. Reasonable people can differ as to whether the side of the tradeoff Go makes encourages or discourages loose coupling.
- n0w 10y agoI think you're a little off. The only boilerplate required for your example is the interface definition. After that you can define your own implementations of the "Do" interface as suggested by the parent. // boilerplate type Do interface { func Do(*http.Request) (*http.Response, error) } // Custom Implementation type MockClient struct {} func (mc *MockClient) Do(req *http.Request) (*http.Response, error) { // MockClient type now satisfies the Do interface }
- masklinn 10y agoYou're not taking this issue in account: > you can't make your own interfaces that unify standard library types and your types unless you make your types take exactly the same methods with exactly the same signatures (problematic if you want your types to support slightly different functionality, etc.)
- n0w 10y agoThe original comment mentioned being able to decouple Go code from specific implementations of functionality by relying on self defined interfaces instead of specific functions. pcwalton's example seemed to confuse this point, which I wanted to clarify. As to that point, your interface types will only match standard library ones if you're trying to unify them. In which case you probably want them to have the same signature.
- masklinn 10y ago> The original comment mentioned being able to decouple Go code from specific implementations of functionality by relying on self defined interfaces instead of specific functions. pcwalton's example seemed to confuse this point, which I wanted to clarify. The problem is that you're clarifying the wrong thing, the example is confusing because it trivially proxies a built-in method but the remark is what's important to the reply. Your reply is only confusing things further by considering the flawed example but ignoring the actual point. > As to that point, your interface types will only match standard library ones if you're trying to unify them. No, why would you think that? You may want to "compatibilise" multiple third-party types (that some of them are in the standard library is a complete red herring) as well as first-party ones (a mock or whatever) e.g. abstract over stdlib sockets, zmq sockets and sqlite.
- daigoba66 10y agoTypeScript also does this. It's neat.