I have to recognize that I really like Lambda Expression (LE) since this feature appeared with .NET Framework 3.5 release. I actually use to apply it in a wide variety of aspects into my developments. I think that it becomes clearly useful when I have to hand some event or async call that involves just one or two statements.
However, and as often happens in these scenarios, what begins being just a single statement becomes a entire method with a bunch of statements. It arises then in something ugly, such code becomes hard to maintain. The code in question could be like this:
Which PositionChanged event initial implementation was:
Now, let’s come across Single Responsability Principle (SRP). In my opinion SRP is one of the most relevant principles not just in OO but in other fields of software engineering thus I use to take it into consideration before starting to write code. After I wrote this code, I went back to it two weeks later and realized that the meaning of having an Lambda Expression there was no longer reasonable. The modification of this code made me spend some additional time refactoring and I decided to move it entirely to a classic method.
Beyond theory, whether it violates or not the SRP, I took a pragmatic point of view that made me realize about the requirement of turning the anonymous method into a classic method, and it was to make this method more maintainable, cointaining just one responsability, however, SRP states that a class or interface, method or package may contain two different responsibilities if, and only if, there is no coupling between them. See Uncle Bob’s Modem Interface sample. That rule did not fit into my code.
I don’t want to coin a rule about using either Lambda Expression or anonymous methods in your code regarding to the accomplishment of SRP but I would say that if you write more than one or two statements into an anonymous method or Lambda Expression it’s likely that you are violating, in some way or another, the SRP and if so then the benefits of anonymous methods or Lambda Expression clearly have no sense.
Obviously, that was in my case and it’s my honest opinion. I’m sure you may have your own about it based on your own experience. That’s why SRP is just it, a Principle.