Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Opinions Wanted: When To use Anonymous Delegates, Where do you draw the line?

I am looking for feedback from the group.  I would like to know where others out there draw the line when it comes to using Anonymous Delegates. 

Personally I like Anonymous Delegates, I think they can be simple and elegant.  But I only like to use them if they are simple and easy to read.  Basically I tend to only like them in scenarios where one line (or so) of code is needed.  If the is more than one line of code, i like to put that code into a concrete method.

Example One, Simple/Elegant:

Example Two, using a Concrete method:

Example Three, using an Anonymous Delegate:

Of the three examples above, I like the first and second, but I don't like the 3rd one.  Personnaly I think this is too much and should not be used.

What is everyones thoughts?

Posted 04-11-2007 7:54 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Jeremy wrote re: Opinions Wanted: When To use Anonymous Delegates, Where do you draw the line?
on 04-11-2007 10:14 AM

I agree, I love anonymous methods but only when I have a concise section of code to put into them.  In most cases, I only use them when i have a single line of code but I have been known to use them if I have two or three lines.  Where I draw the line is conditional statements like in your third example.  If you have logic that requires branching then you need another method or your code gets awkward looking very fast.

If it's just sequential, however, I'm OK with 2 - 3 methods..like here...

#    public void Foo()  

#    {  

#       MyDelegate del = delegate()  

#                          {  

#                              Console.WriteLine("Hello");  

                               Console.WriteLine("World");

                               Console.WriteLine("!");

#                          };  

#       del();    

#    }  

Rob Eisenberg wrote re: Opinions Wanted: When To use Anonymous Delegates, Where do you draw the line?
on 04-11-2007 10:53 AM

I have to agree that examples one and two are nice, with three leaving much to be desired.  In practice, I tend to follow the same pattern that Jeremy mentions, although I have never codified it.  I think he presents a very good rule of thumb though.

Marko wrote re: Opinions Wanted: When To use Anonymous Delegates, Where do you draw the line?
on 04-11-2007 11:02 AM

I agree with you guys, and like to add that leaving conditional code to execute or more than the 3 line "rule of thumb" significantly adds to really ugly code down the road if you have to extend your code.  Somebody who doesn't know much about anonymous methods could simply keep on adding and adding code in there to meet a ton of conditions.

Jason Bock wrote re: Opinions Wanted: When To use Anonymous Delegates, Where do you draw the line?
on 04-11-2007 11:14 AM

Same here. If the implementation of the anonymous method is very short, 1-3 lines of code (AND there's no reuse opportunities - i.e. it's a one-off), then make it anonymouse. Otherwise, define the method explicilty.

Kalpesh wrote re: Opinions Wanted: When To use Anonymous Delegates, Where do you draw the line?
on 04-11-2007 11:23 AM

I think, I would use anonymous delegates only when it contains code, which will not be written again somewhere else & it becomes easy to read the code when its tied together

-- psuedocode

p.GetCustomers(p -> return p.Sales > 1000000;)

Also, the need to share the context variable

int i = console.read();

p.GetCustomers(p -> return p.Sales > i)

Anonymous delegate can use variables from the scope. I dont know what compiler does in such a case. But it is a good thing

Eber Irigoyen wrote re: Opinions Wanted: When To use Anonymous Delegates, Where do you draw the line?
on 04-11-2007 3:11 PM

I use them heavily on "aliased" generic collections (http://ebersys.blogspot.com/search?q=aliased), but always put them on the business layer, so if a collection returns some specific information based on some criteria, I write a new method that takes a parameter, then write an anonymous method (this reminds me I need to write another post on that topic), this shields the front end from the anonymous delegates, while I make good use of them on the backend using closures

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)