tags:

views:

194

answers:

2

I'm writing a lot of code with lambdas these days.

return _schema.GetAll<Node>()
           .ToList()
           .FindAll(node => node.Type == NodeType.Unmanaged)
           .Cast<Shape>()
           .ToList();

Note: GetAll() returns an IList.

Can i get any terser?

+2  A: 

This should work.

return _schema.GetAll<Node>()
    .Where(node => node.Type == NodeType.Unmanaged)
    .Cast<Shape>()
    .ToList()

If your method had a return type of IEnumerable<Shape> you wouldn't need to call ToList().

You could also write it like this (with IEnumerable<Shape> return type):

return from node in _schema.GetAll<Node>()
       where node.Type == NodeType.Unmanaged
       select node as Shape;
spoon16
+3  A: 
  1. You could replace the ToList followed by a FindAll with a Where.
  2. A popular standard with lambda parameters in simple statements is a single character. 'node' could be renamed to just 'n'.
  3. Your method could return an IEnumerable instead of a IList. The method caller could then call ToList if required.

After:

return _schema.GetAll<Node>().Where(n => n.Type == NodeType.Unmanaged).Cast<Shape>();
James Newton-King
Where did you find the "naming standard" for lambdas? Your suggestion can harm readability in many cases.
Mehrdad Afshari
All the code examples in the .NET Framework Design Guidelines book that contained lambdas only used a single character for lambda parameter names - http://blogs.msdn.com/mirceat/archive/2008/03/13/linq-framework-design-guidelines.aspx
James Newton-King
and you believe code examples in that book constitute "the naming standard?" While in some cases, a single letter does make sense, for more complex expressions, it can be a major source of confusion. Those are just examples (designed to be concise, not necessarily readable and maintainable).
Mehrdad Afshari
Is it official? No. However most lambda code examples that come out of Microsoft uses a single character as the parameter name - http://weblogs.asp.net/scottgu/archive/2007/04/08/new-orcas-language-feature-lambda-expressions.aspx - that book is just one example (although notable in that it is written by the people who decide the standards).
James Newton-King
Both of you win... the reality is that one letter names are a practiced standard if not a stated standard. Just because other people do it does not mean you have to, if you like to be more expressive in your lambdas than super :)
spoon16
I like the single character, especially when the logic in the condition is a simple evaluation.
geejay
eh, that's BS. The "naming standard" says no such thing. The rule is pretty much what it always is. Use whatever is most readable. In simple lambdas with simple types and one or two parameters, a single letter is often cleaner. In more complex lambdas, or when it's not clear from the single letter what that parameter means, use a more descriptive name. -1 for encouraging unreadable code.
jalf
writing complex lambdas instead of putting them in methods is unreadable code in itself. +1 to counteract a bad argument.
Jimmy