Question

I am used to

for(int i = ...)
    for(int j = ...)
        for(int k = ...)

If more than 2 nested indexes, then I give some of them proper names (item, obj, parent, window, etc.).


Then I am used to events with e as event argument and exceptions:

try { ... } catch (Exception... e)

If exception is inside event, then I use ee variable.


But what about linq? I see some peoples use

blablabla.Any(i => i.Value == SomeValue);

or

bla.Where(u => u.Something == Something);

Is there any standards of naming variables inside predicates or only opinions? Or is it just me? =D

Était-ce utile?

La solution

A common practice is to use a single letter, the first letter of the type, c for customers, p for products. x is used a lot, I use x when I am typing out the lambda but will usually refactor into a descriptive name or single letter that matches the collection item.

If it is more than a simple lambda, consider using a proper descriptive variable name as always.

The end goal is to make the code readable. If you feel a simple x is readable, use x. If you feel giving a full name more readable use a full name.

Autres conseils

Let me expand on my comment above. I use common sense for some LINQ queries e.g.

bool hasExpiredItems = items.Any(item => item.IsExpired());

If I have a plural collection name then, as in this example with Any(), I use the singular item. This isn't a rule though, jsut use what you think makes sense.

EDIT:

Ooops, my example was wrong. Have updated it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top