views:

696

answers:

10

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?

+31  A: 

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.

Steve Gilham
+1 for striving to prevent religious warfare.
Thilo
Good you mentioned religious warfare. I could've strived!
Secko
+1: Hungarian Notation is a waste of characters.
S.Lott
Especially if you want to change the return type of a method or have a subclass where the return type for a method is different to the parent. The sort of problem Hungarian notation was designed to solve is better handled by IDEs which show you the signature of a method you are calling as you use it.
Peter Lawrey
+12  A: 

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.

Emil H
+3  A: 

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.

Thilo
@Thilo "How about you ask your team lead?" I will, on monday!
Secko
+2  A: 

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

Draemon
I'm also asking because I want to know for the sake of the future!
Secko
In that case see Steve's answer
Draemon
+8  A: 

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.

duffymo
@duffymo "I think embedding type in variable names is an awful idea" - Can you please explain why?
Secko
The definition of the variable shows what type the variable has. If it comes to more objects the type isn't that important and the class could implement more than one interface so that the type in the object name would confuse more then it helps. If you are using a good IDE the IDE should help you at every point where you need the type of the variable. The name of the variable should show for what the variable is used in a semantic way. The type is shown by the syntax of the language
Janusz
@Janusz +1 Thanks!
Secko
It breaks encapsulation, and it forces you to change all kinds of variable names if you change type.
duffymo
That C++ "convention" was invented when the best IDE you could have was emacs or vi. No meta-data to help. We've moved well beyond that. Time to let it die.
duffymo
+1: Hungarian Notation is a waste of characters in Java. At best it doesn't help. At worst, it's misleading because it's wrong.
S.Lott
Absolutely agree
abatishchev
+1  A: 

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.

akf
+3  A: 

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.

Stephen C
+2  A: 

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.

Jonik
Sorry, I know the book almost gets over-advertised on SO, in every possible Java question. But here I think it's actually very appropriate, and something that the OP really should check out, so I couldn't pass the chance to plug it. :-)
Jonik
A: 

If you code in Java yes.

Janusz
+1  A: 

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.

Jason Baker