Using a lambda expression versus a private method

2019-03-11 01:28发布

I read an answer to a question on Stack Overflow that contained the following suggested code:

Action<Exception> logAndEat = ex => 
{  
    // Log Error and eat it
};

try
{
    // Call to a WebService
}
catch (SoapException ex)
{
    logAndEat(ex);
}
catch (HttpException ex)
{
    logAndEat(ex);
}
catch (WebException ex)
{
    logAndEat(ex);
}

My question is: what is the advantage (if any) of using a lambda expression for LogAndEat as opposed to the (in my view simpler and more obvious) private method as follows:

private void LogAndEat(Exception ex)
{
    // Log Error and eat it
}

Edit: Thanks for the answers so far but just restating my fundamental question a little more clearly: which approach is better/would you recommend in this instance? A lambda expression or a private method?

标签: c# .net lambda
7条回答
ゆ 、 Hurt°
2楼-- · 2019-03-11 01:34

I guess you could think of the lambda expression in this example as being a little bit like Pascal's nested functions, in that it's code that can only be executed by the method it's declared in.

A private method could be called from any method in the same class, while a lambda like this is local to the current method and is therefore explicitly only used in that context.

That's really the only advantage I can think of - being explicit in terms of expected usage.

查看更多
Lonely孤独者°
3楼-- · 2019-03-11 01:42

Variables captured by logAndEat would otherwise be parameters to the LogAndEat method. You could consider it a form of currying.

查看更多
神经病院院长
4楼-- · 2019-03-11 01:43

Thanks everyone for the great answers which I have up-voted, but I thought I'd summarize them to try and capture the pros and cons in one answer.

Pros of using a lambda expression (LE) instead of a private method:

  • A LE is scoped to the method in which it is declared so if it is only used by that method then that intent is made explicit by a lambda expression (even though it is possible to pass out a delegate to the LE, one can still argue the intent of declaring a LE in a method is that the LE is scoped to the method). That is, being explicit in terms of its expected usage.
  • Lambda expressions behave like closures so they can access variables scoped to the method they are declared in. This can be neater than passing lots of parameters to a private method.
  • Variables captured by a LE would otherwise be parameters to a private method and this can be exploited to allow a form of currying.

Cons of using a lambda expression instead of a private method:

  • Because the LE can access variables scoped to the method in which they are contained, it is not possible to modify code in the calling method while debugging.

There is also the more subjective issue of maintainability and one could argue that LE are not as well understood by most developers as a private method and thus are somewhat less maintainable. One could also argue that a LE improves maintainability because it is encapsulated in the method in which it is called as opposed to a private method which is visible to the entire class.

查看更多
戒情不戒烟
5楼-- · 2019-03-11 01:46

LogAndEat can reference private fields within the function where it's defined. So:

private bool caughtException; 

Action<Exception> logAndEat = ex => 
    {  
        caughtException = true;
    };

try
{
    // Call to a WebService
}
catch (SoapException ex)
{
    logAndEat(ex);
}
catch (HttpException ex)
{
    logAndEat(ex);
}
catch (WebException ex)
{
    logAndEat(ex);
}

if (caughtException)
{
    Console.Writeline("Ma, I caught an exception!");
}

This is a trite example (!) but this potentially can be a lot tidier than passing a bunch of parameters through to a private method.

查看更多
相关推荐>>
6楼-- · 2019-03-11 01:47

A lot comes down to personal preference, nobody can say one absolute way is the right way or the wrong way.

Lambda Expressions behave like closures in other languages, the cool thing about them is that they can access variables scoped to the method they're declared in. This adds a lot of flexibility to what you can do in your code, but comes at a cost of not being able to modify code in that method while debugging.

For that reason, if you're going to be logging errors, you might find yourself in a debugger in that method at some time or another. If you use a lambda, you won't be able to modify any code at runtime - so for this reason my preference would be to use a separate private method that accepts the exception as its parameter.

查看更多
劳资没心,怎么记你
7楼-- · 2019-03-11 01:54

IMO I don't like tiny little private functions that are only used in another private method wondering around my classes I just find them ugly, that's why I'd use the lambda in that sample

I don't think there's gonna be a performance impact in using the lambda instead of a function

查看更多
登录 后发表回答