在 Java 中选择方法/参数名称
我应该如何选择在 Java(或任何其他语言)中使用哪些方法名称和参数名称/类型?为什么有这么多的方法可供选择?
如果很多人从事一个项目,那么在代码的不同部分看到不同的命名方式是很烦人的,特别是当人们对其用法不一致时。为什么该语言的发明者不简单地声明一个标准——“这是方法和参数应该命名的确切方式。句号。”?
那样的话编程就容易多了。每次编写方法时,我都必须在无数不同的参数/方法名称之间进行选择,并试图在代码中做出选择,这让我感到压力很大。
示例 1:
public void handleFile(String file)
public void handleFile(String fileName)
public void handleFile(File file)
示例 2:
public void exec(String command)
public void exec(String cmd)
public void exec(String commandString)
public void exec(String cmdString)
public void execute(String command)
public void execute(Command command)
public void execute(Command cmdObj)
等等 等等
How should I choose which method names and parameter names/types to use in Java (or any other language for that matter)? And why are there so many ways to choose?
If many people work on a project, it's annoying to see different ways of naming in different parts of the code, especially if people are inconsistent about their usage. Why didn't the inventors of the language simply state a standard -- "This is the EXACT way methods and parameters should be named. Period."?
It would be easier to do programming then. I get stressed by having to choose between a gazillion different parameter/method names each time I write a method and trying to be consequent about how I choose throuhgout the code.
Example 1:
public void handleFile(String file)
public void handleFile(String fileName)
public void handleFile(File file)
Example 2:
public void exec(String command)
public void exec(String cmd)
public void exec(String commandString)
public void exec(String cmdString)
public void execute(String command)
public void execute(Command command)
public void execute(Command cmdObj)
etc. etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您正在与一群人一起工作,那么建立编码标准可能是值得的。谷歌一下这个短语,你会找到例子。
If you're working with a group of people, it might be worth your while to establish a coding standard. Google this phrase and you'll find examples.
事实上,Java 语言存在命名约定:请参阅 http://www.oracle .com/technetwork/java/codeconventions-135099.html
参数,如变量,应采用驼峰式命名法,通常不应缩写,除非是众所周知的首字母缩略词(URL、HTTP 等),最重要的是,应该描述参数的角色而不是其类型。它的类型是已知的:它写在参数之前。
因此,更喜欢
关于
您的示例,以下是我会接受的示例:
两者都可以接受,因为第一个处理名称,第二个处理文件。
In fact, naming conventions exist for the Java language: See http://www.oracle.com/technetwork/java/codeconventions-135099.html
Arguments, like variable, should be written in camelCase, should generally not be abbreviated except for well-known acronyms (URL, HTTP, etc.), and, most important, should describe the role of the argument and not its type. Its type is knwon: it's written just before the argument.
So prefer
to
Regarding your examples, here are the ones I would accept:
Both are acceptable, because the first one handles a name and the second one handles a file.
参数名称向使用它的开发人员提示应该传递什么内容。它们应该尽可能清晰,而不是不必要的冗长。
我建议你做你认为更清楚的事情。
The parameter names are a hint to the developer using it as to what should be passed. They should be as clear as possible without being needlessly verbose.
I suggest you do what you think is clearer.
问题是——这并不重要。方法名称更重要,参数名称应该简单描述参数。尝试执行有关名称的规则将很困难 - 您无法为此制定足够的规则。
另一方面,参数类型很重要 - 您应该选择最具体的参数类型。如果您只需要一个文件名,则传递一个字符串。如果您需要知道是否可以读取文件,请传递
File
。The thing is - it doesn't matter. The method name is more important, the argument names should simply describe the argument. Trying to enforce rules about names will be hard - you can't formulate sufficient rules for that.
The argument type on the other hand is important - you should choose the most specific argument type. If you need simply a filename, then pass a String. If yon need to know whether you can read a file, pass
File
.一般来说,首选是 camelcase 中参数 Class 的名称:
除非是 String/primitive或盒装,您赋予它含义并通常删除类型:
但有时它只是“字符串” - 这里有一些常见的选择
无论哪种风格,您的团队都应该同意命名约定并使用它。
Generally, the first choice is the name of the parameter Class in camelcase:
Except when a String/primitive or boxed, where you give meaning to it and usually drop the type:
But sometimes it is just "a string" - here are some common choices
Whatever the style, your team should agree to a naming convention and use it.
传统上,人们受到屏幕上可以容纳的字符数量的限制,因此首选短变量名称。
如今有了大屏幕、自动换行和自动完成功能,您可以随意命名内容。
这完全取决于您的团队如何决定最好的方法,并且您应该执行大家都同意的编码标准。
就我个人而言,我尝试命名变量以准确描述它们所持有的内容,因此它持有一个
TableViewController
我将称之为tableViewController
这样当我再次查看代码时就很清楚了在一天、几周的时间里,我无需仔细检查就可以使用TableViewController
。如果我将变量tvc
称为缩写或其他缩写,您可能会认为它可能不太清楚我的代码的意图是什么。Well traditionally people were constrained to the amount of chars they could fit on a screen so short variables names were preferred.
Now days with big screen, automatic line wrapping and autocompletion you can name things as long as you want.
It all depends on how you team decides is the best way and you should enforce a coding standard that you all agree on.
Personally I try to name variables to accurately describe what they are holding therefore is it holds a
TableViewController
I will call ittableViewController
that way it is clear when I look at the code again in a day, weeks... time that I am using aTableViewController
without having to double check. If I calle the variabletvc
the or other of shortenings you can think of it may not be as clear what the intent of my code is.