Posted in Coding guidelines

x y z, lambda!

“x, y, z” is often used as variable names in lambda expressions, and if the expression is long and nested and you run out of “x, y, z”, then “x2, y2, z2” is used.

The x,y,z Way

In my opinion this is not very readable or easy to follow. Especially if you’re new to the code.

cars.Where(x => x.Engine.Model == "superfast" && x.Engine.Sparkplugs.All(y => y.Model == "superspark")).GroupBy(x => x.Engine.Model);

The long Way

Why does car become x and spark plugs y? It’s a car with an engine and spark plugs :) But maybe you don’t want to use long variable names because the code becomes cluttered.

cars.Where(car => car.Engine.Model == "superfast" && car.Engine.Sparkplugs.All(sparkplug => sparkplug.Model == "superspark")).GroupBy(car => car.Engine.Model);

The compromise

But you could use the first letter of the entity name or a short name like the two or three first letters if the entity name is long or used multiple times in the same expression.

cars.Where(c => c.Engine.Model == "superfast" && c.Engine.Sparkplugs.All(s => s.Model == "superspark")).GroupBy(c => c.Engine.Model);

Leave a Reply