Re: Lambda expressions: Dangerous Madness
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Thu, 14 Aug 2008 11:00:58 +0100
raylopez99 <raylopez99@xxxxxxxxx> wrote:
Took a look at all the fuss about "lambda expressions" from Jon
Skeet's excellent book "C# in Depth". Jon has an example, reproduced
below (excerpt) on lambda expressions.
My n00b take: it's like SQL and those 'high level' languages that try
to be too clever by half and pack a lot of syntax in as short a space
as possible. Like Egyptian hieroglyphics, like Chinese, like those
non-alphabet languages, it's just way too much information in too
short a space. Sure, writing a program with this syntax will reduce
your source code from 100 pages to 10, but at a cost of readability.
I'd say they greatly add to readability, actually. Yes, you need to get
used to them first, but that's no reason to avoid them.
I think there's actually a shock value in how much you're able to
express in so little code - when you're used to verbose code, it can be
alarming to see how compact it can really be when you have suitable
language features. That's a perception issue though - not a
maintainability issue.
But what do I know? I only have a couple months real experience in
the language. Don't listen to me.
Many powerful features look too hard to start with. Give them a chance
- when you're familiar with them, lambda expressions are *wonderful*.
What would you rather write:
products.OrderBy(product => product.Price)
or
products.OrderBy(PriceComparison)
.... later on in code (second version only) ...
int PriceComparison(Product first, Product second)
{
return first.Price.CompareTo(second.Price);
}
? I know which one *I* find clearer and easier to modify when
necessary.
//Jon humorously states "the combination of the lambda expression
putting the test in just the right place and a well-named method means
we can almost read the code out loud and understand it without even
thinking" HA HA HA! Right. I guess beauty is in the eye of the
beholder.
I wasn't kidding. Read the code out loud - it does exactly what it
says. That's great for readability.
--
Jon Skeet - <skeet@xxxxxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
.
- Follow-Ups:
- Re: Lambda expressions: Dangerous Madness
- From: Peter Morris
- Re: Lambda expressions: Dangerous Madness
- References:
- Lambda expressions: Dangerous Madness
- From: raylopez99
- Lambda expressions: Dangerous Madness
- Prev by Date: Re: Form be auto show after minimizied
- Next by Date: Re: C#--is it growing? Or saturated? (Charles Petzold does C#)
- Previous by thread: Lambda expressions: Dangerous Madness
- Next by thread: Re: Lambda expressions: Dangerous Madness
- Index(es):
Relevant Pages
|