Lambda变量名称-简称还是不简称? [英] Lambda variable names - to short name, or not to short name?

查看:131
本文介绍了Lambda变量名称-简称还是不简称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,当我使用lambdas时,我只使用"a,b,c,d ..."作为变量名,因为可以轻松推断类型,并且我发现短名称更易于阅读.这是一个示例:

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.

在不陷入我上面使用的愚蠢命名示例的情况下,关于Lambda变量名称的一般共识是什么?是短名还是短名?

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?

推荐答案

我通常采用的方式取决于您要枚举的集合.如果集合的名称暗示了lambda参数的类型,那么我只使用单个字母,但是,如果集合的描述性不强,那么我将使用一个单词.

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

这篇关于Lambda变量名称-简称还是不简称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆