Enlightensoft's Blog

Helping in your each step

  • Categories

  • Authors

Good Practices


##########################################################################################################

Most safer way in java 1.5 onwards for Creating Singleton Class is: SingletonEnum

This way providing Singleton Class which is full proof even in Reflection and Serialization.

package j2.singletone;

public enum SingletonEnum {
INSTANCE;
private int i = 2;

public void incrementI() {
i++;
System.out.println(i);
}
}

package j2.singletone;

public class MainSingletonEnumTest {
public static void main(String[] args) {
SingletonEnum singletonEnum = SingletonEnum.INSTANCE;
singletonEnum.incrementI();
System.out.println(singletonEnum);

SingletonEnum singletonEnum2 = SingletonEnum.INSTANCE;
singletonEnum2.incrementI();
System.out.println(singletonEnum2);
}
}

Output:

3
INSTANCE
4
INSTANCE

##########################################################################################################

Prefer for-each loops to traditional for loops

The for-each loop provides compelling advantages over the traditional for loop in clarity and bug prevention, with no performance penalty. You should use it wherever you can. Unfortunately, there are three common situations where you can’t use a for-each loop:

  • Filtering—If you need to traverse a collection and remove selected elements, then you need to use an explicit iterator so that you can call its remove method.
  • Transforming—If you need to traverse a list or array and replace some or all of the values of its elements, then you need the list iterator or array index in order to set the value of an element.
  • Parallel iteration—If you need to traverse multiple collections in parallel, then you need explicit control over the iterator or index variable, so that all iterators or index variables can be advanced in lockstep

##########################################################################################################

Minimize the scope of local variables

  • The most powerful technique for minimizing the scope of a local variable is to declare it where it is first used.
  • Prefer for loops to while loops – Reduce local variable scope (reducing the cut and paste error for multiple loop in same scope) and it is providing initialization section that is called only once for a loop by that it is reducing each time declaration of local variables cost.
  • Keep methods small and focused.

##########################################################################################################

Know and use the libraries

  • By using a standard library, you take advantage of the knowledge of the experts who wrote it and the experience of those who used it before you.
  • You don’t have to waste your time writing ad hoc solutions to problems that are only marginally related to your work.
  • Their performance tends to improve over time, with no effort on your part.

##########################################################################################################


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: