Is there a standard in java for _ (underscore) in front of variable or class names?

天涯浪子 提交于 2019-12-12 07:07:00

问题


I have seen some programmers using _ (underscore) in front of class names, and others using it for local variables.

Does the Java standard require/suggest the use of _ (underscore) in front of an private instance variable or class name?


回答1:


It is a matter of personal taste.

Martin Fowler appears to like it. I don't care much for it - it breaks the reading pattern, and the information it conveys is already subtly told by color in your IDE.

I've experimented with using _ as the name of the variable holding the result to be returned by a method. It is very concise, but I've ended up thinking that the name result is better.

So, get your colleagues to agree on what you all like the most and then just do that.




回答2:


I think artifacts like these are pre-IDE and C++ vintage. Don't do it.




回答3:


if you want to follow best practice java , use the code convention outlined here http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

I.e., no underscore for private member variable names.

Tho, personally, i m not fussed either way, as long as consistency is applied.




回答4:


Many people (including myself) use _ in front of field names. The reason for this is to easily distinguish them from local variables. However in the ages of IDEs this is not so necessary since syntax highlighting shows this. Using an underscore in front of a class name is just wrong. By convention, class names start with an uppercase letter.




回答5:


A lot of people is it for instance variables as it makes them stand out. Personally I hate it mainly because methods should, for the most part, be no more than 10 lines of code (I hardly ever go over 20). If you cannot see that a variable isn't local in under 10 lines then there is something wrong :-)

If you have 100 line methods and you don't declare all your variables at the top of the block that they are used in I can see why you would want to use _ to distinguish them... of course, as with most things, if it hurts then stop doing it!




回答6:


Using _ before a variable helps in one case where if the developer wants to use a standard/key word as a variable name knowingly or unknowingly. It would not give a conflict if that has an additional character.




回答7:


It seems to be a convention that states if an underscore is used as an initial character in the name of method, the method cannot be overridden.

For example, in context of JSP, _jspService() method cannot be overridden.

However, i dont find any document that states above convention.




回答8:


I agree with the others here... It breaks the standard and doesn't really buy you anything if you just use a naming standard. If you need to know the difference between your class and local variables, try a naming convention like lVariableName where the l is used to indicate that it is local. To me this is unnecessary since the IDE highlighting is sufficient, but might be useful to some.




回答9:


  1. Do not use _ when you use a modern LCD color monitor, with modern IDE like Eclipse, Netbeans, IntelliJ, Android Studio.
  2. Use _ when you need to use a old-style monochrome CRT monitor with only green or white text, or when you have difficulty to identify the color on LCD monitor.
  3. The consensus of your code collaborators overrides the above.


来源:https://stackoverflow.com/questions/1899683/is-there-a-standard-in-java-for-underscore-in-front-of-variable-or-class-nam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!