J2ME Code

Rules available in this category:

  1. Avoid_array_length_in_loop_condition
  2. Avoid_unused_classes_that_are_subclassed_only_once
  3. Avoid_using_anonymous_classes_as_interface_implementors
  4. Always_catch_OutOfMemoryError_while_allocating_large_arrays
  5. Use_method_parameters_instead_of_returning_objects
  6. Avoid_overly_accessing_a_field
  7. Avoid_large_number_of_constant_initializations_of_primitive_arrays

Rule 1: Avoid_array_length_in_loop_condition

Severity:  High
Rule:  Avoid array.length in loop condition.
Reason:  Avoid array.length in loop condition.

Usage Example: 

package com.rule;

public class Avoid_array_length_in_loop_condition_violation
{
private void foo(int arr[])
{
for(int i=0; i<arr.length; i++) // Violation
{
//...
}
}
}

Should be written as:

package com.rule;

public class Avoid_array_length_in_loop_condition_correction
{
private void foo(int arr[])
{
int iSize = arr.length;  // Correction
for(int i=0; i<iSize; i++)
{
//...
}
}
}

Reference:  Reference not available.

Rule 2: Avoid_unused_classes_that_are_subclassed_only_once

Severity:  Medium
Rule:  Reducing the number of classes will reduce the application's overall source code size.
Reason:  Reducing the number of classes will reduce the application's overall source code size.

Usage Example: 

public class MyClass extends MyObject 
{
    private int myData;

    public void setMyData(int data) 
   {
        myData = data;
    }

    public int getMyData() 
   {
        return myData;
    }
}

abstract class MyObject     // VIOLATION

    public int hashCode() 
   {
        return super.hashCode();
    }
}

Should be written as:

public class MyClass // FIXED

    private int myData;

    public void setMyData(int data) 
   {
        myData = data;
    }

    public int getMyData() 
   {
        return myData;
    }

    public int hashCode() 
   {
        return super.hashCode();
    }
}

Reference:  http://developers.sun.com/techtopics/mobility/midp/ttips/appsize/

Rule 3: Avoid_using_anonymous_classes_as_interface_implementors

Severity:  Medium
Rule:  The number of allocated objects are reduced by re-using an existing instance of the enclosing class.
Reason:  The number of allocated objects are reduced by re-using an existing instance of the enclosing class.

Usage Example: 

public class MyClass extends Frame 
{
     private TextField text;

     public MyClass() 
    {
         add(text = new TextField(), "Center");
         pack();
         setVisible(true);
         text.addActionListener(new ActionListener() { // VIOLATION
             public void actionPerformed(ActionEvent event) 
            {
                 text.setText("");
             }
         });
     }
}

Should be written as:

public class MyClass extends Frame implements ActionListener // FIXED
{
     private TextField text;

     /**
      * Creates a new example object.
      */
     public MyClass()
    {
         add(text = new TextField(), "Center");
         pack();
         setVisible(true);
         text.addActionListener(this); // FIXED
     }

     public void actionPerformed(ActionEvent event) 
    {
         text.setText("");
     }
}

Reference:  http://developers.sun.com/techtopics/mobility/midp/ttips/appsize/

Rule 4: Always_catch_OutOfMemoryError_while_allocating_large_arrays

Severity:  High
Rule:  Instead of letting the device's default mechanism report an OutOfMemoryError, the application should catch the error.
Reason:  Instead of letting the device's default mechanism report an OutOfMemoryError, the application should catch the error.

Usage Example: 

public class MyArrayAlloc
{
private final static int IMAGE_SIZE = 8*1024;

      public static byte[] createNewImageMemory()
{
         return new byte[IMAGE_SIZE]; // VIOLATION
      }

}

Should be written as:

public class MyArrayAlloc
{
private final static int IMAGE_SIZE = 8*1024;

      public static byte[] createNewImageMemory() 
{
try 
{
return new byte[IMAGE_SIZE];
          } 
catch (OutOfMemoryError error)  // CORRECTION
{
return new byte[0];
       
      // Or report the error in an appropriate manner,
             // or use a different way of obtaining the memory
         }
     }

}

Reference: 

Rule 5: Use_method_parameters_instead_of_returning_objects

Severity:  High
Rule:  Usually, the method creates a new object that might be quickly disposed, thus straining the memory resources of the device as well as the garbage collector.
Reason:  Usually, the method creates a new object that might be quickly disposed, thus straining the memory resources of the device as well as the garbage collector.

Usage Example: 

public class ReturnObjects
{

public static Dimension getDimensions(int x1, int y1, int x2, int y2) 
{
return new Dimension(Math.abs(x1-x2), Math.abs(y1-y2)); // VIOLATION
     }

}

Should be written as:

public class ReturnObjects
{

public static Dimension getDimensions(int x1, int y1, int x2, int y2,  Dimension  returnValue) 
{
returnValue.width = Math.abs(x1-x2);
returnValue.height = Math.abs(y1-y2);
return returnValue; // CORRECTION
    }

}

Reference: 

Rule 6: Avoid_overly_accessing_a_field

Severity:  High
Rule:  Eliminating excessive use of fields will bring down the code size and can potentially increase the execution speed.
Reason:  Eliminating excessive use of fields will bring down the code size and can potentially increase the execution speed.

Usage Example: 

public class HighAccess
{

private int value;

public void adjustValue(boolean a, boolean b, boolean c) 
{
if (a) 
{
              value += 1;
          }
if (b) 
{
            value += 2;
          }
if (c) 
{
             value += 3;
          }
if (a || b) 
{
              value += 4; // VIOLATION
          }
     }

}

Should be written as:

public class HighAccess
{

private int value;

public void adjustValue(boolean a, boolean b, boolean c) 
{
int value = this.value; // CORRECTION
         if (a) 
{
              value += 1;
          }
if (b) 
{
            value += 2;
          }
if (c) 
{
             value += 3;
          }
if (a || b) 
{
              value += 4; 
          }

this.value = value; // CORRECTION
     }

}

Reference:  http://developers.sun.com/techtopics/mobility/midp/articles/uidesign/

Rule 7: Avoid_large_number_of_constant_initializations_of_primitive_arrays

Severity:  High
Rule:  If large arrays need to be filled with predefined constants, this can cause a significant overhead in bytecode size.
Reason:  If large arrays need to be filled with predefined constants, this can cause a significant overhead in bytecode size.

Usage Example: 

public class ArrayInit
{
public void aMethod()
{
int[] prime = {2, 3, 5, 7, 11};  // VIOLATION
}
}

Should be written as:

Move large constant arrays into binary files and initialize the array from the content of the file.

Reference:  http://java.sun.com/developer/J2METechTips/2002/tt0226.html