View RSS Feed


Compilation of better code advice

Rate this Entry
by , 12-27-2011 at 11:14 PM (1860 Views)
Writing Better Java Code
This is a compilation of advice on writing more efficient Java code. Whenever I find more I'll add them to the list. If you spot something fishy do say so, so that I will correct it.

Faster code:
1. Use integer instead of long (long is a 64bit integer, most processors are 32bit so operations can run 2-4 times faster with an int)
2. Use local variables instead of class members
3. Use shorthand when evaluation booleans
Java Code:
return ( boolean_expression );
4. Dividing positive numbers by 2 can be done with a bit-shift operation:
Java Code:
halfnumber = number >> 1;
5. If you use the same expression twice or more, use a local temp variable for it (make 1 call).
6. Loop backwards if possible rather than forwards (this is to avoid the extra variable on the stack: array.length), if you have to loop forwards then assign the length/size to a local variable.
Java Code:
int size = list.size();
for (int i=0; i <size; i++) {
for (int i=list.size() - 1; i >= 0; i--) {
7. Avoid the String constructor
Java Code:
String string = "Some string";
8. Mark classes as final if you know you will never need to extend them
9. If you don't need to access an object's fields, make your method static.
10. From within a class, call fields directly rather than calling getters or setters.

Less garbage collection:
1. Avoid calling System.gc() explicitly.
2. When extracting a String from data, try to return a substring rather than creating a copy, the substring will create a new String object but it will share the char[] data.
3. An array of int is much better than an array of Integer.
4. In your internal code (not shared with others) it is better to use arrays in parallel rather than to implement a container which holds a sequence of tuples.
5. Convert numbers to Strings with String.valueOf(number) rather than string =""+number. The latter creates an instance of StringBuilder.

Better design:
1. Better to use instanceof to check if a cast succeeded
Java Code:
if (obj instanceof MyObj) {
    (MyObj) obj.doSomething();
} else {
2. Better to use the Model-View implementation when creating a GUI application, it will help you to manage your code and update it in a much less painful manner. Use an IDE strictly for saving time in designing a form/frame.
- You will need 4 classes; the Model class (for domain knowledge), the Observer class (fetch data from Model and relay to View), the Listener class (fetch data from View and relay to Model), and the View class (Information-display knowledge only).
- The Observer class and the Listener class can be private classes within the View (GUI) class, but if they grow too large they can be separated.
- The Model class should extend Observable, and two methods setChanged() and notifyObservers() should be called in any setter methods which you believe needs to update the View.
- The Observer class should implement the Observer interface and override the public void update(Observable observable, Object object) method to make it update your View.
- Each time you create a new object of the model class, you should use the obj.addObserver(new myObserver()); method.
- Add Listeners to any Swing component you wish to listen on, or otherwise add appropriate Window or Mouse listeners, define listener methods dictated by the listener interface, the information should fetch data from the View (GUI) and use setters from the Model to set the new value to the Model object.
- Don't forget to use appropriate validation before putting data into your setters.
3. Implement a custom Interface in your View class to make sure you conform to all required methods and fields, but also to make it easier for others to create a GUI for your application (by implementing the same interface).
fatabass likes this.

Submit "Compilation of better code advice" to Facebook Submit "Compilation of better code advice" to Digg Submit "Compilation of better code advice" to Submit "Compilation of better code advice" to StumbleUpon Submit "Compilation of better code advice" to Google

Updated 12-27-2011 at 11:26 PM by ozzyman

Tags: None Add / Edit Tags