views:

516

answers:

7

I recently expressed my view about this elsewhere* , but I think it deserves further analysis so I'm posting this as its own question.

Let's say that I need to create and pass around a container in my program. I probably don't have a strong opinion about one kind of container versus another, at least at this stage, but I do pick one; for sake of argument, let's say I'm going to use a List<>.

The question is: Is it better to write my methods to accept and return a high level interface such as C#'s IEnumerable? Or should I write methods to take and pass the specific container class that I have chosen.

What factors and criteria should I look for to decide? What kind of programs work benefit from one or the other? Does the computer language affect your decision? Performance? Program size? Personal style?

(Does it even matter?)

*(Homework: find it. But please post your answer here before you look for my own, so as not bias you.)

+2  A: 

The only thing that should affect your decision is how you plan to use the parameter. If you're only iterating over it, use IEnumerable<T>. If you are accessing indexed members (eg var x = list[3]) or modifying the list in any way (eg list.Add(x)) then use ICollection<T> or IList<T>.

Matt Hamilton
+13  A: 

Your method should always accept the least-specific type it needs to execute its function. If your method needs to enumerate, accept IEnumerable. If it needs to do IList<>-specific things, by definition you must give it a IList<>.

Rex M
Agreed, but I was just about to ask almost the exact same question, because my code feels awkward using IEnumerable; instead of List<Foo> foos = fooQuery.GetFoos(), it becomes List<Foo> foos = new List<Foo>(fooQuery.GetFoos()). Am I missing something? Or is this the price we pay for flexibility?
Si
An example using the above comment is in unit tests where you want to verify results e.g. IList.Count, actual usage doesn't really need IList, and IEnumerable will suffice, but it just feels awkward...
Si
@Si I think we are talking about different things here. This question is only about the scope inside the method; you're talking about return types and usage outside the method.
Rex M
A: 

It does matter, but the correct solution completely depends on usage. If you only need to do a simple enumeration then sure use IEnumerable that way you can pass any implementer to access the functionality you need. However if you need list functionality and you don't want to have to create a new instance of a list if by chance every time the method is called the enumerable that was passed wasn't a list then go with a list.

Quintin Robinson
+2  A: 

There is always a tradeoff. The general rule of thumb is to declare things as high up the hierarchy as possible. So if all you need is access to the methods in IEnumerable then that is what you should use.

Another recent example of a SO question was a C API that took a filename instead of a File * (or file descriptor). There the filename severly limited what sores of things could be passed in (there are many things you can pass in with a file descriptor, but only one that has a filename).

Once you have to start casting you have either gone too high OR you should be making a second method that takes a more specific type.

The only exception to this that I can think of is when speed is an absolute must and you do not want to go through the expense of a virtual method call. Declaring the specific type removes the overhead of virtual functions (will depend on the language/environment/implementation, but as a general statement that is likely correct).

TofuBeer
+1  A: 

rarely use IEnumerable<T> - it's soooo damn slow
better use ICollection<T> or IList<T>

have a look at this

Andreas Niedermair
Let me guess - you write realtime hardward controllers for a living? Or for some reason you write graphics drivers in C#?
Daniel Earwicker
nope, but why should a consumer of your service wait (think of multi-user-applications)?! the executiontime sums up multiplied by the consumers... (factor 12-15)
Andreas Niedermair
But what if you're talking about a difference of nanoseconds? Multiply it by a thousand, and you're still only talking about microseconds. Multiply by a million and you're still only talking about milliseconds.
Daniel Earwicker
Moral: benchmark results should have units on them!
Daniel Earwicker
in this case, from experience, this won't be this small scale :)
Andreas Niedermair
A: 

I answered a similar C# question here. I think you should always provide the simplest contract you can, which in the case of collections in my opinion, ordinarily is IEnumerable Of T.

The implementation can be provided by an internal BCL type - be it Set, Collection, List etcetera - whose required members are exposed by your type.

Your abstract type can always inherit simple BCL types, which are implemented by your concrete types. This in my opinion allows you to adhere to LSP easier.

Ed Blackburn
A: 

It was a discussion with me that prompted this question, so Euro Micelli already knows my answer, but here it is! :)

I think Linq to Objects already provides a great answer to this question. By using the simplest interface to a sequence of items it could, it gives maximum flexibility about how you implement that sequence, which allows lazy generation, boosting productivity without sacrificing performance (not in any real sense).

It is true that premature abstraction can have a cost - but mainly it is the cost of discovering/inventing new abstractions. But if you already have perfectly good ones provided to you, then you'd be crazy not to take advantage of them, and that is what the generic collection interfaces provides you with.

There are those who will tell you that it is "easier" to make all the data in a class public, just in case you will need to access it. In the same way, Euro advised that it would be better to use a rich interface to a container such as IList<T> (or even the concrete class List<T>) and then clean up the mess later.

But I think, just as it is better to hide the data members of a class that you don't want to access, to allow you to modify the implementation of that class easily later, so you should use the simplest interface available to refer to a sequence of items. It is easier in practice to start by exposing something simple and basic and then "loosen" it later, than it is to start with something loose and struggle to impose order on it.

So assume IEnumerable<T> will do to represent a sequence. Then in those cases where you need to Add or Remove items (but still don't need by-index lookup), use IContainer<T>, which inherits IEnumerable<T> and so will be perfectly interoperable with your other code.

This way it will be perfectly clear (just from local examination of some code) precisely what that code will be able to do with the data.

Small programs require less abstraction, it is true. But if they are successful, they tend to become big programs. This is much easier if they employ simple abstractions in the first place.

Daniel Earwicker