Should I use the Java naming convention?

荒凉一梦 提交于 2021-02-05 05:56:05

问题


I'm assigned to a large project that is going to be done in Java and viewed by a few programmers on the team. I want the code to be readable and understandable, and so want the naming convention to suite everyone that reads it.

I'm wondering if I have to use the Java naming convention in order for other people to view it and understand it naturally. I always use a type of my own naming convention that doesn't differ much from the Java naming convention but inherits from the C/C++ naming convention.

The biggest difference is the variable, class and method naming. I use names like intLargestNumber, chGet, strName for variables. funType, funErase, funAdd, getThis, setThis for methods and Screen, clsPlay, cls_User for classes. Where Java naming convention would use something like myNumber for a variable, lowSplit() for a method and Image for a class.

My question is, should I use Java naming convention or use my own naming convention and provide a lexicon or a log for my code?


回答1:


Using the standard for the language will make it simpler to bring new people on board. Also, using an externally defined convention can help mute the level of religious warfare that these sorts of style issues often provoke.




回答2:


Use the Java naming convention. It's almost always a mistake to try to apply conventions from one language to another. The fact is that most people won't spend time reading up on the conventions used for a project. Therefore it's always easier to use the established norms of the language.




回答3:


Personally, I'm against your naming convention. It's got a name: Hungarian notation. I think embedding type in variable names is an awful idea. If I were on your team, I'd vote against it.




回答4:


I'm assigned to a large project that is going to be done in Java and viewed by a few programmers on the team.

How about you ask your team lead?

In general, you should have a good reason not to follow the conventions everyone else expects to see.




回答5:


Most Java programs make heavy use of methods and classes in the standard Java Class Libraries and in third party libraries. With very few exceptions, these conform to the recommendations in the Java Style Guide. If you mandate a significantly different conventions for names, your code will consist of an ugly mixture of styles. Any possible (theoretical) advantages of your style will be lost in the mental dissonance of the style inconsistency.




回答6:


As Steve Gilham and Emil H said, yes, you should definitely stick to Java conventions when you write Java code. (The same obviously goes for any language or platform.)

As to why that should be so important, I recommend that you check out the classic Java book, Effective Java, by Joshua Bloch. In its foreword (available online here!), Guy Steele provides a nice, concise argument for writing code that is idiomatic for the given language. The book also contains a section specifically about this (item 56 in 2nd edition): Adhere to generally accepted naming conventions. That item is only a couple of pages long, and you may already know most of what is says, but, more importantly, the book as a whole is the best possible guide for writing clear and maintainable Java code.

You mention it's a large project done by a team of programmers, which makes the choice even clearer. Even if all the current programmers happened to be happy with your C/C++ like naming pattern, remember that it's very plausible that the composition of your team changes at some point. A newly recruited Java programmer will certainly feel more at home when confronted with a codebase of idiomatic Java that adheres to common conventions of the language.




回答7:


It shouldn't be your decision. It's a decision that should be made by the team, or imposed upon the team.




回答8:


Using the standard naming conventions will reduce the ramp-up for new hires and others migrating to the project. You also will not need to document your individual style for the benefit of the group, saving the time which would be invariably spend describing what the fields mean and the reason for not using the standard. Another benefit is that you can use features provided by many IDEs for simple code generation with little customization - a process that would be required by all the developers on your project.




回答9:


I've always been of the opinion that it's better to be consistently wrong than it is to be inconsistently right. If you're starting a new project and are wanting to decide on a naming convention, by all means use the standard.

If you've already got a project, then it's a bit more difficult. If that's the case, you're just going to have to decide whether it's worth it to change your naming convention over to the Java standard or to keep it the way it is. It's always a pain to deal with code that isn't consistent in their naming, so I'd advise against a piecemeal approach if you can avoid it.




回答10:


If you code in Java yes.



来源:https://stackoverflow.com/questions/1183407/should-i-use-the-java-naming-convention

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