Tuesday, July 24, 2012

Java programing Guidlines

History



Java is a programming language created by James Gosling from Sun Microsystems in 1991. The first public available version of Java (Java 1.0) was released 1995. Over time several version of Java were released which enhanced the language and its libraries. The current version of Java is Java 1.6 also known as Java 6.0.

From the Java programming language the Java platform evolved. The Java platform allows that code is written in other languages then the Java programming language and still runs on the Java virtual machine.

Characteristics of Java



The target of Java is to write a program once and then run this program on multiple operating systems.

Java has the following properties:

· Platform independent: Java programs use the Java virtual machine as abstraction and do not access the operating system directly. This makes Java programs highly portable. A Java program which is standard complaint and follows certain rules can run unmodified all several platforms, e.g. Windows or Linux.

· Object-orientated programming language: Except the primitive data types, all elements in Java are objects.

· Strongly-typed programming language: Java is strongly-typed, e.g. the types of the used variables must be pre-defined and conversion to other objects is relatively strict, e.g. must be done in most cases by the programmer.

· Interpreted and compiled language: Java source code is transfered into byte-code which does not depend on the target platform. This byte-code will be interpreted by the Java Virtual machine (JVM). The JVM contains a so called Hotspot-Compiler which translates critical byte-code into native code.

· Automatic memory management: Java manages the memory allocation and de-allocation for creating new objects. The program does not have direct access to the memory. The so-called garbage collector deletes automatically object to which no active pointer exists.


The Java syntax is similar to C++. Java is case sensitive, e.g. the variables myValue and myvalue will be treated as different variables.

Development with Java



The programmer writes Java source code in an text editor which supports plain text. Normally the programmer uses an IDE (integrated development environment) for programming. An IDE support the programmer in the task of writing code, e.g. it provides auto-formatting of the source code, highlighting of the important keywords, etc.

At some point the programmer (or the IDE) calls the Java compiler (javac). The Java compiler creates platform independent code which is called bytecode. This byte-code is stored in ".class" files.

Bytecode can be executed by the Java runtime environment. The Java runtime environment (JRE) is a program which knows how to run the bytecode on the operating system. The JRE translates the bytecode into native code and executes it, e.g. the native code for Linux is different then the native code for Windows.

By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with -d

Classpath



The classpath is the connection between the Java compiler and Java interpreter. It defines where the compiler and interpreter look for .class files to load.

The classpath in Java defines which Java class are available for your Java program. For example if you want to use an external Java library you have to add this library to your classpath to use it in your your program.

1.     Overview




The Java programming language consists out of a Java compiler, the Java virtual machine, and the Java class libraries. The Java virtual machine (JVM) is a software implementation of a computer that executes programs like a real machine.

The Java compiler translates Java coding into so-called byte-code. The Java virtual machine interprets this byte-code and runs the program.

The Java virtual machine is written specifically for a specific operating system.

The Java runtime environment (JRE) consists of the JVM and the Java class libraries.

1.1     Purpose




This document describes a collection of standards, for development of programs in Java. The application of these standards will increase the readability and maintainability.


1.2     Reference Documents



The following have been used as reference for the development of this document:




Sl.No.

Document Name


1.

TCS Java Coding Standards and Guidelines


2.

Java Code Conventions from Sun Microsystems


3.

Thinking in Java - Bruce Eckel


4.

Netscape’s software coding standards guide for Java


5.

How to write doc comments for javadoc -http://java.sun.com/products/jdk/javadoc/writingdoccomments.html












2.     Java Coding rules




Rules are those coding standards that are "necessary and required" coding practices that have been agreed upon by members of the team. Everyone is expected to follow these rules.
2.1     Naming Conventions

2.1.1     Package Names


·         Package names should be all lower case.

·         Package names should occur on the first non-commented line of the source.
·         Package names should normally be 3 levels deep, however a 4th level should be used, if needed, to logically group related packages under a category.  Keep default package level security in mind when designing packages.
First level Example: aig or aiu
·         The second level should be a short form of the application name, tc as opposed to trade credit.
·         The third level should be the package name or a package category, brief but not cryptic.
·         The fourth level should be the package name when a third level exists.

Example:
aiu.core.util
aiu.core.security.logon
aiu.core.security.applevel
aiu.s3web.client
aiu.s3web.producer

Three levels make sense for most of these. However, the function of applevel would not have been as apparent if it were not grouped under security.  aiu.core.logon may have been clear but if we create aiu.core.secuirty.applevel then logon makes more sense to be under security also.

2.1.2     Class Names


The name of the class will be same as the source file name, except for the .java extension. This is a requirement of the Java compiler. The Java compiler will generate a .class file for each .java file.

·         Capitalize the first letter of class names and the first letter of subsequent words.  The name should not use underscore characters.
·         Use multiple words to create a meaningful name.

Example:
ConnectionPool
ProducerEntityMgr

2.1.3 Method Names



· The first letter of the method name should be in lower case and the first letter of all subsequent words should be in upper case. The name should not include underscore characters.

· Use multiple words to create a meaningful name.

· Verbs are often used as the first word.

· The get and set methods should begin with “get” or “set”, followed by the attribute. Capitalize the first letter of the attribute.

· Get methods with boolean return type should start with “is” or “has” as the prefix

· Methods that are meant only for debugging, should start with “debug”.



Example:



boolean isCheckValid();

void returnNothing();

double calculateDiscount(double stickerPrice);

double getBalance();

void setBalance(double balance);

boolean isUpdatable();

void debugSampleMethod();

2.1.4     Constants

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.

Example:
static final int MIN_WIDTH = 4;
static final int MAX_WIDTH = 999;
static final int CPU_TYPE = 1;

2.1.5     Variable Names


·         Instance variables should start with lower case and subsequent words should start with an upper case character.  There should be no underscores used.
·         Define one variable per line.  Include a // comment about its purpose.
·         Avoid Name hiding – This refers to the practice of using the same name for an attribute/variable as for one of higher scope. The most common abuse of name hiding is to name a local variable the same as an instance attribute. Name hiding should be avoided as it makes your code harder to understand and prone to bugs.   Using the same name in an argument as that of an instance attribute is acceptable, if the intention is to set the instance attribute. 

2.1.6     Argument Names


Argument names should be consistent across methods when their use is identical.  This applies to a class as well as the entire package.  For instance, don’t use “currency” in some methods and “moneyType” in others, if they are really the same.

2.1.7     Hungarian Notation


DO NOT USE Hungarian Notation for names. It is easy to check the definition of Java variables because there are no header files. Likewise variables in methods should be easy to check because methods should be short and only do one thing.  Variable types can be added to the end of variable names if they enhance understanding and sound right when spoken.  Don’t interpret this to mean that all variable names should have their type as a suffix.  “cityString” does not enhance the understanding of this variable and sounds awkward.

Example:
Button okButton;
String [] messageArray;
Vector allocatedVector;
Hashtable entityMgrHash;


2.1.8     Interface Names

·         Use prefix ‘I” for interface classes.
·         Follow all other rules for class names.

Example:
ImyInterface

2.1.9     Component Factory Names

A component factory is a public class that implements static methods for "Factory functions" or "component constructors". Factory class names should include the word "Factory".

Example:
public class WidgetFactory
{
static Button makeButton(int buttonType);
static ListBox createListBox();
}

2.2     Coding Style

2.2.1     Braces

When using braces, “{}”, put each brace on a new line, aligned with the beginning of the command, with no code before or after them.  Use braces for single line commands attached to the following control structures as well.


·         If…else
·         Do…while
·         Switch…case
·         For…loop

Example:
if (countryCode == UK)
{
drink = “tea”;
                currency = “pound”;
}

if (countryCode == US)
{
                drink = “coke”;
}









No comments:

Post a Comment

Note: Only a member of this blog may post a comment.