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?
Don't shorten names unless you really need to. It really only makes sense if the variable name is already super long. For example, con is a popular abbreviation for a network connection, but it could also mean a drawback (pro vs con), or the end of an array .
Having really long variable names will muddle up your code's readability, so you want to avoid those. But on the other end of the spectrum, you want to avoid ultra-short names or acronyms like "n" or "ne." Short, cryptic names like these will cause someone trying to read your code to tear their hair out.
A variable name must start with a letter or an underscore character (_) A variable name cannot start with a digit. A variable name can only contain alpha-numeric characters and underscores ( a-z, A-Z , 0-9 , and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)
Variable name may not start with a digit or underscore, and may not end with an underscore. Double underscores are not permitted in variable name.
The way I usualy do it depends on the collection you're enumerating over. If the name of the collection implies what type the lambda parameter will be, then I just go with the single letter, however if the collection isn't as descriptive, then I'll use a word.
IE:
myCollection.Where(person =>....); //non descriptive collection name myPeopleCollection.Where(p=>...); // descriptive collection name
I try to use single-word but meaningful names. So I would tend to use "person" rather than "p" but wouldn't go for "newlyAddedPerson".
This goes for query expressions as well - I may well violate this in quick throwaway examples, but I don't generally like:
from p in source where p.Age > 10 select p.Name;
I'd far rather see
from person in source where person.Age > 10 select person.Name;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With