*
The moose likes Java in General and the fly likes Why no underscores in field names? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why no underscores in field names?" Watch "Why no underscores in field names?" New topic
Author

Why no underscores in field names?

Bryan Welch
Ranch Hand

Joined: Jan 13, 2000
Posts: 32
I saw a rule in the style guide saying not to use underscores in java field names. Why? I'm hearing a lot of local opposition to making that a rule in the style guide.

----<br />Bryan Welch<br />bwelch42@yahoo.com
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
I saw a rule in the style guide saying not to use underscores..........
Is it like in your work place or something?
Just curious where you saw this style guide
regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Bryan Welch
Ranch Hand

Joined: Jan 13, 2000
Posts: 32
In Javaranch! Here's the link:
http://javaranch.com/Style.jsp#intro
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Bryan,
I do not know for sure, but it is probably related to the way that Sun usually formats their code. I don't have a link to the Sun Official Java Style Sheet, but in all Java code developed by Sun it is customary to see most variables in this format ( someJavaIdentifier ) in which the first word is lower case and any following words in the variable start with an upper-case letter. On the other hand constant ( final ) variables are in the form ( CONSTANT_JAVA_IDENTIFIER ) all upper-case with underscores used to seperate the words. I believe this is used so that when a variable is seen in a program it is easier for the person reading the code to tell if an identifier is a constant. Since underscores are not needed to seperate words in a non-constant variable name using underscores simply lengthens the variable name.
Anyway, just my $0.02...
-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Bryan Welch
Ranch Hand

Joined: Jan 13, 2000
Posts: 32
Thanks, Nate. That does make sense, and I think that's what I'll use in our style sheet. Any comments from other readers? The more comments I get, the better this document will end up becoming.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I know Martin Fowler (Refactoring . . .) always begins his variable names with an underscore.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
I think for style, the most important thing is consistent, and easy for maintenance. The better for future programmers, the better for the company. Because almost no programmer works for one company until retires.
Written standard is much better than oral aggreement, even it is a bad one. It is very hard for programmers from different background to agree with the same standard. However, we have to learn how to live with the standard we hate. That is a good quality of a good programmer IMHO.
Thanks!
Roseanne
Join our SCJD Study Group when certified
[This message has been edited by Roseanne Zhang (edited October 20, 2001).]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
"I know Martin Fowler (Refactoring . . .) always begins his variable names with an underscore."
I know that was a standard from some long time ago C programmers, and do not remember when and where anymore. However, if you're hired by Mr. Martin Fowler, you have to learn this standard, and follow it like religion, even you might hate it.
That is a good example for my above post.
Roseanne
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

IMHO, this is an opinion from the legacy programmers - no offence
meant to anyone. Once when I was programming in a training class
I was using underscores for class variables and my instructor
stopped by and said "be carefull with that, sometimes the compiler doesn't like it".
However, till now I haven't seen any probs with that.
Moreso, IMO, a style guide is a convention that is followed for a
specific task/project/company and not a language std. nor a
universally accepted policy.
$0.02.........
regds.
- satya
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
"Moreso, IMO, a style guide is a convention that is followed for a specific task/project/company and not a language std. nor a
universally accepted policy."
Agreed!
Sometimes, it is a culture thing, even "religion". I'm not a Microsoft fan, and know exactly what Microsoft did for Java, which made my working company then abandoned Java. That was why there is a big gap on my resume about my Java experiences.
However, Microsoft uses Hungarian notation (invented by a Hungarian mathematician, not Microsoft!), which is an excellent coding convention, it can save you huge amount of maintenance time. However, since some people do not like Microsoft, therefore laugh at Hugarian notation too. When I came back to Java, I've to abandon it to avoid hurting somebody's feelings. Yes, it is true!
Roseanne

[This message has been edited by Roseanne Zhang (edited January 30, 2001).]
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

The underscore is of course a big C/C++ convention. I typically see it used to separate words in an identifier (where Java Software recommends mixed case), or to precede an identifier to suggest local scope or "private" implementation.
I think the break with this convention is based on two ideas. One, mixed case follows SmallTalk in typography to look familiar to OO programmers. Two, we have a private scope that is below the level of the source code file itself, and we have method overloading, so the initial underscore isn't needed to avoid name collision.
Nonetheless, the initial underscore in an identifier was left in to allow C/C++ coders to maintain conventions they preferred.
A one-off reason for avoiding the initial underscore has to do with native methods. To write a C header and stub for the entry code into a native method, JNI "mangles" the method name by prepending an underscore to the identifier. The same process occurs, more or less, when a $ is placed between an outer and inner class in the inner class' compiled filename. So, avoiding use of these two characters arguably clarifies the purpose of the code.
For what it's worth,
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited January 30, 2001).]


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
In case you haven't already done so, Roseanne, it's probably worth doing a search here for "Hungarian Notation". There's a lot of intelligent discussion of it, but the result tends towards agreeing that Hungarian Notation can be useful in systems with a small number of carefully distingiushed types, but can become a great burden in a true OO system with lots of types. In a system which makes a lot of use of multiple inheritance or interfaces, it can become a nightmare.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
How about a system with close to one hundred of components, probably million lines of code, used multi-inheritance for sure in VC++. And we used Hungarian notation successfully. I was one developer of the team.
Actually the more complicated the system, the more powerful the Hungarian notation is, the more time you save for future maintenance, provided you know how to use it properly.
Roseanne
[This message has been edited by Roseanne Zhang (edited January 30, 2001).]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
This is the kind of thing which goes emotional easily, especially when it goes a little conflict to some "religion".
I'm not a Microsoft defence lawyer in a JavaRanch. Do not say "send her back to Microsoft, or VC++". I'm the one who easily follows any standard as long as the boss or the team decided so.
However, we have to learn how to live with the standard we hate. That is a good quality of a good programmer IMHO.( Quatation from Roseanne's post above)

Roseanne
[This message has been edited by Roseanne Zhang (edited January 30, 2001).]
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Here's an online paper on Hungarian notation written by Charles Simonyi and prefaced by someone named Dr. GUI. Wonder what his Mom gets for her birthday?
I think Simonyi's assumptions in the text speak for themselves. For example, he implies that mixed case is a common C convention. A pervasive C convention for code style? Excuse me? And there's a grudging admission that the scheme falls into exactly the same trap as pointer declaration in C. Is a pfc a pointer to an 'fc' or is it a pfc type? You don't know until you read the code. Ok, thanks very much.
My little digs at it aside, I think Hungarian notation has limited appeal in a Java environment that puts any emphasis on design. It's defined to view what we call "messages" as concrete types. We don't have to decode obscure pointer declarations to start with: type names are already clear-text. That and javadoc covers a lot of ground.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited January 31, 2001).]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
"javadoc covers a lot of ground."
Excellent point, agreed absolutely. C++ does not have this excellent tool, which makes Hungarian Notation more attractive.
When we were using Hungarian Notation, we did not use any abbreviations except the primitive types and Strings. Nobody needs to re-invent any type abbreviations, which solved Frank's worry about many type problems. This policy made the variable name very long, which had a lot of resistance at the beginning, but everybody saw the benefit of it in the process and enjoyed it a lot later, since no confusion out there anymore.
Very strict component/class naming policy saved a lot develop time too. However, with no comparison with Java, we develop Java a lot faster, at least twice faster than in C++.
Roseanne
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
I know that underscores are sometimes difficult to see, could that be the reason why the style guide says no underscores ??
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Paul Wheaton:
Once you get into the habit that character case is significant in names, it is an easy step to use character case as a "word separator" in multi-word names. The old C habit of using underscores can lead to long names if you try to make them expressive, and just ignoring spaces can lead to words that read very strangely.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
FWIW, I tend to avoid underscored variable names, because IDEs sometimes generate them, and it can lead to conflict. No reason to temp fate.
M
[ July 18, 2002: Message edited by: Max Habibi ]

Java Regular Expressions
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
I think the reason Java programmers are recommended not to use underscores or hungarian notation is the same and very simple: your code becomes unreadable. The point is that you lose something when you drop undescores and the type prefixes, but you gain much more because your code is much more readable without all this nonsense.
Eugene.
Dan Cranmer
Greenhorn

Joined: May 24, 2000
Posts: 24
Here is the link to Sun's Java Coding Conventions
Code Conventions for the Java Programming Language
 
Don't get me started about those stupid light bulbs.
 
subject: Why no underscores in field names?
 
Similar Threads
What's wrong with my hashCode() ?
Extracting data from a JSON file/URL/Object/String in Java
Interface Concept
static insights?
Can form element name/id have spaces?