Lambda

P

Peter

Hi

I am trying to get a handle on lambda expressions. I'm slowly beginning
to be able to read and understand them.

Quite often people use a single character to represent the parameter in
the expression. Is this considered good practice? At my stage it makes
it a little hard to understand the meaning.

Here is a simple example I found on a website:

List<string> names = new List<string>();
names.Add("Dave");
names.Add("John");
names.Add("Abe");
names.Add("Barney");
names.Add("Chuck");

string abe = names.Find(p => p.Equals("Abe"));

What is "p"? Does one read this by seeing that the variable "names" is
a "List<string>", and knowing that "Find" takes a "predicate" which
matches the type of the generic collection it is called on...?

Of course, real world examples can be slightly more complex, using
other classes and methods than "string" and "Equals" for example.


Thanks,
Peter
 
H

Hans Kesting

Peter wrote on 3-3-2009 :
Hi

I am trying to get a handle on lambda expressions. I'm slowly beginning
to be able to read and understand them.

Quite often people use a single character to represent the parameter in
the expression. Is this considered good practice? At my stage it makes
it a little hard to understand the meaning.

Here is a simple example I found on a website:

List<string> names = new List<string>();
names.Add("Dave");
names.Add("John");
names.Add("Abe");
names.Add("Barney");
names.Add("Chuck");

string abe = names.Find(p => p.Equals("Abe"));

What is "p"? Does one read this by seeing that the variable "names" is
a "List<string>", and knowing that "Find" takes a "predicate" which
matches the type of the generic collection it is called on...?

Of course, real world examples can be slightly more complex, using
other classes and methods than "string" and "Equals" for example.


Thanks,
Peter

That "p" is similar to a parameter of a method. Your lambda expression
translates to a method like this:

bool LambdaMethod(string p)
{
return p.Equals("Abe");
}

The compiler knows that "names" contains strings, so it can infer the
type of "p".

Hans Kesting
 
J

Joe Greer

Hi

I am trying to get a handle on lambda expressions. I'm slowly beginning
to be able to read and understand them.

Quite often people use a single character to represent the parameter in
the expression. Is this considered good practice? At my stage it makes
it a little hard to understand the meaning.

Using a single character isn't necessarily either good or bad practice.
The reason people tend to keep parameters short is to keep the size of the
lambda small. If a lambda starts getting big and complicated, then it is
time for it to become a real method of its own.
Here is a simple example I found on a website:

List<string> names = new List<string>();
names.Add("Dave");
names.Add("John");
names.Add("Abe");
names.Add("Barney");
names.Add("Chuck");

string abe = names.Find(p => p.Equals("Abe"));

What is "p"? Does one read this by seeing that the variable "names" is
a "List<string>", and knowing that "Find" takes a "predicate" which
matches the type of the generic collection it is called on...?

In this case, I probably would have called 'p' 'n' where n would stand in
for "name". The lambda is a predicate, but the parameter is a name and
that would make more sense to me.
Of course, real world examples can be slightly more complex, using
other classes and methods than "string" and "Equals" for example.

If your lambda starts getting too complex, then you need to start adding in
formatting and real names. For example, the above could be formated as:

string abe = names.Find( (string aName) =>
{
return aName.Equals("Abe");
});

Of course, in this simple case, you would never do that, but in more
complicated cases you may want to keep things understandable. Remember that
at some point you may need to come back to your code and maintain it or
adapt it to some other use. That means you (or some other programmer on
your team) need to be able to understand it at a glance.

Hope that helps,
joe
 
P

Pavel Minaev

I am trying to get a handle on lambda expressions. I'm slowly beginning
to be able to read and understand them.

Quite often people use a single character to represent the parameter in
the expression. Is this considered good practice? At my stage it makes
it a little hard to understand the meaning. ....
string abe = names.Find(p => p.Equals("Abe"));

What is "p"? Does one read this by seeing that the variable "names" is
a "List<string>", and knowing that "Find" takes a "predicate" which
matches the type of the generic collection it is called on...?

A convenient way to read that line of code is: 'Find a p in names such
that p.Equals("Abe")'.

For a short lambda like this one, naming the parameter "person"
instead would probably not make much of a difference. For something
more complicated, it might, and it's better to err on the side of
caution there (i.e. use descriptive names when in doubt).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top