Anonymous Methods and Types
-
Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?
Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!
-
Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?
Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!
No. You just need to learn to love them. Update: The best thing about them are that they are closures. Closures are the poor man's objects*. Objects are the poor man's closures. * As in OOP, eg classes
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Editionmodified on Wednesday, March 10, 2010 1:11 PM
-
Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?
Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!
It really depends on the situation. I have used them in thread callback methods, where it saves me for creating a one lined method. My standard is that if Anonymous methods over 3 lines they better be made into a regular method. In case of lambda expressions, I do not go beyond a single statement.
-
Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?
Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!
What does my head in are the clowns who insist on replacing every single foreach loop with a ForEach lambda and calling it refactoring. I do kinda prefer typing => to having the word 'delegate' and type names repeated everywhere though.
-
Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?
Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!
Anonymous types are handy when you want to databind properties that are not directly available in the form you want them to be in. Without anonymous types, you'd end up creating an inner private struct or class merely for databinding which is kinda overkill.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com link -
Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?
Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!
There are some designs where they lead to easier to read code. I have some code that renders a tree view I use this with different lists of objects. I take 2 delegates that provide a grouping key and an area for each node. In the past I would have added an interface to each object I wanted to use, or create methods in the object, or a helper object to pass as parameters, now I just use lambdas. The objects never have to know that they will be used this way, the renderer can be used on any object, and the caller of the renderer is the only code responsible for making sure the objects can be rendered.
I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon
-
Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?
Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!
Because other languages have them and the C# team wants C# to be everything? :~
-
Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?
Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!
Very useful when you need to overwrap some component event handler add a new one and then return the original delegate. For example you could catch Mouse Up event over some DataGridView component, save a reference to the original event handler and remove it, add a new one with your code, which makes something useful – modify the behavior when user rearrange the columns and then remove your handler and return the original Mouse Down event handler. All this in a few rows short method. Here is some simple example:
void bindGrid\_ColumnDisplayIndexChanged(object sender, DataGridViewColumnEventArgs e) { DataGridView thisGrid = sender as DataGridView; MouseEventHandler mouseEventHandlerToRemove = null; MouseEventHandler mouseEventHandler = delegate(object mSender, MouseEventArgs mouseEventArgs) { foreach (DataGridViewColumn clmn in thisGrid.Columns) { if (clmn is DataGridViewButtonColumn) { clmn.DisplayIndex = (thisGrid.Columns\[clmn.Index - 1\].DisplayIndex + 1); } } thisGrid.MouseUp -= mouseEventHandlerToRemove; }; mouseEventHandlerToRemove = mouseEventHandler; thisGrid.MouseUp += mouseEventHandler; }
The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.
-
Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?
Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!
Only one place I use var, aside from LINQ...
var something = new This.Really.Long.Namespace.Definition.Wastes.Lots_Of.Space.AndMakes.TheCode.Look.Sloppy();
Only when it's all together like this, so there's no question what type the variable is. (Obviously, if I was using this several times in the same code file, I'd shorten it with 'using' instead, but not for a one-off) As for anonymous methods... They're great in LINQ, whether in the LINQ syntax or just the LINQ extensions (ListOfStuff.Select(a=>a.Name).OrderBy(a=>a.SubString(2));
)... I might use them on occasion for a quick event handler, but only for trivially-simple ones... It makes for clean-looking code, but it's a bit counter-intuitive, because you have to remember that the anonymous method won't necessarily be executed there and then.Proud to have finally moved to the A-Ark. Which one are you in?
Author of Guardians of Xen (Sci-Fi/Fantasy novel) -
Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?
Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!
hopingToCode wrote:
Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg
Pretty impressive! How long did that take? brain fart. Just noticed you've mentioned the time taken too. :-O I might put myself on a muscle gain program. Best arm size from the past = 18 inches, relaxed. Current size after 3 years of rest for the back injury to heal = 14.2 inches. Target = old size. Going to take ages. :( BTW, I think that an arc trainer could possibly be the best calorie burning machine. I burned 200 calories in 10 minutes this evening. :)
“Follow your bliss.” – Joseph Campbell
-
Because other languages have them and the C# team wants C# to be everything? :~
This is not entirely correct, the main C #prototypes - Java and Object Pascal has neither anonymous methods nor delegates.
The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.
-
hopingToCode wrote:
Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg
Pretty impressive! How long did that take? brain fart. Just noticed you've mentioned the time taken too. :-O I might put myself on a muscle gain program. Best arm size from the past = 18 inches, relaxed. Current size after 3 years of rest for the back injury to heal = 14.2 inches. Target = old size. Going to take ages. :( BTW, I think that an arc trainer could possibly be the best calorie burning machine. I burned 200 calories in 10 minutes this evening. :)
“Follow your bliss.” – Joseph Campbell
Rajesh R Subramanian wrote:
Best arm size from the past - 18 inches, relaxed.
Holy Cow! :omg:
The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.
-
Only one place I use var, aside from LINQ...
var something = new This.Really.Long.Namespace.Definition.Wastes.Lots_Of.Space.AndMakes.TheCode.Look.Sloppy();
Only when it's all together like this, so there's no question what type the variable is. (Obviously, if I was using this several times in the same code file, I'd shorten it with 'using' instead, but not for a one-off) As for anonymous methods... They're great in LINQ, whether in the LINQ syntax or just the LINQ extensions (ListOfStuff.Select(a=>a.Name).OrderBy(a=>a.SubString(2));
)... I might use them on occasion for a quick event handler, but only for trivially-simple ones... It makes for clean-looking code, but it's a bit counter-intuitive, because you have to remember that the anonymous method won't necessarily be executed there and then.Proud to have finally moved to the A-Ark. Which one are you in?
Author of Guardians of Xen (Sci-Fi/Fantasy novel)Anonymous type…isn’t this SO gay VB?
The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.
-
Rajesh R Subramanian wrote:
Best arm size from the past - 18 inches, relaxed.
Holy Cow! :omg:
The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.
-
Anonymous types are handy when you want to databind properties that are not directly available in the form you want them to be in. Without anonymous types, you'd end up creating an inner private struct or class merely for databinding which is kinda overkill.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com linkIn other words, a closure.
-
Anonymous type…isn’t this SO gay VB?
The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.
Nah... Ya know, I originally learned to program in BASIC, and learned windows-based programming in VB3 (MFC was intimidating at that age)... Having long since outgrown that, I know all about the horrible things VB3 lets you do... Anonymous types in C# are NOTHING like the old "Variant" type in VB, even though they share a few letters. Variants are late-bound and utterly evil, but the C# "var" is just a syntax shortcut (The compiler knows the type). Now, the "dynamic" keyword they're adding in the next version... That's going to cause untold misery...
Proud to have finally moved to the A-Ark. Which one are you in?
Author of Guardians of Xen (Sci-Fi/Fantasy novel) -
Apart from LINQ- WHY???? It makes for messy code and harder to maintain code. At least that's all I've seen so far. any thoughts on the opposite?
Weight loss Target Weight at start [1/Feb/2009] 127kg Weight now [17/Feb/2010] 97.5kg Target weight : 80kg Only 17.5 to go hope to be there by July Wish me luck!
I agree completely. Clarity and simplicity always win the day over slightly more verbose code in my mind. In my first job many years ago I wrote a line of C++ code which took up about 4 lines. I used all the clever things you could do, assignments as expressions, ternary operators and such and marveled at how much I'd managed to fit between semicolons. My boss didn't like it though and taught me the most important lesson I've adhered to ever since - to make everything as clear and simple as possible. If only everyone else would choose simplicity over elegance or cleverness life would be much, much easier.
Regards, Rob Philpott.
-
I agree completely. Clarity and simplicity always win the day over slightly more verbose code in my mind. In my first job many years ago I wrote a line of C++ code which took up about 4 lines. I used all the clever things you could do, assignments as expressions, ternary operators and such and marveled at how much I'd managed to fit between semicolons. My boss didn't like it though and taught me the most important lesson I've adhered to ever since - to make everything as clear and simple as possible. If only everyone else would choose simplicity over elegance or cleverness life would be much, much easier.
Regards, Rob Philpott.
In other words... "Just because you can, doesn't mean you should."
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
Because other languages have them and the C# team wants C# to be everything? :~
bingo. someone needs to tell the C# features team to take a few years off.
-
This is not entirely correct, the main C #prototypes - Java and Object Pascal has neither anonymous methods nor delegates.
The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.
And you can bet the C# team is having a good snicker over that...
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001