• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why no underscores in field names?

 
Bryan Welch
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bryan Welch
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Javaranch! Here's the link:
http://javaranch.com/Style.jsp#intro
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bryan Welch
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know Martin Fowler (Refactoring . . .) always begins his variable names with an underscore.
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the link to Sun's Java Coding Conventions
Code Conventions for the Java Programming Language
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic