在 Java 中选择方法/参数名称

发布于 2024-11-19 06:41:56 字数 704 浏览 2 评论 0原文

我应该如何选择在 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 技术交流群。

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

发布评论

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

评论(6

夜血缘 2024-11-26 06:41:56

如果您正在与一群人一起工作,那么建立编码标准可能是值得的。谷歌一下这个短语,你会找到例子。

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.

八巷 2024-11-26 06:41:56

事实上,Java 语言存在命名约定:请参阅 http://www.oracle .com/technetwork/java/codeconventions-135099.html

参数,如变量,应采用驼峰式命名法,通常不应缩写,除非是众所周知的首字母缩略词(URL、HTTP 等),最重要的是,应该描述参数的角色而不是其类型。它的类型是已知的:它写在参数之前。

因此,更喜欢

void copy(SomeClass source, SomeClass destination)

关于

void copy(SomeClass someClass1, SomeClass someClass2)

您的示例,以下是我会接受的示例:

public void handleFile(String fileName)
public void handleFile(File file)

两者都可以接受,因为第一个处理名称,第二个处理文件。

public void execute(String command)
public void execute(Command command)

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

void copy(SomeClass source, SomeClass destination)

to

void copy(SomeClass someClass1, SomeClass someClass2)

Regarding your examples, here are the ones I would accept:

public void handleFile(String fileName)
public void handleFile(File file)

Both are acceptable, because the first one handles a name and the second one handles a file.

public void execute(String command)
public void execute(Command command)
凑诗 2024-11-26 06:41:56

参数名称向使用它的开发人员提示应该传递什么内容。它们应该尽可能清晰,而不是不必要的冗长。

我建议你做你认为更清楚的事情。

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.

澉约 2024-11-26 06:41:56

问题是——这并不重要。方法名称更重要,参数名称应该简单描述参数。尝试执行有关名称的规则将很困难 - 您无法为此制定足够的规则。

另一方面,参数类型很重要 - 您应该选择最具体的参数类型。如果您只需要一个文件名,则传递一个字符串。如果您需要知道是否可以读取文件,请传递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.

老娘不死你永远是小三 2024-11-26 06:41:56

一般来说,首选是 camelcase 中参数 Class 的名称:

void someMethod(SomeClass someClass)

除非是 String/primitive或盒装,您赋予它含义并通常删除类型:

void someMethod(String filename) // note filename is so common, it's rarely "fileName"

但有时它只是“字符串” - 这里有一些常见的选择

void trim(String aString)
void trim(String string)
void trim(String s)

无论哪种风格,您的团队都应该同意命名约定并使用它。

Generally, the first choice is the name of the parameter Class in camelcase:

void someMethod(SomeClass someClass)

Except when a String/primitive or boxed, where you give meaning to it and usually drop the type:

void someMethod(String filename) // note filename is so common, it's rarely "fileName"

But sometimes it is just "a string" - here are some common choices

void trim(String aString)
void trim(String string)
void trim(String s)

Whatever the style, your team should agree to a naming convention and use it.

夏雨凉 2024-11-26 06:41:56

传统上,人们受到屏幕上可以容纳的字符数量的限制,因此首选短变量名称。

如今有了大屏幕、自动换行和自动完成功能,您可以随意命名内容。

这完全取决于您的团队如何决定最好的方法,并且您应该执行大家都同意的编码标准。

就我个人而言,我尝试命名变量以准确描述它们所持有的内容,因此它持有一个 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 it tableViewController that way it is clear when I look at the code again in a day, weeks... time that I am using a TableViewController without having to double check. If I calle the variable tvc the or other of shortenings you can think of it may not be as clear what the intent of my code is.

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