I disagree.
Trying to distinguish a 10-method interface from a
12-method struct implementing that interface is
probably more pain than it's worth.
Russ
An interface should describe what it does. For example, Reader, Writer.
Similarly, a type should describe what it is. For example,
json.Encoder, http.Request. A type may implement more than one
interface, so naming your type after the interfaces it implements will
lead to painfully verbose naming.
To summarize: when naming a type, use the simplest name that can be
understood in the context of the package.
(ie, json.Encode, not json.JsonEncoder)
Andrew
In other languages, sure. But not in Go.
Look around the source tree: most functions only need
one or two methods from the object being passed in,
and those use a precise interface. Part of the reason
it's okay to be so precise is that there is no tight
coupling (like in Java) between the implementations
and the interfaces: the implementations don't have to
list every interface they're implementing. We talked
about this quite a bit in the Google I/O talk:
http://www.youtube.com/watch?v=jgVhBThJdXc
> The way I would put it is, if you are writing a toss-off or one-off
> program and you cannot imagine anyone will want to extend it, then
> maybe just write all your code to be exactly as specific as you need
> it right now. But are you sure about your supposition? Remember that
> most expense comes in maintaining and extending/modifying code
> long after it was originally written.
Obviously, we like interfaces. But it's easy to change,
even retrofit, existing code to accept interfaces later,
with minimal changes in the callers (see video),
so agonizing over this decision right now may not be
the best use of time.
It's also very hard (easy?) to argue over generalities. I tried to
be concrete in my reply (10-method interface vs 12-method struct)
but it would help focus the discussion if you could be more
specific about the case you have in mind. It seems unlikely
you are actually coding a HotWaterHeater.
In my own programs I've found that the interface and
concrete types often end up in different packages, which
can provide another answer to your question. For example,
the Cipher interface in the crypto/block package is
implemented by the Cipher type in the crypto/aes and
crypto/blowfish subdirectories. I haven't felt a need to
mangle either name to try to distinguish them. There are already
good tools for learning about types (godoc crypto/block Cipher;
godoc crypto/aes Cipher), I'm sure integration with
IDEs will improve over time, and the qualified names give
useful hints as to the generality of the type:
block.Cipher vs aes.Cipher.
Russ