命名包含文件名的变量?
如果我有一个包含文件完全限定名称(例如项目文件)的变量,它应该被称为 projectFile
、projectFileName
或 项目路径
? 或者是其他东西?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这取决于您的环境中使用的命名约定,例如在 Python 中,由于 os.path 模块。
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.我通常使用这些:
FileName
仅用于文件名(不带路径)FilePath
仅用于父路径(不带文件名)FileFullName
对于带有路径的完全限定名称,我认为不存在公认的标准。 我取决于您(团队)的偏好以及您是否需要在给定情况下区分这三者。
编辑:我对这些特定命名约定的想法是:
我不能总是坚持这些约定,但我尝试这样做。 如果我决定使用特定的命名模式,即使这意味着我必须编写更具描述性(=更长)的变量名称,我也会保持一致。 阅读代码的次数多于编写代码的次数,因此额外的少量输入并不会太困扰我。
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 pathI 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:
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.
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 asfilename
, and its containing directory asdirectory
. I would suggest that in your caseprojectPath
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 asfileName
and it's containing directory asparentDirectory
.我认为对此没有达成共识,只是尽量保持一致。
.NET Framework 中的示例:
FileStream(string path...);
Assembly.LoadFrom(string assemblyFile)
XmlDocument.Load(string filename)
甚至文件名的大小写 (filename / fileName) 在框架中不一致,eg:
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.:
您应该在它可能包含的文件之后为其命名,即:
“文件”或“文件名”否则大多无用
此外,“文件”可能意味着“我是一个文件点”,这是不明确的,“文件名”则不然说明它是否具有上下文(即:目录),fileURI 是上下文明确的,因为它表示“这是一个资源标识符,当观察时,它指向一个资源(文件)”
You should title it after the file it is likely to contain, ie:
"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 ) "
一些想法:
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 ofFolder
abstraction in the code..NET sometimes uses
path
to refer to a file, and sometimes usesfilename
.请注意,“文件名”在英语中是一个单词! 标识符中间的“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.
所有这些都部分取决于方法的大小以及变量是否是类变量。
如果它们是类变量或在大型复杂方法中,请遵循 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
根据上述选项的含义不明确且没有广泛接受的名称的答案,如果这是请求文件位置的 .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.