How do you name your interfaces?

I want to create a Stack interface, and then 2 implementations of it: StackImpl and StackConcurrentImpl (the second one is safe for concurrent usage).

Any idea on naming ? Should I create two packages, stack & concurrentStack, and inside each have a Stack interface with an Impl structure that implements it ?

What is the idiomatic way of naming the interfaceand struct that implements it ?

Minor side note : I am personally allergic to abbreviations in names, with the exception of well known abbreviations like JSON, XML, HTML…Most (all) IDE:s today will help you so you don’t have to write the entire word anyway, so I would avoid names like StackImpl and StackConcurrentImpl, and just write out the entire name. I prefer clear and descriptive names, over short names.

(btw, this is one thing I don’t like about Go. The standard library is full of, in my eyes, bad abbreviations, like strconv, regexp and so on)

But hey, if it works for you…:slight_smile:

1 Like

Impl suffixes are definitely a java-ism, and nothing is expect in go.

Just have the stack interface and then implement it’s methods on a regular stack and an concurrent stack.

The official Effective Go document has a section about interface names:

By convention, one-method interfaces are named by the method name plus an -er suffix or similar modification to construct an agent noun: Reader , Writer , Formatter , CloseNotifier etc.

It is generally a good practice to keep the number of methods in an interface as small as possible (the “I” in “SOLID”). One method is good number which will allow you to follow above naming convention.

If you can’t reduce the number of methods in the interface to one, the name of the interface should at least tell the reader what behaviour he can epxect from an object that implements the interface. Stack could be a good name since to the average reader it implies behaviour like Push and Pop.

If a type only implements an interface without this being the main thing it does, it should not have a name that refers to the name of the interface. It would be a bad idea to include Reader into the name of every type that implements the Reader interace. Many types do without having Reader in their names.

If the main thing a type does is to implement an interface in a specific way and this specific way is of interest to the user, incldue this way into the name of the type together with the interace name. In Java the interface Map is implemented by the HashMap and the TreeMap. Hash and Tree are the how, Map is the what.

Never use Impl or even worse SimpleStack, BasicStack or other names that say nothing about how a type implements an interface. If you can’t think about a good name that tells the reader about the how, maybe there is no need for an interface at all.

2 Likes

Great answers everyone. @lutzhorn you make a very good point about the what and the how - I will stick to it.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.