命名包含文件名的变量?

发布于 2024-07-10 06:14:22 字数 126 浏览 4 评论 0原文

如果我有一个包含文件完全限定名称(例如项目文件)的变量,它应该被称为 projectFileprojectFileName项目路径? 或者是其他东西?

If I've got a variable that contains the fully-qualified name of a file (for example, a project file), should it be called projectFile, projectFileName or projectPath? Or something else?

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

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

发布评论

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

评论(9

绝情姑娘 2024-07-17 06:14:23

这取决于您的环境中使用的命名约定,例如在 Python 中,由于 os.path 模块。

>>> import os.path
>>> path = "/path/to/tmp.txt"
>>> os.path.abspath(path)
'c:\\path\\to\\tmp.txt'
>>> os.path.split(path)
('/path/to', 'tmp.txt')
>>> os.path.dirname(path)
'/path/to'
>>> os.path.basename(path)
'tmp.txt'
>>> os.path.splitext(_)
('tmp', '.txt')

It depends on naming conventions used in your environment e.g. in Python the answer would be projectpath due to naming conventions of os.path module.

>>> import os.path
>>> path = "/path/to/tmp.txt"
>>> os.path.abspath(path)
'c:\\path\\to\\tmp.txt'
>>> os.path.split(path)
('/path/to', 'tmp.txt')
>>> os.path.dirname(path)
'/path/to'
>>> os.path.basename(path)
'tmp.txt'
>>> os.path.splitext(_)
('tmp', '.txt')
ま柒月 2024-07-17 06:14:22

我通常使用这些:

  • FileName 仅用于文件名(不带路径)
  • FilePath 仅用于父路径(不带文件名)
  • FileFullName对于带有路径的完全限定名称,

我认为不存在公认的标准。 我取决于您(团队)的偏好以及您是否需要在给定情况下区分这三者。

编辑:我对这些特定命名约定的想法是:

  • 直观上,“名称”是一个字符串,“路径”(和“文件名”)也是一个字符串,
  • “名称”是相对的,除非它是“全名”
  • 相关变量名称应该以相同的前缀(“文件”+...)开头,我认为这提高了可读性
  • 具体/属性是右分支:“文件”-> “FileName”
  • 特化是左分支:“FileName”-> “ProjectFileName”(或“ProjectFileFullName”)
  • “File”是代表物理对象的对象/句柄,因此“ProjectFile”不能是字符串

我不能总是坚持这些约定,但我尝试这样做。 如果我决定使用特定的命名模式,即使这意味着我必须编写更具描述性(=更长)的变量名称,我也会保持一致。 阅读代码的次数多于编写代码的次数,因此额外的少量输入并不会太困扰我。

I usually go with these:

  • FileName for the file name only (without path)
  • FilePath for the parent path only (without the file name)
  • FileFullName for the fully qualified name with path

I don't think there is such a thing as an accepted standard for this. I depends on your (team's) preferences and whether you need to differentiate between the three in a given situation.

EDIT: My thoughts on these particular naming conventions are these:

  • intuitively, a "Name" is a string, so is a "Path" (and a "FileName")
  • a "Name" is relative unless it is a "FullName"
  • related variable names should begin with the same prefix ("File" + ...), I think this improves readability
  • concretions/properties are right-branching: "File" -> "FileName"
  • specializations are left-branching: "FileName" -> "ProjectFileName" (or "ProjectFileFullName")
  • a "File" is an object/handle representing a physical object, so "ProjectFile" cannot be a string

I cannot always stick to these conventions, but I try to. If I decided to use a particular naming pattern I am consistent even if it means that I have to write more descriptive (= longer) variable names. Code is more often read than written, so the little extra typing doesn't bother me too much.

随遇而安 2024-07-17 06:14:22

System.IO.Path 似乎将文件的完全限定名称称为 path,将文件本身的名称称为 filename,并将其包含的目录称为 >目录。 我建议在您的情况下,如果您在 System.IO.Path 的上下文中使用,则 projectPath 最符合此命名法。 然后,我将文件的名称称为 fileName,并将其包含的目录称为 parentDirectory

System.IO.Path seems to refer to the fully qualified name of the file as the path, the name of the file itself as filename, and its containing directory as directory. I would suggest that in your case projectPath is most in keeping with this nomenclature if you are using in the context of System.IO.Path. I would then refer to the name of the file as fileName and it's containing directory as parentDirectory.

心不设防 2024-07-17 06:14:22

我认为对此没有达成共识,只是尽量保持一致。

.NET Framework 中的示例:

  • FileStream(string path...);

  • Assembly.LoadFrom(string assemblyFile)

  • XmlDocument.Load(string filename)

甚至文件名的大小写 (filename / fileName) 在框架中不一致,eg:

  • FileInfo.CopyTo(string destFileName)

I don't think there's a consensus on this, just try to be consistent.

Examples from the .NET Framework:

  • FileStream(string path...);

  • Assembly.LoadFrom(string assemblyFile)

  • XmlDocument.Load(string filename)

Even the casing of filename (filename / fileName) is inconsistent in the framework, e.g.:

  • FileInfo.CopyTo(string destFileName)
天冷不及心凉 2024-07-17 06:14:22

您应该在它可能包含的文件之后为其命名,即:

system_configuration_file_URI
user_input_file_URI
document_template_file_URI

“文件”或“文件名”否则大多无用

此外,“文件”可能意味着“我是一个文件点”,这是不明确的,“文件名”则不然说明它是否具有上下文(即:目录),fileURI 是上下文明确的,因为它表示“这是一个资源标识符,当观察时,它指向一个资源(文件)”

You should title it after the file it is likely to contain, ie:

system_configuration_file_URI
user_input_file_URI
document_template_file_URI

"file" or "filename" is otherwise mostly useless

Also, "file" could mean "I am a file point" which is ambiguous, "filename" doesn't state whether or not it has context ( ie: directories ), fileURI is contextwise unambiguous as it says "this is a resource identifier that when observed, points to a resource ( a file ) "

话少情深 2024-07-17 06:14:22

一些想法:

  • projectFile 可能不是一个字符串——它可能是一个表示文件解析内容的对象。
  • projectFileName 可能不是完全限定的。 如果文件实际上是“D:\Projects\MyFile.csproj”,则可能会包含“MyFile.csproj”
  • projectPath 可能被视为文件的完全限定路径,也可能被视为包含该文件的父文件夹的名称。
  • projectFolder 可能被视为保存父文件夹的名称,或者它实际上可能是代码中某种 Folder 抽象的实现。

.NET 有时使用path 来引用文件,有时使用filename

Some thoughts:

  • projectFile might not be a string -- it might be an object that represents the parsed contents of the file.
  • projectFileName might not be fully-qualified. If the file is actually "D:\Projects\MyFile.csproj", then this might be expected to contain "MyFile.csproj".
  • projectPath might be considered to be the fully-qualified path to the file, or it might be considered to be the name of the parent folder containing the file.
  • projectFolder might be considered to hold the name of the parent folder, or it might actually be an implementation of some kind of Folder abstraction in the code.

.NET sometimes uses path to refer to a file, and sometimes uses filename.

智商已欠费 2024-07-17 06:14:22

请注意,“文件名”在英语中是一个单词! 标识符中间的“n”不需要大写。

也就是说,我将 Filename 附加到包含文件名的所有字符串变量中。 但是,我尝试避免整个场景,转而在支持文件和目录类型的语言中使用强类型变量。 毕竟,这就是可扩展类型系统的用途。

在强类型语言中,通常不需要描述性后缀(尤其是在函数参数中),因为变量类型和用法可以推断其内容。

Notice that “filename” is one word in English! There's no need to capitalize the “n” in the middle of the identifier.

That said, I append Filename to all my string variables that contain filenames. However, I try to avoid this whole scenario in favour of using strongly typed variables in languages that support a type of files and directories. After all, this is what extensible type systems are there for.

In strongly typed languages, the need for the descriptive postfix is then often unnecessary (especially in function arguments) because variable type and usage infers its content.

忆伤 2024-07-17 06:14:22

所有这些都部分取决于方法的大小以及变量是否是类变量。

如果它们是类变量或在大型复杂方法中,请遵循 Kent Fredric 的建议,并将它们命名为指示文件用途的名称,即“projectFileName”。

如果这是一个小型实用程序方法,例如删除文件,则不要将其命名为“projectFileName”。 那么就简单地称其为“文件名”。

永远不会将其命名为“路径”,因为这意味着它指的是它所在的文件夹

将其称为“文件”就可以了,如果< /b> 没有其他变量,例如“fileID”或“filePtr”。

因此,我将使用“文件夹”或“路径”来标识文件所在的目录。

并使用“fileID”来表示文件对象。

最后,“文件名”是文件的实际名称。

快乐编码,
兰迪

All of this depends partly on the size of the method and if the variables are class variables.

If they are class variables or in a large complicated method, then follow Kent Fredric's advice and name them something that indicates what the file is used for, i.e. "projectFileName".

If this is a small utility method that, say, deletes a file, then don't name it "projectFileName". Call it simply "filename" then.

I would never name it "path", since that implies that it's referring to the folder that it's in.

Calling it "file" would be OK, if there were not also another variable, like "fileID" or "filePtr".

So, I would use "folder" or "path" to identify the directory that the file is in.

And "fileID" to represent a file object.

And finaly, "filename" for the actual name of the file.

Happy Coding,
Randy

梦里泪两行 2024-07-17 06:14:22

根据上述选项的含义不明确且没有广泛接受的名称的答案,如果这是请求文件位置的 .NET 方法,则在 XML 注释中指定您想要的内容以及示例,如下所示如果其他开发人员不确定您想要什么,他们可以参考该内容。

Based on the above answers of the choices being ambiguous as to their meaning and there not being a widely accepted name, if this is a .NET method requesting a file location then specify in the XML comments what you want, along with an example, as another developer can refer to that if they are unsure of what you want.

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