开发者

What is the correct way to refer to Java members in text? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 9 months ago.

Improve this question

In answering questions I find myself referring to method names and online documentation often. I'm confused about how method names should be referenced in text.

For example I often type:

One should use String.equals() for comp开发者_开发技巧aring two strings for equality.

However, this is a little misleading:

  1. It makes equals() appear to be a static member.
  2. It makes equals() appear to not take any arguments.

In the interest of compleness, I would like to know:

What is the correct way to refer to both static members and instance members?

I've seen things like:

  • String.equals()
  • String#equals()
  • myString.equals()

Is there a way to refer to methods in an argument-agnostic manner?

For example, in C foo(void) is explicitly a zero-argument function and foo() can be redefined later to have a different set of arguments. (?)


If you want to be really be exact you could use

java.lang.String#equals(java.lang.Object)

or the shorter form

String#equals(Object)

This is similar to the notation used in javadoc and interestingly also looks similar to the link to the documentation which ends with:

String.html#equals(java.lang.Object)

Edit: Here is a link to the javadoc documentation describing how to use references.


I've found that a link to the documentation for String's equals method removes the ambiguity.


Using myString.equals() makes no sense unless you state very clear, that myString is an instance of String. javadoc uses String#equals() so that should be readable and understandable by most developers. This would be my choice.


String.equals.

If you wanted to give argument types String.equals(Object).

Adding the parentheses (equals()) is a C thing. In particularly an ANSI C thing, in which a no-args function would be written as tostring(void) (I'm actually too young(!) to remember K&R/PCC C). C++ fixed that - so the () are really quite old school.


I generally reference the class and method name, with the expectation that the reader can use that to track down the documentation. Perhaps that's a little arrogant. I don't know.

If I'm trying to make a point about which signature of a method to use, I'll usually provide sample code. This is mainly because I find it easier than writing out the description long-hand.


myString.equals() is perfect. It implies the type and makes it clear that we are talking about an instance.

You could even add:

myString.equals(aString)


When referring to a method, I use "String.equals()". This is for human readers, and they likely to know what you are talking about from the context. In case it could become confusing, just say "method String.equals()"

One should use String.equals() for comparing two strings for equality.

Seriously, who would ever be confused by this sentence? So don't worry. It's just English language, there is no compiler yelling at you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜