Reduce number of exception creations for performace reasons
Example:
public class SampleViolation {
public static final int TEN = 10;
public void sample() throws Exception {
if(true)
{
throw new Exception("1");
}
if(true)
{
throw new Exception("2");
}
if(true)
{
throw new Exception("3");
}
if(true)
{
throw new Exception("4");
}
if(true)
{
throw new Exception("5");
}
if(true)
{
throw new Exception("6");
}
if(true)
{
throw new Exception("7");
}
if(true)
{
throw new Exception("8");
}
if(true)
{
throw new Exception("9");
}
if(true)
{
throw new Exception("10");
}
if(true)
{
throw new Exception("11");// VIOLATION, it is the 11th exception.
}
}
}
Should be written as:
public class SampleCorrection {
public static final int TEN = 10;
public void sample() throws Exception {
if(true)
{
throw new Exception("1");
}
if(true)
{
throw new Exception("2");
}
if(true)
{
throw new Exception("3");
}
if(true)
{
throw new Exception("4");
}
if(true)
{
throw new Exception("5");
}
if(true)
{
throw new Exception("6");
}
if(true)
{
throw new Exception("7");
}
if(true)
{
throw new Exception("8");
}
if(true)
{
throw new Exception("9");
}
if(true)
{
throw new Exception("10");
}
// CORRECTION.
}
}
Ensure efficient iteration over map entries
Using a keyset to iterate over a map, and then requesting values for each key is inefficient.
Example:
public class SampleViolation {
public void Sample(Map map)
{
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object value = map.get(key); // VIOLATION
}
}
}
Should be written as:
public class SampleCorrection {
public void sample(Map map)
{
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
Object key = entry.getKey();
Object value = entry.getValue(); // CORRECTION
}
}
}
Ensure efficient removal of map entries
Searching a keyset/entryset to fetch the key to remove the element is inefficient.
Example:
public class SampleViolation {
public void sample(HashMap collection) {
Set keySet = collection.keySet();
Iterator keyIter = keySet.iterator();
while (keyIter.hasNext()) {
Object key = keyIter.next();
collection.remove(key); // VIOLATION
}
}
}
OR
another case when we iterate on entry set:
public class SampleViolation {
public void sample(HashMap collection) {
Set entrySet = collection.entrySet();
Iterator entriesIter = entrySet.iterator();
while (entriesIter.hasNext()) {
Map.Entry) entry = (Map.Entry)entriesIter.next();
Object key = entry.getKey();
collection.remove(key); // VIOLATION
}
}
}
Should be written as:
public class SampleCorrection {
public void sample(HashMap collection) {
Set keySet = collection.keySet();
Iterator keyIter = keySet.iterator();
while (keyIter.hasNext()) {
keyIter.remove(); // CORRECTION
}
}
}
Always reuse immutable constant objects for better memory utilization
Creation of constant immutable objects that are not assigned to static final variables lead to unnecessary memory consumption.
Example:
public class SampleViolation {
protected Object[] getObjects() {
return new Object[0]; // VIOLATION
}
public static Integer sample(String s) {
if (s == null || s.length() == 0) {
return new Integer(-1); // VIOLATION
} else {
return new Integer(s);
}
}
}
Should be written as:
public class SampleCorrection {
public static final Object[] NO_OBJECTS = new Object[0];
protected Object[] getObjects() {
return NO_OBJECTS; // CORRECTION
}
private static final Integer INT_ = new Integer(-1);
public static Integer sample(String s) {
if (s == null || s.length() == 0) {
return INT_; // CORRECTION
} else {
return new Integer(s);
}
}
}
Avoid instantiation for getClass
Do not create instances just to call getClass on it.
Example:
package com.vimal.sample;
public class SampleViolation {
public void sample() {
Class c = (new ClassSample()).getClass(); // VIOLATION
}
}
Should be written as:
package com.vimal.sample;
public class SampleCorrection {
public void sample() {
Class c = ClassSample.class; // CORRECTION
}
}
Avoid equality with boolean
Avoid comparing a boolean with "true".
Example:
public class SampleViolation {
boolean sample(String value) {
boolean b = false;
String str = "S";
if (value.endsWith(str) == true) {// VIOLATION
b = true;
}
return b;
}
}
Should be written as:
package com.vimal.sample;
public class SampleCorrection {
boolean sample(String value) {
boolean b = false;
String str = "A";
//....
//........
if ( value.endsWith(str) ) { // CORRECTION
b = true;
}
return b;
}
}
Avoid call to Thread.sleep()
Do not call Thread.sleep() for performance reasons
Example:
public class SampleViolation {
public void sample() {
Thread.sleep(100); // VIOLATION
}
}
Should be written as:
package com.vimal.sample;
public class sampleCorrection {
public void sample() {
this.wait(100); // CORRECTION
}
}
Avoid creating double from string
Avoid creating double from string for improved performance
Example:
public class SampleViolation {
public void sample() {
Double db = new Double("3.44"); // VIOLATION
Double.valueOf("3.44"); // VIOLATION
if(db == null) {
// Do Something
db = null;
}
}
}
Should be written as:
Avoid converting String to Double
Avoid using String charAt
Use char[] representation of the string instead of using String.charAt().
Example:
package com.vimal.sample;
public class SampleViolation {
public void sample(String str) {
for(int i=0; i<str.length(); i++) {
System.out.println(str.charAt(i)); // VIOLATION
}
}
}
Should be written as:
package com.vimal.sample;
class SampleCorrection {
public void Sample(String str) {
char[] carr = str.toCharArray(); // CORRECTION
for(int i=0; i<carr.length; i++) {
System.out.println(carr[i]); // CORRECTION
}
}
}
Avoid using exponentiation
Do not use exponentiation.
Example:
package com.vimal.sample;
public class SampleViolation {
public int getPower(int iB, int iP) {
int it = (int) Math.pow(iB, iP); // VIOLATION
return it;
}
}
Should be written as:
package com.vimal.sample;
public class SampleCorrection {
public int getPower(int iB, int iP) {
int it = 1;
for (int i = 0; i < iP; i++) { // CORRECTION
it *= iB;
}
return it;
}
}
Avoid using Double toString
Avoid using Double toString for performance reasons
Example:
package com.vimal.sample;
public class SampleViolation {
public void sample(double d) {
String dStr = Double.toString(d); // VIOLATION
System.out.println(dStr);
}
}
Should be written as:
Avoid Using Double.toString() instead use custom conversion algorithm.
Avoid LinkedLists
Avoid LinkedLists instead use Vector / ArrayList as LinkedList implementation has a performance overhead for indexed access.
Example:
package com.vimal.sample;
public class ClassViolation {
public void sample() {
LinkedList list = new LinkedList();
if(list != null) {
list = null;
}
}
}
Should be written as:
package com.vimal.sample;
public class SampleCorrection {
public void sample() {
final int SIZE = 10;
ArrayList list = new ArrayList(SIZE);
if(list != null) {
list = null;
}
}
}
Declare public or protected method final
It optimizes the code and makes the code self documenting.
Example:
class SampleViolation{
public void sample() { // VIOLATION
}
}
Should be written as:
class SampleCorrection {
public final void method() { // CORRECTION
}
}
Declare private constant fields final
A private constant fields should be declared final for performance reasons
Example:
package com.vimal.sample;
class SampleViolation {
private int i = 5; // VIOLATION
public void sample() {
int j = i;
j = j + i;
}
}
Should be written as:
package com.vimal.sample;
class SampleCorrection {
private final int i = 5; // CORRECTION
public void sample() {
int j = i;
j = j + i;
}
}
Do lazy initialization
Example:
package com.vimal.sample;
public class SampleViolation {
private Classviolation instance = new Classviolation(); //Violation
}
Should be written as:
package com.vimal.sample;
public class SampleCorrection {
private ClassViolation instance;
public ClassVoliation getInstance()
{
if(doLazy == null)
instance = new ClassViolation(); // Correction
return instance;
}
}
Use entrySet instead of keySet
Use entrySet() instead of keySet().
Example:
public class SampleViolation {
public void sample() {
Map m = new HashMap();
Iterator it = m.keySet().iterator();
Object key = it.next();
Object v = m.get(key); // Violation
}
}
Should be written as:
package com.vimal.sample;
public class SampleCorrection
{
public void sample()
{
Map m = new HashMap();
Set set = m.entrySet(); //Correction.
Object keyValuePair = it.next();
}
}
Avoid boolean array
Do not use array of boolean.
Example:
package com.vimal.sample;
public class SampleViolation {
public void sample() {
boolean[] b = new boolean[]{true, false, true}; // VIOLATION
}
}
Should be written as:
package com.vimal.sample;
public class SampleCorrection {
public void sample() {
BitSet bs = new BitSet(3); // CORRECTION
bs.set(0);
bs.set(2);
}
}
Loop invariant code motion
The code that is going to result in same value over the iterations of loop, should be moved out of the loop.
Example:
package com.vimal.sample;
class SampleViolation {
public void sample(int x, int y, int[] z) {
for(int i = 0; i < z.length; i++) {
z[i] = x * Math.abs(y); // VIOLATION
}
}
}
Should be written as:
package com.vimal.sample;
class SampleCorrection {
public void sample(int x, int y, int[] z) {
int t1 = x * Math.abs(y); // CORRECTION
for(int i = 0; i < z.length; i++) {
z[i] = t1;
}
}
}
Use System arrayCopy
Use 'System.arraycopy()' instead of a loop to copy array. This rule disallows the copying of arrays inside a loop
Example:
package com.vimal.sample;
class SampleViolation {
public int[] copyArray (int[] array) {
int length = array.length;
int[] copy = new int [length];
for(int i=0;i<length;i++) {
copy [i] = array[i]; // VIOLATION
}
return copy;
}
}
Should be written as:
package com.vimal.sample;
class SampleCorrection {
public int[] copyArray (int[] array) {
final int ZERO = 0;
int length = array.length;
int[] copy = new int [length];
System.arraycopy(array, ZERO, copy, ZERO, length); // CORRECTION
return copy;
}
}
Avoid debugging code
Avoid debugging code.
Example:
package com.vimal.sample;
public void SampleVolation {
private int count =0;
public int getCount() {
//...
System.out.println("count ="+count); // Violation
return count;
}
}
Should be written as:
package com.vimal.sample;
public void SampleCorrection {
private int count =0;
public int getCount() {
if (Debug.ON) {
System.out.println("count ="+count); //correction
}
return count;
}
}
Ensure efficient removal of elements in a collection
Searching a collection to fetch the element to remove is inefficient.
Example:
public class Sample {
public void sample(Collection collection) {
Iterator iter = collection.iterator();
while (iter.hasNext()) {
Object element = iter.next();
collection.remove(element); // VIOLATION
}
}
}
Should be written as:
public class Sample {
public void someMethod(Collection collection) {
Iterator iter = collection.iterator();
while (iter.hasNext()) {
iter.remove(); // CORRECTION
}
}
}
Enjoy.....................