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 Notes

Variable Names

Basic variable naming conventions

Choosing good names is probably the single most important factor in making programs readable -- or unreadable. It's worth spending some time to get good names. When I come back to a program of mine and find unhelpful names, the first refactoring I apply is to replace bad names with good names. Why would I choose bad names in the first place? When everything is fresh in your mind, you don't need the extra help of good names so its easy to choose an unhelpful name. Later, or to another reader, it's often obvious.

Sun's Code Conventions for the Java Programming Language (java.sun.com/docs/codeconv/) is the most widely used set of Java coding guidelines. Altho the organization you work for may have have different standards in some aspects, these variable naming conventions are almost always used. Here is what they have to say about variable names.

"Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

Variable names should be short yet meaningful. The choice of a variable name should be mnemonic - that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters."

Meaningful

  • Special case: Domain names. It's important to use variable names which correspond to the problem domain. For example, two-dimensional coordinates are traditionally named x and y. These short names would be acceptable because they correspond to the same names in the problem domain. Because x and y are also traditional names for algebraic variables, it might be even better to name them xCoord and yCoord.
  • Special case: Booleans. Boolean variables are often named beginning with the prefix is, for example, isClean, isOpen, etc.
  • Special case: Loop variables. The meaningless short variable names i and j are so commonly used as iteration variable names, that it is permissible to do so. Do not use i and j for other purposes.
  • Special case: n is commonly used for the number of elements to be processed.
  • Special case: Method return value. If the return value of a method is stored in a variable, the name result is commonly used.
  • Plural names are often used for arrays or collections of multiple values.

Differentiating by Lifetime (Local, Instance, and Class variables)

Hints about about a variable's lifetime can be quite useful. Sun gives no guidance on this, and as a result, there are many different styles, including the most popular -- no naming distinction.

  • Instance variables are often prefixed with one of the following:
    • m_ - "Member" is another term for instance (C++ origin).
    • _ - Underscore is sometimes used in C++ and carried over to Java. This violates Sun's standard naming conventions, but it is used sometimes. Making it a suffix puts it back into the Sun naming convention. You will find it in some of my code, as well as other conventions.
    • its
    • my
    • this. may be used to qualify instance variables. The problem with this is that it isn't enforced by the compiler. On the other hand, the advantage is that it can also easily be used with functions, whose names can't be changed when they are inherited.
  • Class - Static variables are sometimes prefixed with
    • the

Differentiating type - Hungarian notation

Obfuscation

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.

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:
  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.