For ... each中的命名约定 [英] Naming conventions in For... each
问题描述
大家好,
如果我有一个名为Foo的类,是否有一个首选的命名约定
迭代Foos集合中的每个Foo实例?我已经看过
几种不同的版本,即使在MSDN文档中:
#1:
对于Foo中的每个Foo来自Foo
...
下一个Foo
这种变化是直接的,但有点含糊不清,因为它使用相同的名称
用于Foo类和Foo实例。 (我知道Next
Foo中的Foo是可选的 - 我只是为了清晰起见而包含它。)
#2:
对于每个MyFoo [或aFoo,TheFoo等]作为Foo中的Foo
...
下一个MyFoo
这种变化通过在实例中添加前缀来消除歧义
名称。然而,它似乎有点过于可爱。对我来说。
#3:
每个f为Foo
...
下一个f
这似乎是C#约定,无论好坏。 Terse和
点。
我倾向于使用第一个变体,但想知道这是否使我的代码更少
可读。有没有人对这些或任何其他
变体有偏好?我不想开始一场宗教战争,但我很好奇
是否有一贯的最佳实践。
Hello all,
If I have a class called "Foo," is there a preferred naming convention for
iterating through each Foo instance in a collection of Foos? I''ve seen
several different variations, even in the MSDN documentation:
#1:
For each Foo as Foo in Foos
...
Next Foo
This variation is direct, but a bit ambiguous since it''s using the same name
for the Foo class and the Foo instance. (I know that the "Foo" in "Next
Foo" is optional -- I''m just including it for clarity.)
#2:
For each MyFoo [or aFoo, TheFoo, etc.] as Foo in Foos
...
Next MyFoo
This variation eliminates the ambiguity by adding a prefix to the instance
name. However, it seems a bit too "cutesy" for me.
#3:
For each f as Foo
...
Next f
This appears to be the C# convention, for better or worse. Terse and to the
point.
I tend to use the first variation, but wonder if this makes my code less
readable. Does anyone have a preference for any of these, or any other
variations? I don''t want to start a religious war, but am curious about
whether there''s a consistent "best practice."
推荐答案
嗨罗伯特,
关于数字1的耻辱;;-)
我会说只有''最佳实践''不要给你的变量相同
作为班级的名字。
这是我在我面前看到的Foo,
我手上的手柄?
来吧,让我抓住你。
我没有你,但我仍然看到了你。 />
因为你是班级而不是对象,
还是反过来呢?
现在我都是困惑。
其他所有。我会说,是可选的,如您所建议的那样多变,
等等。
对于像循环这样的简短代码,单个字母可以是好。我使用
''c'用于char循环,'S'用于简单的字符串操作,''''用于表单中的
小表单实用程序一般来说,范围很小,并且
变量使用了很多。
对于一般的对象,我使用oDis和oDat。 (是因为我是Oi''m Oirish,
也许不是)。这是一个有历史的习惯,而且它是一个非常强大的习惯 - 但是我在
在.NET中重新评估它。它确实具有使用相同名称
作为课程的优势。
我绝对< hate>我的一切 - 现在<那是>很可爱。
但偶尔我会使用TheWidget或ThisWidget。
问候,
Fergus
Hi Robert,
Shame about number 1. ;-)
I would say the only ''best practice'' is don''t give your variables the same
name as the class.
Is this a Foo which I see before me,
The handle toward my hand?
Come, let me clutch thee.
I have thee not, and yet I see thee still.
For you are the class not the object,
Or is it the other way round?
And now I''m all confused.
Everything else. I would say, is optional and as varied as you suggest,
and more.
For short pieces of code like a loop, the single letter can be good. I use
''c'' for char loops, ''S'' for simple string manipulation, ''F'' for a Form in a
small Form utility routine, etc. Generally the scope will be small and the
variable used a lot.
For objects in general I use oDis and oDat. (is dat ''cos Oi''m Oirish,
maybe not). It''s a habit with history and it''s a pretty strong one - but I''m
re-evaluatiing it in .NET. It does have the advantage of using the same name
as the class.
I absolutely <hate> My anything - now <that''s> cutesy.
But occasionally I use TheWidget or ThisWidget.
Regards,
Fergus
*" Robert Jacobson" < RJ ********************** @ nospam.com> scripsit:
* "Robert Jacobson" <rj**********************@nospam.com> scripsit:
如果我有一个名为Foo的类,是否有一个首选的命名约定来遍历Foos集合中的每个Foo实例?我已经看到了几种不同的版本,即使在MSDN文档中:
#1:
对于Foo中的Foo来说每个Foo
...
下一个Foo
这种变化是直接的,但有点含糊不清,因为它为Foo类和Foo实例使用相同的名称。 (我知道Next
Foo中的Foo是可选的 - 我只是为了清楚而包含它。)
我认为'这是一个非常糟糕的惯例。
#2:
对于每个MyFoo [或aFoo,TheFoo等]作为Foo中的Foo
......
下一个MyFoo
这种变化通过在实例名称中添加前缀来消除歧义。然而,它似乎有点过于可爱。为了我。
似乎很好对我来说。
#3:
每个f如Foo
...
下一个f
这似乎是C#惯例,无论好坏。 Terse和
点。
If I have a class called "Foo," is there a preferred naming convention for
iterating through each Foo instance in a collection of Foos? I''ve seen
several different variations, even in the MSDN documentation:
#1:
For each Foo as Foo in Foos
...
Next Foo
This variation is direct, but a bit ambiguous since it''s using the same name
for the Foo class and the Foo instance. (I know that the "Foo" in "Next
Foo" is optional -- I''m just including it for clarity.)
I think that''s a really bad convention.
#2:
For each MyFoo [or aFoo, TheFoo, etc.] as Foo in Foos
...
Next MyFoo
This variation eliminates the ambiguity by adding a prefix to the instance
name. However, it seems a bit too "cutesy" for me.
Seems "nice" to me.
#3:
For each f as Foo
...
Next f
This appears to be the C# convention, for better or worse. Terse and to the
point.
快速解决方案。
-
Herfried K. Wagner
MVP·VB Classic,VB.NET
< http://www.mvps.org/dotnet>
The "quick" solution.
--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
哈哈!你需要写一本关于VB的艺术和诗歌的书。现在,如果只有你,那么
可以拿出bawdy VB limericks。 有一个VB程序员来自
Nantucket ......
" Fergus Cooney" <音响****** @ tesco.net>在消息中写道
新闻:eo ************** @ TK2MSFTNGP10.phx.gbl ...
[ Snip]
LOL! You need to write a book on the Art and Poetry of VB. Now if only you
could come up with bawdy VB limericks. "There was a VB programmer from
Nantucket..."
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:eo**************@TK2MSFTNGP10.phx.gbl...
[Snip]
这是我在我面前看到的Foo,
我手上的手柄?
来吧,让我抓住你。
我没有,然而我仍然看到你了。
因为你是班级而不是对象,或者反过来呢?
现在我很困惑。
Is this a Foo which I see before me,
The handle toward my hand?
Come, let me clutch thee.
I have thee not, and yet I see thee still.
For you are the class not the object,
Or is it the other way round?
And now I''m all confused.
这篇关于For ... each中的命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!