Best practices for writing an efficient BlackBerry Java Application
Best practice: Writing efficient code
To allow a BlackBerry® Java® Application to use resources efficiently, consider the following guidelines:
•
Use local variables.
•
Use shorthand for evaluating Boolean conditions.
•
Make classes final.
•
Use
int
instead of
long
.
•
Avoid garbage collection.
•
Use static variables for
Strings
.
•
Avoid the
String
(
String
) constructor.
•
Write efficient loops.
•
Optimize subexpressions.
•
Optimize division operations.
•
Avoid
java.util.Enumeration
.
•
Perform casts using
instanceof
.
•
Evaluate conditions using
instanceof
.
•
Avoid using
StringBuffer.append
(
StringBuffer
).
•
Avoid returning
null
.
•
Avoid passing
null
into methods.
•
Use caution when passing
null
into a constructor.
•
Use
long
for unique identifiers.
•
Exit applications correctly.
•
Print the stack trace.
Using local variables
Use local variables whenever possible. Access to local variables is more efficient than access to class members.
Using shorthand for evaluating Boolean conditions
To evaluate a
Boolean
condition, use shorthand. The resulting compiled code is shorter.
Code sample
return( boolean_expression );
Fundamentals Guide
Best practices for writing an efficient BlackBerry Java Application
13