Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Java Code Conventions

 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quoted from http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367
"The names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscores ("_"). (ANSI constants should be avoided, for ease of debugging.) static final int MIN_WIDTH = 4;"
What about local final variables ?
What is an ANSI constant ?
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For local finals no. The point is that class-wide constants or static final should be all upper case with underscores for spaces. ANSI stands for American National Standards Institue and in this case deals with the ANSI character set which is ASCII codes 128-255.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also I saw in familiar java standards,
they defined methods with underscore("_") for ex: _doSomething().

Why didn't they follow these java coding rules??
ne one can clarify me??
Regards,
Jelda
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by R Jelda:
Also I saw in familiar java standards,
they defined methods with underscore("_") for ex: _doSomething().


Experienced C++ programmers have a habit of intentionally obfuscating identifiers (I think C++ has namespace problems, or it did in the mid-90's) and they carry those practices with them as they move to Java. In Java we prefer to use access modifiers (public, private, protected, "friendly") to control the visability of our class members rather than making our code unreadable.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joe Ess:

Experienced C++ programmers have a habit of intentionally obfuscating identifiers (I think C++ has namespace problems, or it did in the mid-90's) and they carry those practices with them as they move to Java. In Java we prefer to use access modifiers (public, private, protected, "friendly") to control the visability of our class members rather than making our code unreadable.


I think C++ programmers use underscores to help distinguish between class variables and local variables. Since C++ classes are often declared in a header file and defined in an implementation file, its more tedious to check if a variable has class scope or not. If you think that underscores are ugly, take a look at Hungarian notation a la Microsoft Foundation Classes.
Regards,
Arthur
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C++ has public, protected, and private scopes too. The underscores are generally holdovers from early versions of c (not C++) where you would sometimes have to put some identifiers in the global scope but wanted to avoid name collisions. This was particularly an issue when writing a library where your 'internal globals', so to speak, really shouldn't have been visible to the user of the library, but there wasn't any good way to hide them.
In C++, there are only a few cases where you might have the same situation - for example, if you're mixing in assembly language - but if you see it a lot, it might just be bad C++ programmers who saw better programmers using underscores, didn't understand why they were used but thought it looked neat, and continued the practice.
 
R Jelda
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you misunderstood my question.
And I am sorry that I was not so clear questioning.
The thing is, still I see in many familar java standards (ex:that come from JCP) are having underscores("_") in their naming.
suppose take JCA specification, they still follow underscores in their specificaton and even implementations also..If they have already provided some naming rules - whats the meaning in this asking others to follow and ignoring to follow themselves.
Regards,
Jelda
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by R Jelda:
suppose take JCA specification, they still follow underscores in their specificaton and even implementations also..If they have already provided some naming rules - whats the meaning in this asking others to follow and ignoring to follow themselves.


Art thou Holier than the Pope?
The Java Tutorial from Sun doesn't follow the Java coding conventions as put forward by Sun...
You'd expect at least their own tutorial to strictly follow their own coding conventions even if none of their other code does, but it doesn't.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by R Jelda:
whats the meaning in this asking others to follow and ignoring to follow themselves.


Code conventions aren't some sort of law. Just because one uses underscores in their variable names doesn't mean they aren't writing Java code. Those JCP submissions come from all over the industry. Some of those people are C/C++ folks who use those languages and conventions every day. I don't so my code looks closer to the standard. Just because something has been submitted to the JCP doesn't mean it's endorsed by Sun or the Java community. Heck, even if a JCP proposal been accepted I don't think it's an endorsement of the way the code is written but rather the ideas presented in the document and associated code are accepted.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic