Improving coding style into classes

**Class and Interface declarations should be organized in the following manner: ** 1. Class/Interface documentation. 2. class or interface statement. 3. Class (static) variables in the order public, protected, package (no access modifier), private. 4. Instance variables in the order public, protected, package (no access modifier), private. 5. Constructors. 6. Methods (no specific order). Reduce complexity by making the location of each class element predictable. Imported classes should always be listed explicitly. [Read More]

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. [Read More]

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. [Read More]

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. [Read More]

Java and CPP - the differences and similarities

This list of similarities and differences is based heavily on The Java Language Environment, A White Paper by James Gosling and Henry McGilton http://java.sun.com/doc/language_environment/ and the soon-to-be published book, Thinking in Java by Bruce Eckel, http://www.EckelObjects.com/. At least these were the correct URLs at one point in time. Be aware, however, that the web is a dynamic environment and the URLs may change in the future. Java does not support typedefs, defines, or a preprocessor. [Read More]

Bubble sort

The sorting problem Input: Array of numbers , unsorted. Eg. Output : Same numbers sorted in some order, say increasing order. Eg. What is Bubble Sort? The bubble sort works by comparing each item in the list with the item next to it, and swapping them if required. The algorithm repeats this process until it makes a pass all the way through the list without swapping any items (in other words, all items are in the correct order). [Read More]

Stack implementation using linked list

We will be understanding the stack implementation using linked list. So, please understand the link list before proceeding. Lets understand how we can implement the different operation using linked list. CPP implementation Here is how we use linked list to implement stack in cpp: #include <iostream> using namespace std; struct node { int info; struct node \*next; }; struct node \*top; int empty() { return((top == NULL)? 1:0); } void push(int n) { struct node \*p; p=new node; if(p! [Read More]