Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I realised the other day that I don't know how to talk about anonymous methods. It's easy enough to talk about named methods (The X method takes a Y1, Y2 and Y3) and classes (The X class has A, B and C methods), but it's harder with anonymous methods.

How does one go about talking (and writing) about them? From the number of parameters they take, to the types of those parameters to the method signature?


For this anonymous method signature, how would you read it aloud?

int -> int -> bool -> double

I would say "int, int, bool to double". What about calling it? How would you read this?

(A ,B) => A*B;

I would say "A B goes/is to A times B".

share|improve this question
2  
if you have to talk about it or describe it, maybe it shouldn't be anonymous? –  Colin D Apr 26 '12 at 14:10
1  
There is that, however I was thinking mostly for talking about supplying them when using something like the .NET extension methods (Where, Select, GroupBy etc) –  AndyBursh Apr 26 '12 at 14:11

5 Answers 5

up vote 14 down vote accepted

If

The X method takes P1, P2, P3 and returns R

is good enough, why is

An anonymous method that takes P1, P2, P3 and returns R

insufficient?

In my experience, the best way to talk about anonymous methods is the same way you'd talk about named methods.

For

(A, B) => A*B;

I would read it as "an anonymous method that multiplies two numbers" (or whatever A*B means in context).

share|improve this answer
1  
+1: For your last example, I'd read it "A and B yields A times B". –  StriplingWarrior Apr 26 '12 at 14:25
1  
+1 If we are talking about .NET, I'd read it as "A and B goes to A times B" from the link the lambda operator =>, which is read as "goes to" –  Joshua Drake Apr 26 '12 at 14:52
1  
Maybe it's due to my locale, but I've never heard them described or read that way. Occasionally "A and B returns A times B", but 'goes to' sounds so awkward except perhaps in a select context... Regardless, a good link. –  Telastyn Apr 26 '12 at 14:57
    
@StriplingWarrior agreed. I always read as "yeilds" –  bunglestink Apr 26 '12 at 15:44

Anonymous methods are always declared in a context, so I use that context to name it: the document ready handler, the link click handler, the lambda returned from functionX, etc.

If you add arguments into the mix, it's something like, "Make a lambda that multiplies its two arguments." Describe what it does, the same as you would when making an explicit name for it. The more formal descriptions rarely need to be spoken.

share|improve this answer

Lambas assist C#'s ability to support functional programming. In functional programming we declare what we are going to do without defining how we are going to do it. The concentration is not on the algorithm or implementation, rather on the action to be performed. E.G. In SQL we do not define how SELECT is to be performed, we simply declare that we want a SELECT to occur.

Where(), Select(), OrderBy() are actions named in the API. Like SQL, the name describes the function without describing the implementation. The way they go about their business is delegated (in this case to a Lamba/Func<>/Action<>). The delegate is just a variable to be passed in.

=> in .NET is called the goes to operator. I would read (A ,B) => A*B; aloud as "In this expression, the parameters A and B goes to A times B"

share|improve this answer

Typically, an anonymous method is being used to do something, so I would talk about it in the context of its purpose. If I absolutely must discuss the lambda without context, I'd refer to it like others have, ie, "the anonymous function taking two int and returning the multiplication of them."

But that's rare. More commonly it would be with a context, such as the following examples talking about LINQ: "The Select function gives back the names and IDs as a new object" or "The Where function filters out even numbered IDs." Lambdas are to aid in declarative program, so I'd be talking about what I'm trying to do.

share|improve this answer

Anonymous methods are often used like values, so talk about them the same way you'd talk about other values or objects. In Objective-C they're known as blocks, and it's typical to assign them to variables or pass them as parameters to other methods. In either case, you can then use the name of the variable or parameter to refer to them. You might say "the block passed in the completionHandler parameter," or more simply just "the completionHandler." If you need to describe a block outside this kind of context, you can describe what it does: "a block that cleans up after the connection is closed."

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.