参数命名:文件名还是文件名?

发布于 2024-07-17 03:11:44 字数 339 浏览 6 评论 0原文

我尝试在命名时保持语法正确*。 我一直使用 filename 而不是 fileName。 java 约定似乎也使用此,但 FxCop 更喜欢 fileName

维基百科上有关于它的讨论。 我读得越多,我就越觉得我是对的(这很常见!:))。 有谁有明确的答案还是这只是主观的?

* 我只是希望这篇文章没有语法错误!

I try to be grammatically correct in my naming*. I've always used filename instead of fileName. The java convention also seems to use this, but FxCop prefers fileName.

There's a discussion on WikiPedia about it. The more I read, the more I feel I'm right (which is quite usual! :) ). Does anyone have a definitive answer or is this merely something subjective?

* I just hope there are no grammar errors in this post!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

笔落惊风雨 2024-07-24 03:11:44

建议字段和参数使用小驼峰式

示例1

fileName // for fields, parameters, etc.
FileName // for properties, class names, etc.

通常使用fileName,而不是filename; 您可以通过阅读 Microsoft 创建的开源内容的源代码来验证这一点,例如 企业库

原因

  1. 这背后的要点是在这种情况下名称更具可读性。
  2. 此外,当在同一方法(类..)中使用多个参数(字段、变量..)并且具有相同前缀“文件”时,此方法还增加了一致性,如下所示
  3. :还有其他一些原因,但它们更加主观。

示例 2

fileName, fileSize... // instead of filename AND filesize

另请参阅

获取完整的命名约定集规则,我建议检查这本书:

并且还可以在 IDesign.net

Lower camel case is recommended for fields and parameters.

Example 1:

fileName // for fields, parameters, etc.
FileName // for properties, class names, etc.

Generally, fileName is used and NOT filename; you can verify that by reading source code of open source stuff created by Microsoft, such as Enterprise Library.

Reasons:

  1. The main point behind this is that names are more readable in this case.
  2. Also this approach adds consistency when several parameters (fields, variables..) are used in the same method (class..) and the with same prefix "file", as demonstrated below:
  3. ...there are a few other reasons, but they are more subjective.

Example 2:

fileName, fileSize... // instead of filename AND filesize

See also:

For a full set of naming convention rules, I recommend checking this book:

And also check some stuff at IDesign.net

云胡 2024-07-24 03:11:44

'filename' 假定该词描述单个对象,例如“牛”或“椅子”
'fileName' 假设这是一个复杂的对象,有一个名为 file 的对象,并且该对象描述了该文件的名称。

两种哲学方法,任君选择。

'filename' assumes that this word describes a singular object like 'cow' or 'chair'
'fileName' assumes that this is a complex object, that there is an object called file and that this object describes the name of that file.

Two philosophical approaches, take your pick.

不打扰别人 2024-07-24 03:11:44

英文写“文件名”或“文件名”是可以接受的。 当您将其转换为编码时,大写“n”或不大写“n”可以采用任何一种方式(假设camelCase或PascalCase)。

顺便说一句,你在问题中确实犯了语法错误——讽刺的是,就在你表达希望不存在语法错误的句子中。 你说:“我只是希望这篇文章没有语法错误!” 但“errors”是复数,因此“there's”的“is”代表主谓不一致。

* 我只是希望这篇文章没有语法错误!

It is acceptable English to write "filename" or "file name". When you translate that into coding, capitalizing the "n" or not capitalizing the "n" can go either way (assuming camelCase or PascalCase).

By the way, you did make a grammatical error in the question--ironically, in the very sentence in which you were expressing your hope that there were no grammatical errors. You said, "I just hope there's no grammar errors in this post!" But "errors" is plural, therefore the "is" of "there's" represents a subject-verb disagreement.

* I just hope there are no grammatical errors in this post!

碍人泪离人颜 2024-07-24 03:11:44

就我而言,

thisIsMuchMoreReadable
阅读本文

As far as I am concerned,

thisIsMuchMoreReadable than
readingthis.

折戟 2024-07-24 03:11:44

我认为这里的答案跨越两个问题。

  • “文件名”与“文件名”(应该
    “名字”是一个单独的词)

  • 'fileName' 与 'FileName' (应该首先
    字符应小写)。

在大多数情况下,我更喜欢将这个词视为一个完整的词“文件名”。
我还喜欢以小写开头变量/方法,以便更轻松地完成代码完成菜单导航。

我想驼峰大小写的问题也在这里,我认为应该使用它来区分多单词名称。

I think the answers here are spanning two issues.

  • 'FileName' vs 'Filename' (should
    'name' be a separate word)

    and

  • 'fileName' vs 'FileName' (should first
    character be lower case).

In most cases, I prefer to treat this word as a single whole word 'filename'.
I also prefer starting variables/methods with lower case for easier code completion menu navigation.

I guess the issue of camel case is here too which I think should be used to distinguish multi-word names.

南烟 2024-07-24 03:11:44

FxCop 是一个自动化工具,这不是显而易见的答案吗? 它认识到“name”是一个单词,因此建议以大写的 N 开头。我们碰巧知道“filename”也是一个单词,因此只有第一个 F 应该大写。

Isn't the obvious answer that FxCop is an automated tool? It recognizes that "name" is a word, so it suggests starting it with a capital N. We happen to know that "filename" is also a word, and so only the first F should be capitalized.

孤蝉 2024-07-24 03:11:44

这里不可能有真正的对或错。

这是纯粹主观的事情,与您所在的社区完全相关。如果您经常遇到的 FxCop 和 StyleCop 以及 .net 代码正在使用 fileName,则使用 fileName。 如果它使用其他东西,那么就使用它。

您的首要任务可能应该是与您自己的代码中的模式保持一致,然后与您的社区保持一致。

在这种特殊情况下,.net Reflector 使用 fileName 显示大量 .net 代码,因此我个人会采用该模式。

如果您在 java 世界中并且运行 PMD 和 checkstyle 并且它们的 api 经常使用文件名,那么我会选择这样做。

除了维基百科命名文章之外,还有 Kernighan 和 Pike 的编程实践。 其中第一章涉及很多命名和代码一致性问题。

There can be no real right or wrong here.

This is something that is purely subjective and relates completely to the community you are working in. If FxCop and StyleCop and the .net code that you regularly encounter is using fileName, then use fileName. If it is using something else, then use whatever that is.

Your first priority should probably be to be consistent to the pattern in your own code and then consistent with your community.

In this particular case, .net Reflector shows a lot of .net code using fileName so I would go with that pattern personally.

If you were in the java world and running PMD and checkstyle and their apis made frequent use of filename, then I would go with that.

In addition to the wikipedia naming article, there is also The Practice of Programming by Kernighan and Pike. The first chapter in it touches on a lot of naming and code consistency issues.

情泪▽动烟 2024-07-24 03:11:44

文件名 ~ 赋予电子存储的计算机文件的识别名称,符合操作系统施加的限制,如长度或字符选择的限制。

过去这被认为是两个单词,但现在被定义为一个单词,所以

var filename = ......

如果它是两个单词,那就是

var fileName = ....

Filename ~ an identifying name given to an electronically stored computer file, conforming to limitations imposed by the operating system, as in length or restricted choice of characters.

In the past this was considered two words but now is defined as one word so

var filename = ......

if it was two words it would be

var fileName = ....

‖放下 2024-07-24 03:11:44

如果您正在编写 c/c++,那么很可能会使用人们实际上可以读取的名称; ie filename 很好,yet_another_file_name 也很好(假设您没有将 filename 视为正确的英语单词 - 我通常这样做)。

请参阅 Google 编码标准

If you are writing c/c++ there is a strong tendency to use names that people can actually read; i.e. filename is good, and so is yet_another_file_name (assuming you are not considering filename as a proper english word - I usually do).

See google coding standards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文