Typically, when I use lambdas, I just use "a, b, c, d..." as variable names as the types are easily inferred, and I find short names to be easier to read. Here is an example:
var someEnumerable = GetSomeEnumerable();
var somethingElseList = someEnumerable.Select(a => a.SomeProperty)
.OrderBy(a => a.SomePropertyField);
var someDictionary = somethingElseList.ToDictionary(a => new SomeClass(a.Prop1),
a => a);
Some question this naming, and would prefer to see long typed out names, like this:
var someEnumerable = GetSomeEnumerable();
var somethingElseList = someEnumerable.Select(importantObj => importantObj.SomeProperty)
.OrderBy(objsInfo => objsInfo.SomePropertyField);
var someDictionary = somethingElseList.ToDictionary(theInfoId => new SomeClass(theInfoId.Prop1),
theInfoId2 => theInfoId2);
Since the scope is so narrow (between the parens), unless you're getting stupid and nesting them, I find it easier to read short names.
Without getting caught up in the silly naming examples I used above, what is the general consensus on Lambda variable names? To short name, or not to short name?
I go with one, two or three letters, which form an abbreviated representation of the object in question.
I'd say I agree with BFree. That, for me, it will depend on the context, but I always try to make it as 'concise' as possible. Notice I say concise, and not terse or short.
Here's two examples in the LISP dialect Clojure:
For those that don't know Lisp/Clojure, my lambda is the argument to the function 'filter'. That is, "(fn [name] ....)". I chose to use 'name' here because it's short but describes exactly what I'm working with. However, I think an 'a', 'i', 'x', etc would be just as readable.
Here, I just use 'n' to stand for 'number'. I think 'number' would be OK too, but is slightly too verbose for my taste.
I certainly would NOT use the names 'nameOfPerson' or 'previousOddNumber'. That's just WAY too much information. I also try to keep types out of my names most of the time, e.g. 'nameString'. I find stuff like that, most of the time, to be superfluous information.
Personally, I think extra verbose names like that tend to stem from the idea that it helps document the code. It is especially prevalent in languages like Java/C# it seems. I think this could be argued both ways, depending on the programmer's style. However, the more verbose the name, the more tight (read brittle) the code can become. If my name is very specific, and it's a function changes often, then chances are the name will have to change a lot too. Eventually, the DEV might get lazy and you'll end up with a name that doesn't actually describe what the variable is used for. This is not a problem, if and only if the programmer doesn't assume the name is correct. Granted, this would probably be figured out quickly during compilation (because the programmer will try to divide two strings or some such thing), but it can cause wasted time and confusion in larger projects.
I would also argue for conciseness because of screen real-estate. I still try to wrap my rows at 80 columns wide, and that's hard when 'myVaraibleNameIsAsLongAsAParagraph'.
Ultimately, I think it's always going to come down to compromise. So they don't like 'a', but maybe they can agree that you should strive for one-word names like 'person' or 'number', and avoid that awful camel case.
Sorry for the book.
I like the shortname thing. I do that all the time. I mostly use i,y,x in lambdas, but I use a,b,c,d in sql.
Our team does not allow single letter variables, not event in lamdas.
What we find is, just like in TSQL, the longer the code lives the more confusing it gets. As far as lambda's
We would do something like this:
That way the next dev will not have to look at
It seems readable, but it always does at first
What about a join
the same with variable names
Oh but this is confusing and ugly. Just use differenct letters so it less confusing
Still confusing, but now its worse. So we break the lambda up so we refactor for readability
This is not a perfect example but make the code readable with very little insider knowledge. We find this exposes complex code(lambda) which needs to be broke up into smaller chunks.
Shortening is better if the purpose of the variable is clear. Lambdas call for a functional style of writing, hence you see a lot of chaining in one single line. Its cleaner to see when you keep them short.
Another case is when sometimes you need to declare a dummy variable which has no relevance/meaning in that scope. In such cases I use
_
which makes it clear for me. This comes handy especially during method overloading, for eg,