tags:

views:

217

answers:

11

I know this might seem a controversial question but it really is not meant to be. is there an optimal number of methods in an interface.

For example, I personally hate an interface with 20 methods. It is just difficult to implement. The contract seems to hard to maintain. Similarly if the number of methods is just 1. It makes me wonder if it is really a good abstraction.

Any thoughts ?

+2  A: 

There's a well-known study that suggests that the number of items of information we can process at a time is seven, plus or minus two. I often find that this is a good rule of thumb for this type of question -- in this case, if the interface is designed to manage a single related set of functionality, we probably won't be able to understand the functionality as a single set if it's much more than seven.

However, for many interfaces a developer doesn't need to be able to understand the functionality as a single related set, in which case this rule would not be relevant.

JacobM
A: 

The way to deal with many methods when you only need to implement a few is Abstract classes that supply default implementations or throw NotImplementedException. If there is only one method then it probably is ok because there are methods that only expect that one Iterface and call that one method. There are lots of patterns, Vistor for example that only need one method.

fuzzy lollipop
+3  A: 

Well interfaces with one method usually have their purpose and you can implement them anonymously more easily. My rule of thumb is as many methods as it needs to have. But a lot of methods in one interface usually suggests that you can split it into several other interfaces, especially when it affects different task areas (e.g. a UserLoginAndAdministrationInterface becomes one UserLogin and one UserAdministration interface). You can implement as many interfaces as you want in a class and they can be subclassed, too. So splitting them doesn't hurt at all.

Daff
+12  A: 

An interface should have exactly as many methods as it needs. Examples:

java.lang.Iterable - 1 method
java.lang.Comparable - 1 method
java.util.Collection - 14 methods
java.util.List - 25 methods (including those from Collection)

So the answer is - don't take the number as a criterion for your interface. Instead, put methods in interfaces where they belong logically.

Bozho
java.lang.Runnable - 1 method :)
Andrei Ciobanu
java.io.Serializable - 0 methods :)
pgmura
`Serializable` is quite a different story ;) (`Cloneable` is even another different story)
Bozho
@Bozho: so *List* is the best abstraction ever created by man for a list and that perfect abstraction needs to have *"exactly"* [sic] 25 methods? As much as I like Java, I wouldn't exactly consider the design of the default Java Collections to be the be-all/end-all of OO design (in this case, it *may* very well be possible that the List abstraction should itself be depending on other abstractions, instead of having 25 methods...). I just don't like an example showing Collection and List examples and telling they have *"exactly as many method as they need"*.
Webinator
the java collection framework is designed with a lot of thought and I wouldn't criticize it. And as I said, the 25 methods include those from collection (a higher abstraction).
Bozho
I find the Marker pattern useful on occasion as well, and that is usually implemented as an interface with no methods.
MatthieuF
+3  A: 

An interface should have exactly as many methods as it needs.

If that number is large the rationale should be looked at carefully, but may be valid.

On the other hand there is (at least) one instance in Java where the interface needs no methods (serializable) and thus has none. Fewer methods, definitely makes it easier to implement the interface but can still provide a very useful abstraction. There are a number of interfaces with one method.

Kris
+3  A: 

I'd be more concerned that the interface is logically consistent than it has some arbitrary optimal number of methods. That said, a large number of methods might indicate a "god" object, that should be refactored. The clearer indication is that the methods aren't cohesive, but the number of methods might be easier to observe and lead you to closer examination. Sometimes, though, you just need to have a lot of methods because there are a lot of possible operations you'd like to do on objects of that type. An example of this would be something like IEnumerable<T> in .NET. I can't think of any valid reason for breaking those out into separate interfaces, yet there are a lot of methods in the interface.

tvanfosson
+2  A: 

There is no optimal number of methods in an interface. Interfaces are used for all kinds of different things, and what is appropriate depends entirely on what.

On one extreme, an interface might be automatically code-generated by a program for consumption by another program, and might for good reason have 1,000 methods in it.

For code readable by humans, rules cannot substitute for judgement.

bmargulies
A: 

Those twenty methods either capture something useful together or not. If they do, then you need to implement all of them to provide an implementation to that thing they capture. It will be consistent to house them in the same interface in this case.

David Soroko
A: 

the answer is - don't take the number as a criterion for your interface. Instead, put methods in interfaces where they belong logically and you can use it in differnt classes you can use one method of interface in two or more classes so don't hesitate to use interface its a good programing techniqe.

Sanjeev
Why did you copy-paste my answer? In hope of gaining reputation? this is not the way.
Bozho
sorry dear but I am trying to give my answer.
Sanjeev
A: 

There is no right or wrong answer to how many methods are acceptable for an interface to have. Some can have zero, others one, and probably very few will ever exceed a hundred. The largest interface I've ever written was nearly thirty methods but it was a facade for clients.

However I would think that an interface that had more than say 8-10 methods would be a possible code smoke -- just something that would warrant 5 seconds to investigate.

tmeisenh
A: 

7 ± 2

John Topley