Ayende @ Rahien

Refunds available at head office

Find the design flaw

This method has a design flaw, can you see it?

image

Hint, it is using the yield keyword.

Tags:

Posted By: Ayende Rahien

Published at

Originally posted at

Comments

ashic
05/22/2010 07:34 AM by
ashic

It's returning an IEnumerable using yield. The caller would take out the ones they need. There's no sense in passing parameters like limit as it would the the callers' responsibility to get only the number they need. IEnumerable won't trigger data access until and unless the data is required and so the limit parameter doesn't have any use.

James Newton-King
05/22/2010 07:44 AM by
James Newton-King

An endId and limit seem at odds with one another.

Ayende Rahien
05/22/2010 08:41 AM by
Ayende Rahien

James,

startId: 1

endId: 100000

limit: 100

Chojrak
05/22/2010 08:55 AM by
Chojrak

I'd change it to IEnumerable <dictionary<int,jsondocument>

, I think key-value dependencies are better handled using Dictionary. Any ideas?

Chojrak
05/22/2010 08:56 AM by
Chojrak

Oh no, damned HTML. Again:

I'd change it to IEnumerable<Dictionary<int,JsonDocument>> I think key-value dependencies are better handled using Dictionary. Any ideas?

Ayende Rahien
05/22/2010 08:58 AM by
Ayende Rahien

Chojrak,

It isn't key value. It is a tuple, it isn't a set of int to json doc, it is a int & json doc.

Patrik
05/22/2010 09:38 AM by
Patrik

This might be personal preference but I really don't like methods exposing tuple types. Create a type (possibly a struct) that has carries some semantics instead of the tuple.

Note that this in C#, this would obviously not be my thoughts if the language was F#, Haskell, Erlang or any other functional language.

James Newton-King
05/22/2010 09:42 AM by
James Newton-King

Is the collection between the startId and endId not continuous? A limit would make sense then.

Ayende Rahien
05/22/2010 09:54 AM by
Ayende Rahien

James,

Yes, the collection may not be continuous

Ayende Rahien
05/22/2010 09:54 AM by
Ayende Rahien

Patrik,

This is mostly for internal API needs, it just doesn't make sense to create a type here, it wouldn't add enough meaning

Andrew
05/22/2010 09:56 AM by
Andrew

Is it not also bad practice to publicly expose IEnumerable for methods like this ?

Ayende Rahien
05/22/2010 10:03 AM by
Ayende Rahien

Andrew,

I disagree with that quite strongly

Andrew
05/22/2010 10:19 AM by
Andrew

Yeah now that I've thought it through, I'm unsure why I even said that. I read something like this on someone's blog recently and haven't had time to process it.

Dmitriy Nagirnyak
05/23/2010 11:56 PM by
Dmitriy Nagirnyak

When using 'yield' the resulting items will be returned 'lazily' (delayed execution).

This might not be what the method is intended to do. I think the intention is to have the results ready after the call.

It also might have potential issue when the underlying data store is closed and the enumeration gets executed.

Cheers,

Dmitriy,

Martin R-L
06/17/2010 06:27 AM by
Martin R-L

@Patrik,

What I do in order to use the ease of tuples and other complex generic types, and get readable code, is to at least give them a friendly name by utilizing the using statement of C#.

An example is given on my blog: http://goo.gl/ksCm

In a specific context, you can also give the type friendly method names (like e.g. Ruby's alias methods) by using extension methods that are just simple pass-throughs.

Comments have been closed on this topic.