- 45. Minimize the scope of local variables
- 46. Prefer for-each loops to traditional
- 47. Know and use the libraries
- 48. Avoid
doubleof exact answers are required
- 49. Prefer primitive types to boxed primitives
- 50. Avoid strings where other types are more appropriate
- 51. Beware the performance of string concatenation
- 52. Refer to objects by their interfaces
- 53. Prefer interfaces to reflection
- 54. Use native methods judiciously
- 55. Optimize judiciously
- 56. Adhere to generally accepted naming conventins
45. Minimize the scope of local variables
- Declare a variable when first using it.
- Declare a local variable with a initial expression
- Prefer for loop to while loop, since while loop may need a extra looping variable
46. Prefer for-each loops to traditional
47. Know and use the libraries
Example: random number generator
There are three flaws.
nis a small power of 2, the sequence of random numbers it generates will repeat itself after a fairly short period.
nis not a power of 2, some numbers will be returned more frequently than others. If
nis large, this effect can be quite pronouned.
- It can fail catastrophically, returning a number outside the specified range.
The easiest way to solve the problem is to use
Use libraries, take advantage of other ones' research, focus on your logic implementation, improve the readability of your code
Read java5-feat, java6-feat page on official website.
double of exact answers are required
double are not suitable for currency computation.
long instead. But
BigDecimal is slow and not convenient.
long is preferred.
49. Prefer primitive types to boxed primitives
Java 1.5 added new feature of autoboxing and auto-unboxing.
Pay attention, use
== on boxed primitive type always make wrong result.
In some circumstances, you have to use boxed primitives:
- Put element into
Collectiononly accept boxed primitives
- Generic type doesn't allow primitives either.
50. Avoid strings where other types are more appropriate
- Strings are poor substitues for other value types
- Strings are poor substitutes for enum types
- Strings are poor substitues for aggregate types. eg.
String compoundKey = className + "#" + i.next();
- Strings are poor substitues for capabilities
51. Beware the performance of string concatenation
String concatenation is not suitable for concatenate a lot of Strings.
Using the string concatenation operator repeatedly to concatenate n strings requires time quadratic in n.
52. Refer to objects by their interfaces
53. Prefer interfaces to reflection
The trade-off of reflection:
- Losing all the benefits of compile-time type checking
- The code required to perform reflective access is clumsy and verbose.
- Performance loss
The core reflection facility was originally designed for component-based application builder tools. It is a design time technique.
Example: a program that can be transformed into a type checking program
54. Use native methods judiciously
JNI is used to call native method, such as C/C++ programs. It is mainly used to accessible platform specific stuff, such as registry and file lock.
It is rarely advisable to use native methods for improved performance.
55. Optimize judiciously
We should forget about small efficiencies, say about 97% of the timeL premature optimization is the root of all evil. -- Donald Knuth
Do not optimize until you have a perfectly clear and unoptimized solution. -- M.A.Jackson