9 users responded in this post

Subscribe to this post comment rss or trackback url
User Gravatar
anonymous said in September 20th, 2007 at 4:05 pm

that’s brillant

User Gravatar
Scott said in September 20th, 2007 at 4:44 pm

This is all wrong man.

User Gravatar
Scott said in September 20th, 2007 at 4:46 pm

This is all wrong man. Unreal dude.

User Gravatar
Scott said in September 20th, 2007 at 4:47 pm

Can you show me how to do it in Visual Basic?

User Gravatar
Roy Lawson said in September 20th, 2007 at 5:44 pm

How would you go about unit testing delegate methods?

"People.RemoveAll(delegate(Person person) { return person.Age > 12 && person.Age < 20; });"

Using your example, what we have is a simple way to prevent creating a new method (ie the IsTeen(person) method). But, as the delegate methods get complex it becomes difficult to read, debug, or test. At what point does it become too complex to use this approach? Is there a rule of thumb?

User Gravatar
David said in September 20th, 2007 at 7:24 pm

Another thing to remember when using anonymous methods, is that Debug-Continue will not work in Visual Studio. Just something to keep in mind

User Gravatar
AJ.NET said in September 21st, 2007 at 9:08 am

I agree that this is a most compelling feature and will reduce code amount and complexity. However I wonder whether the code becomes too simple. E.g. what about null checks (will NullReferenceException replace ArgumentNullException?). What about consistency (e.g. in one place using case sensitive comparison, case insensitive in the other. Identifying a person by name in onle place. Also checking the birth date in the other.)

Due to lambdas we _will_ see this code more often. To the good or the bad? I don’t know.

User Gravatar
Randy Patterson said in September 21st, 2007 at 11:28 am

It is not a requirement to use anonymous methods for Predicate<T> or Comparison<T> parameters but, when it makes sense, it produces concise and easily readable code. However, when anonymous methods are misused it can produce code that is difficult to maintain. Like most features, the “trick” is determining when it becomes a benefit and when it becomes the bane of your existence.

When to use Anonymous methods:

1. Single use code snippet
2. Logic is less than 5 lines of code.
3. The intent can clearly be seen in the code, otherwise a method name would better express intent (i.e method name of SortByLastNameThenFirstName)

When <B>not</B> to use Anonymous methods:

1. Code is used multiple times.
2. Requires a Unit Test
3. Code is complex enough to obfuscate intent with a quick review.

These are just guidelines and you will, no doubt, amend them as you become more comfortable using anonymous methods

User Gravatar
Jeff P said in December 20th, 2008 at 5:34 pm

Exactly what I was looking for. Thanks for posting this, Randy!
I couldn’t figure out the "Predicate" idea…was sorting through my List Collection from top to bottom.
This idea dramatically sped things up.
I did get a little snagged because I wanted to use a FIND.
Kept trying to put the findings to a LIST, but it needed to return one of the List’s objects.

I had already figured out the use of Sorting delegates a couple days ago.
I have a delegate with 6 levels of sorting and it is very fast.
I’m still a little baffled on how the Sort "works"…I’m just glad I don’t have to write it.
I remember writing those routines in CompSci. Ugh!

Jeff P.

Leave A Reply

 Username (Required)

 Email Address (Remains Private)

 Website (Optional)