Thursday, 16 September 2010

Java specific naming convention

JFC (Java Swing) variables should be suffixed by the element type.
widthScale, nameTextField, leftScrollbar, mainPanel, fileToggle, minLabel, printerDialog
Enhances readability since the name gives the user an immediate clue of the type of the variable and thereby the available resources of the object.

Array specifiers must be attached to the type not the variable.
int[] a = new int[20]; // NOT: int a[] = new int[20]
The arrayness is a feature of the base type, not the variable. It is not known why Sun allows both forms.

Java source files should have the extension .java. Point.java Enforced by the Java tools.


The import statements must follow the package statement. import statements should be sorted with the most fundamental packages first, and grouped with associated packages together and one blank line between groups. 
 import java.io.IOException; 
import java.net.URL;
import java.rmi.RmiServer; 
import java.rmi.server.Server; 
import javax.swing.JPanel; 
import javax.swing.event.ActionEvent; 
import org.linux.apache.server.SoapServer; 
The import statement location is enforced by the Java language. The sorting makes it simple to browse the list when there are many imports, and it makes it easy to determine the dependiencies of the present package The grouping reduce complexity by collapsing related information into a common unit.


The package statement must be the first statement of the file.
All files should belong to a specific package. The package statement location is enforced by the Java language. Letting all files belong to an actual (rather than the Java default) package enforces Java language object oriented programming techniques. 

Specific cases of naming enhancing naming style

The term find can be used in methods where something is looked up.
vertex.findNearestVertex(); matrix.findSmallestElement(); node.findShortestPath(Node destinationNode);
Give the reader the immediate clue that this is a simple look up method with a minimum of computations involved. Consistent use of the term enhances readability.

The term initialize can be used where an object or a concept is established.
printer.initializeFontSet();
The American initializeshould be preferred over the English initialise. Abbreviation init must be avoided.




Plural form should be used on names representing a collection of objects.
Collection points; int[] values;
Enhances readability since the name gives the user an immediate clue of the type of the variable and the operations that can be performed on its elements.
 

n prefix should be used for variables representing a number of objects.
nPoints, nLines
The notation is taken from mathematics where it is an established convention for indicating a number of objects.

Note that Sun use num prefix in the core Java packages for such variables. This is probably meant as an abbreviation of number of, but as it looks more like number it makes the variable name strange and misleading. If "number of" is the preferred phrase, numberOf prefix can be used instead of just n. num prefix must not be used.

No suffix should be used for variables representing an entity number.
tableNo, employeeNo
The notation is taken from mathematics where it is an established convention for indicating an entity number.

An elegant alternative is to prefix such variables with an i: iTable, iEmployee. This effectively makes them named iterators.

Improving coding style into functions or methods

Method modifiers should be given in the following order: static abstract synchronized final native
The modifier (if present) must be the first modifier.
public static double square(double a);
// NOT: static public double square(double a);
is one of public, protected or private while includes volatile and transient. The most important lesson here is to keep the access modifier as the first modifier. Of the possible modifiers, this is by far the most important, and it must stand out in the method declaration. For the other modifiers, the order is less important, but it make sense to have a fixed convention.
 

Some great quotes in The art of Coding

Design bugs are often subtle and occur by evolution with early assumptions being
forgotten as new features or uses are added to a system.
—Fernando J. Corbató

By relieving the brain of all unnecessary work, a good notation sets it free to
concentrate on more advanced problems, and in effect increases the mental power of
the race. Before the introduction of the Arabic notation, multiplication was difficult, and
the division even of integers called into play the highest mathematical faculties.
Probably nothing in the modern world would have more astonished a Greek
mathematician than to learn that … a huge proportion of the population of Western
Europe could perform the operation of division for the largest numbers. This fact would
have seemed to him a sheer impossibility…. Our modern power of easy reckoning with
decimal fractions is the almost miraculous result of the gradual discovery of a perfect
notation.
—Alfred North Whitehead

The Sapir-Whorf hypothesis says that your ability to think a
thought depends on knowing words capable of expressing the thought. If you don't
know the words, you can't express the thought and you might not even be able to
formulate it (Whorf 1956).

Wednesday, 15 September 2010

Unique testing

Random in java

The expression Math.random() yields a random double number in the range zero (0.0) to one (1.0) (excluding the upper limit of exactly 1.0). You can print this value using: 
double rand = Math.random();
System.out.println(""+rand);
You can convert this to an integer in the range 1 to n using the  following code where n = 10

int n = (int)(Math.random()*10)+1;
System.out.println(""+n);

What java takes by default?

There are many things like:
Decimal numbers (for example 1.3) are of type double by default. To make them of type float they must be followed by F (say, 1.3F).