What is correct Java naming convention for id?

2019-02-08 09:20发布

If I have user id variable, what is correct java naming convention for it?

userId or userID

8条回答
放我归山
2楼-- · 2019-02-08 09:23

This is quite a handy resource for Java naming conventions http://geosoft.no/development/javastyle.html

查看更多
甜甜的少女心
3楼-- · 2019-02-08 09:26

I consider the item described in: Effective Java by Joshua Bloch authoritative in this matter:

Adhere to generally accepted naming conventions

Class and interface names should consist of one or more words, with the first letter of each word capitalized [..] Abbreviations are to be avoided, except for acronyms and certain common abbreviations like max and min. [...] a strong argument can be made in favor of capitalizing only the first letter. [...] you can still tell where one word starts and the next word ends. Which class name would you rather see, HTTPURL or HttpUrl?

So, It should be userId

查看更多
不美不萌又怎样
4楼-- · 2019-02-08 09:26

It's like this:

  1. For acronyms, uppercase only the first letter. Definitely. Ignore the fact that some JDK methods don't.
  2. But "id" is one of the strangest words in the language -- an abbreviation for which no one even really knows exactly what it's short for. Some have suggested "identifying document", but this is likely just made up after the fact. 'identifiying... something' is about the best we have. Anyway, if you were to use userID, it suggests that a plain id would be iD, and you sure as heck don't want to do that, so the winner is:

userId

(as others have said).

So it ends up being treated like an acronym anyway.

查看更多
何必那么认真
5楼-- · 2019-02-08 09:27

This is a tough decision, even Sun isn't so sure what to do. Look at classes in JRE,

java.net.URL
java.security.acl.Acl
java.net.HttpURLConnection
java.awt.dnd.DnDConstants
java.awt.color.ICC_Profile

Personally, I prefer to use camel-case always to be consistent.

查看更多
欢心
6楼-- · 2019-02-08 09:30

It mainly depends on your naming convention together with your application (if there are any). For instance, userId is a preferred one with Java convention

查看更多
The star\"
7楼-- · 2019-02-08 09:34

The first one is preferred according to Java naming convention (1) (2).

查看更多
登录 后发表回答