4 ms·
Sometimes you don’t know what the implementation will be yet, and writing a bunch of interfaces out can help you discover what it should be, then you start work
by seanmcdirmid 9y ago
Sometimes you don’t know what the implementation will be yet, and writing a bunch of interfaces out can help you discover what it should be, then you start working it out with interfaces gradually giving way to implementation.
Interfaces are rarely rarely about interoperability even in Java. Most interfaces have one implementation. Instead, they are about abstraction, design, and so on. This article assumes that the end product is what was intended all along.
- xienze 9y ago> Most interfaces have one implementation. Close, with the exception of mocking interfaces for unit testing, which is very common in Java.
- stubish 9y agoThis. So many packages are a pain to test, because only a concrete implementation is provided. If an interface was used, consumers can swap it out for a mock implementation. It is an almost endemic problem, but the article is criticizing the only available solution (apart from forking). I am utterly sick of writing fragile scaffolding to deal with these dependencies, to the point I wish that Go provided an implicit interface for every declared type. Testing would be so much easier if I could pass in a MockFoo instead of a Foo, provided MockFoo satisfied the implicit interface.
- sythe2o0 9y agoThe article, from how I read it, is not suggesting single-implementation interfaces are the problem but instead where those interfaces are stored. The package accepting the interface should define the interface it needs (and perhaps that package defines the mock implementation), and the implementation should exist somewhere else.
- ghthor 9y agoI've programmed in both ways and they each have there place. But in general this is the right way, you shouldn't export interfaces, you should export concrete types and then use the documentation and tests to example what types of interfaces the types can cover.
- seanmcdirmid 9y agoTypeScript, having a purely structural type system, means any class has an interface. Unfortunately, if the class has any private or protected members, its interface becomes unimplementable.
- dahauns 9y agoYeah, tricky one, that. At least you have the workaround type PublicPart<T> = {[K in keyof T]: T[K]}; //keyof only sees public properties
- XR0CSWV3h3kZWg 9y agoI generally start with making something that accomplishes some small part of what I am trying to eventually accomplish, then iteratively add things. It's awful to do in Java, but I felt fine doing it in go.
- catnaroek 9y ago> Most interfaces have one implementation. Instead, they are about abstraction, design, and so on. Then what you want is an abstract data type. Unlike interfaces, which can be implemented anywhere, an abstract data type is implemented in a single place. This has obvious benefits for performance (clients don't need to dynamically dispatch on operations), control (implementors don't need to worry about malicious clients supplying impostors) and precision (a single module can export several abstract types, whose implementations are mutually dependent). Go's interfaces are pretty terrible at all of the above. Not that Java provides anything substantially better.
- kuschku 9y agoLuckily, Kotlin does provide much better ADTs, and Java can be swapped out for Kotlin 1:1. While Go is reinventing the languages of the 80s in the 2010s.
- catnaroek 9y agoAbstract data types have nothing to do with either algebraic data types or abstract classes.
- seanmcdirmid 9y agoI have only seen abstract data types used to describe stacks and queues, and never in the context of a larger design where the underlying implementations weren’t data structures but objects. Do you have any good examples?
- deleted 9y ago[deleted]
- catnaroek 9y agoIf you want data abstraction to do at least a minimum of static invariant enforcement, you pretty much have to use abstract data types. But if you're fine with littering logs with “exception was caught here, such and such data structure was corrupted”, suit yourself.
- chewxy 9y agoAh, that's why you write your abstract implementations in Haskell, and then rewrite them in Go ;P