Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java: Names

Names should be meaningful and follow conventions

Source code is read by the compiler, which doesn't care about your writing style, and people, who care enormously about the readability of your code. To be readable, the names should be meaningful and they should follow the Java conventions for capitalization and use of the underscore.

Meaningful names

Variables, methods, classes, etc must be given names that mean something to the reader (temperature, velocity, ...). Avoid names like a, b, and c unless they are truly without special meaning.

I worked with someone who claimed he couldn't think of meaningful variable names, so used v1, v2, v3, ... He was quite smart, but his programs were a nightmare to read. Fortunately, he soon moved into management.

Sadly, I must admit that some of my own old programs have mysterious names like nbrt, which must have been an abbreviation for something. Using abbreviations isn't necessarily bad, but they must either be commonly understood or documented in comments where the variable is declared.

Single use. Do not use a variable for more than one purpose. Local variables are very cheap so you should always declare a new variable, rather than reuse one for more than one purpose.

Language rules

Identifiers, which is what names are usually called in programming languages, must obey the rules of the particular programming language you are using.

Java identifiers start with a letter or underscore and may be followed by zero or more lower case or upper case letters, decimal digits, or underscore characters. Technically the dollar sign is also allowed, but should never be used because it may conflict with compiler-generated internal identifiers.

Conventions

In addition to being legal, programmers expect identifiers to follow standard conventions. These conventions often vary somewhat from language to language. The standard Java conventions are explained below. Your organization (or instructor) may have additional naming conventions.

Names consisting of multiple words

Because blanks can't be used in identifiers, multi-word names are handled by using camel case in Java, or separating the words with underscores in some other languages (eg Ruby).

Camel case marks the start of a new word by writing the first letter in upper case.

   double dailyAverage;
   int    maximumNumberOfSunnyDays;

A popular alternative to camel case in some languages is to separate the words with an underscore. Altho underscores are a good convention, they won't be accepted in Java programs, with one exception as noted below.

Sun's conventions for Java identifiers

Use the following conventions, which are almost universally followed in Java programs.

  • Class names (and interface names) start with a upper case letter and are continued in camel case.
    Examples: String, JOptionPane, DecimalFormat.
  • Variable and method names begin with a lower case letter and are continued in camel case.
    Examples: dailyAverage, maximumNumberOfSunnyDays
  • Constant names should be entirely in upper case, with multiple words separated by underscores.
    Examples: EXIT_ON_CLOSE, MILES_PER_KILOMETER.

Review questions

The following are all accepted by the compiler without error, but they all should be changed. How?

  1. public class celsiustofahrenheit {
  2. int maximum_temperature;
  3. static double MilesPerKilometer = 0.6;
  4. private double compute(double n) {return n * 0.000144;}

References

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.