LINQ 查询:SQL 风格的缩写还是拼写?
Microsoft 非常清楚 .NET“标识符”或“参数”不应包含缩写。直接从马嘴里说出来:
为了避免混淆并保证跨语言互操作,请遵循以下有关缩写词使用的规则:
- 请勿使用缩写或缩写作为标识符名称的一部分。例如,使用 GetWindow 而不是 GetWin。
- 请勿使用计算领域不普遍接受的首字母缩略词。
- 在适当的情况下,使用众所周知的首字母缩略词来替换冗长的短语名称。例如,使用 UI 作为用户界面,使用 OLAP 进行在线分析处理。
- 使用首字母缩略词时,对于长度超过两个字符的首字母缩略词,请使用帕斯卡大小写或驼峰大小写。例如,使用 HtmlButton 或 htmlButton。但是,仅由两个字符组成的首字母缩写词应大写,例如 System.IO 而不是 System.Io。
- 请勿在标识符或参数名称中使用缩写。如果必须使用缩写,请对包含两个以上字符的缩写使用驼峰式大小写,即使这与单词的标准缩写相矛盾。
- http://msdn.microsoft.com/en -us/library/141e06ef%28VS.71%29.aspx
但是,如果您查看 MSDN 自己的 101 LINQ 示例页面,您会发现大量这样的示例...
var productNames =
from p in products
select p.ProductName;
...并且很少(没有?)像这样...
var productNames =
from product in products
select product.ProductName
...但是在其他 MSDN 页面上,您会找到说明问题的示例(例如 http: //msdn.microsoft.com/en-us/library/bb384065.aspx)。
讨论
我对此百感交集。有了智能感知,把所有的事情都说清楚并不困难,但我也不确定它有多大优势。它的可读性更高还是更少?你有点用冗长来换取清晰。 SQL 程序员似乎能够很好地使用表名的简短缩写,但话又说回来,程序员也曾经使用匈牙利表示法生存,而且这已被充分证明是一个坏主意。
那么,有几个问题:
- 是否有关于 LINQ 查询是否应该缩写的官方指导?
- 您的特别偏好是什么?为什么?
Microsoft is pretty clear that .NET "identifiers" or "parameters" should not contain abbreviations. Straight from the horse's mouth:
To avoid confusion and guarantee cross-language interoperation, follow these rules regarding the use of abbreviations:
- Do not use abbreviations or contractions as parts of identifier names. For example, use GetWindow instead of GetWin.
- Do not use acronyms that are not generally accepted in the computing field.
- Where appropriate, use well-known acronyms to replace lengthy phrase names. For example, use UI for User Interface and OLAP for On-line Analytical Processing.
- When using acronyms, use Pascal case or camel case for acronyms more than two characters long. For example, use HtmlButton or htmlButton. However, you should capitalize acronyms that consist of only two characters, such as System.IO instead of System.Io.
- Do not use abbreviations in identifiers or parameter names. If you must use abbreviations, use camel case for abbreviations that consist of more than two characters, even if this contradicts the standard abbreviation of the word.
- http://msdn.microsoft.com/en-us/library/141e06ef%28VS.71%29.aspx
Yet, if you take a look at MSDN's own 101 LINQ Samples page, you will find tons of examples like this...
var productNames =
from p in products
select p.ProductName;
...and very few (none?) like this...
var productNames =
from product in products
select product.ProductName
...but on other MSDN pages, you'll find examples that spell things out (e.g., http://msdn.microsoft.com/en-us/library/bb384065.aspx).
Discussion
I have mixed feelings about this. With intellisense, it's not exactly a hardship to spell everything out, but I'm not sure there's much advantage in it either. Is it more readable or less? You're sort of trading off wordiness for clarity. SQL programmers seem to get along well enough with short abbreviations for table names, but then again, programmers used to survive using Hungarian notation, too, and that's been pretty well proven to be a bad idea.
So, a couple questions:
- Is there any official guidance on whether LINQ queries should be abbreviated or not?
- What is your particular preference and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想您可能会说范围变量实际上并不遵循与其他标识符相同的规则,因为它的范围完全限于查询表达式。 “p”的含义更加明显,因为您永远不会在
from p in products
的上下文之外查看它。我的偏好 - 也许我不应该承认这一点 - 就是使用“p”。老实说,我根本不认为描述性地命名只能在一个语句中使用的东西有什么意义。
想象一下您正在编写一个 lambda 表达式。你会写:
或者
我实际上发现第一个版本更可读,但这也许只是个人喜好。
I suppose you could say that a range variable doesn't really follow the same rules as other identifiers, since its scope is entirely limited to the query expression. It's a bit more obvious what "p" means because you're never looking at it outside the context of
from p in products
.My preference - and perhaps I shouldn't be admitting this - is just to use "p". To be honest, I simply don't see the point in descriptively naming something that can only ever be used in one statement.
Imagine you're writing a lambda expression instead. Would you write:
or
I actually find the first version more readable, but maybe that's just personal preference.
我认为它更类似于 for 循环中的索引变量。虽然我通常会避免使用单字母变量名称,但在这种情况下以及 LINQ 查询(或 lambda 表达式)中的临时占位符的情况下,我对此表示同意。我喜欢简洁。它将它与局部范围的变量区分开来。由于我比 LINQ 语法更倾向于使用扩展方法,因此我在 lambda 中经常使用单个变量名称,并且更喜欢在那里使用它。当此类变量的数量较少时,阅读起来当然并不困难,而且可能会更容易。不过,如果更长的变量名更容易理解,我也不反对使用它。
I think it's much more akin to index variables in a for-loop. While I would normally avoid single letter variable names, in that context and in the context of a temporary placeholder in a LINQ query (or lambda expression), I'm ok with it. I like the brevity. It sets it apart from locally scoped variables. Since I'm more apt to use extension methods than LINQ syntax, I use single variable names a lot in lambdas and prefer it there. It's certainly no harder to read and may be easier when the number of such variables is small. If a longer variable name makes it more understandable, though, I'm not against using that either.
如果少于 10 个字符左右,我会使用整个单词。我还使用@符号作为范围内/范围外的视觉提示。
I use the whole word if its less than 10 or so characters. I also use the @symbol to as a in/out of scope visual cue.