Return default value on "invalid" input.

S

shapper

Hello,

I have 3 methods that:
1 - Return one random record from a list;
2 - Return many random records from a list;
3 - Order a list randomly.

At the moment I am trowing exceptions where the collection is empty or
null but this does not make sense:

1:
public static T Random<T>(this IEnumerable<T> collection) {

if (collection == null) throw new
ArgumentNullException("collection");
if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");

2:

public static IEnumerable<T> Random<T>(this IEnumerable<T>
collection, Int32 size) {

if (collection == null) throw new
ArgumentNullException("collection");
if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");
if (size <= 0) throw new ArgumentOutOfRangeException("size",
"Size must be greater than zero");

3:

public static IEnumerable<T> OrderRandomly<T>(this IEnumerable<T>
collection) {

if (collection == null) throw new
ArgumentNullException("collection");
if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");


For 3, the OrderRandomly, I think I could just return the collection
again:
if (collection == null) return collection;

What should I return on 1 and 2?
I was trying to return null but got errors ...

Thanks,
Miguel
 
S

shapper

Hello,

I have 3 methods that:
1 - Return one random record from a list;
2 - Return many random records from a list;
3 - Order a list randomly.

At the moment I am trowing exceptions where the collection is empty or
null but this does not make sense:

1:
    public static T Random<T>(this IEnumerable<T> collection) {

      if (collection == null) throw new
ArgumentNullException("collection");
      if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");

2:

    public static IEnumerable<T> Random<T>(this IEnumerable<T>
collection, Int32 size) {

      if (collection == null) throw new
ArgumentNullException("collection");
      if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");
      if (size <= 0) throw new ArgumentOutOfRangeException("size",
"Size must be greater than zero");

3:

    public static IEnumerable<T> OrderRandomly<T>(this IEnumerable<T>
collection) {

      if (collection == null) throw new
ArgumentNullException("collection");
      if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");

For 3, the OrderRandomly, I think I could just return the collection
again:
if (collection == null) return collection;

What should I return on 1 and 2?
I was trying to return null but got errors ...

Thanks,
Miguel

Maybe default<T> for (1) and List<T> for (2)?
 
A

Arne Vajhøj

Hello,

I have 3 methods that:
1 - Return one random record from a list;
2 - Return many random records from a list;
3 - Order a list randomly.

At the moment I am trowing exceptions where the collection is empty or
null but this does not make sense:

1:
public static T Random<T>(this IEnumerable<T> collection) {

if (collection == null) throw new
ArgumentNullException("collection");
if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");

2:

public static IEnumerable<T> Random<T>(this IEnumerable<T>
collection, Int32 size) {

if (collection == null) throw new
ArgumentNullException("collection");
if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");
if (size<= 0) throw new ArgumentOutOfRangeException("size",
"Size must be greater than zero");

3:

public static IEnumerable<T> OrderRandomly<T>(this IEnumerable<T>
collection) {

if (collection == null) throw new
ArgumentNullException("collection");
if (collection.Count() == 0) throw new ArgumentException("The
collection cannot be empty", "collection");


For 3, the OrderRandomly, I think I could just return the collection
again:
if (collection == null) return collection;

What should I return on 1 and 2?
I was trying to return null but got errors ...

To me an exceptions makes perfectly sense.

If you try to retrieve a single element from an empty collection
then an exception seems as the only logical way to handle that.

And even for getting a collection, then I would prefer the
exception over a collection with zero elements.

Arne
 
S

shapper

To me an exceptions makes perfectly sense.

If you try to retrieve a single element from an empty collection
then an exception seems as the only logical way to handle that.

And even for getting a collection, then I would prefer the
exception over a collection with zero elements.

Arne

But for example, consider I am getting a random record from a list to
display.
Very often, by many reasons, that list might be empty.
So it seems more logic to check if the result is, for example, null
then getting an exception. Not?

An exception seems more logic to me if I provide an invalid
argument ...
But if I am providing a list then it is a valid argument just happens
that it is empty.
 
P

Peter Duniho

shapper said:
[...]
But for example, consider I am getting a random record from a list to
display.
Very often, by many reasons, that list might be empty.
So it seems more logic to check if the result is, for example, null
then getting an exception. Not?

What if the list has null elements in it? How is the caller supposed to
know the difference between the method's failure to accomplish what it
was asked to do and it just randomly returning that null element?
An exception seems more logic to me if I provide an invalid
argument ...
But if I am providing a list then it is a valid argument just happens
that it is empty.

Just because the argument is a valid object, that doesn't make it valid
input.

To order an empty enumeration randomly, I agree that simply returning an
empty enumeration is fine.

But I agree with Arne that the other two scenarios call for an
exception. A caller should have no reasonable expectation that if it
asks for a randomly chosen element from an empty collection, that your
method could return an element. I can't get a cookie from an empty
cookie jar, why should you be able to get a random element from an empty
collection?

And the second method is even more obviously deserving of throwing an
exception on input that doesn't fit. The caller is specifically asking
for a certain number of elements. It's simply not possible for the
method to return that number of elements if there aren't that many in
the collection to start with.

The only way I can see the above analysis failing to apply is if you
decide that an empty collection is treated as though it has enough
default-value elements to satisfy the caller's request. In that case,
you could return "default(T)" for the first method, and in the second
method you would have to pad the input with enough "default(T)"
instances so that there are "size" elements to choose from.

But really, it seems like those are situations where an exception should
be thrown. The caller should not be calling the methods in those cases,
because there's no reasonable thing the methods can return.

Pete
 
P

Patrice

Hello,
What should I return on 1 and 2?

For 1 and 2 I would keep using exceptions. It just doesn't do what you
described i.e. returning 1 elements or many elements...

For 3 this is debatable (IMO if null it should throw, if the collection is
empty it could be considered as doing its job that is an orderer empty list
is an empty list).
I was trying to return null but got errors ...

Please never post about an error without telling which error you get.

The empty collection you got would be better. IMO null should throw as
trying to sort a null collection doesn't make sense to me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top