About Me

我的相片
Mr. Pigg
Taipei, Taiwan
檢視我的完整簡介

星期四, 7月 31, 2008

Effective Java Chapter 8 General Programming

0 意見

1. Minimize the scope of local variables
- 盡量在用到的地方才宣告
- 每個變數都要有initializer, 沒有代表你還不需要宣告
- Prefer for to while
- 讓每個method的功能盡量單純

2. Prefer for-each to for

3. Know and use the libraries
- don't reinvent the wheel
- java.lang / java.util / java.io
- java.util.concurrent

3. Avoid float and double if exact answers are required
- 用BigDecimal, int or long

4. 盡量使用primitive
- 使用boxed primitives時機: Collection keys, parameter types, reflective method invocation

5. Avoid strings where other types are more appropriate

6. 不用把String 相加

7. Refer to objects by their interfaces
- 除非是沒有interface 如value class
- or interface沒有定義要用的method
- by abstract class

8. Optimize judiciously
- 如果不清楚的話,不要亂做optimization
- 好的程式架構比performance重要

9. Naming convention

10. Prefer interface to reflection

星期二, 7月 29, 2008

Effective Java - Chapter 7 Method

0 意見

1. Check parameters for validity
- For exported API: check parameters first
- IllegalArgumentException, IndexOutOfBoundsException, NullPointerException
- For unexported API: Assertion
- 小心不要讓check parameters影響performance
- Document it

2. Make defensive copies when needed
- 確保物件不會被傳進來的人改掉
- 不是Final class不要用clone
- return 物件最好也用defensive copy
- 最好是immutable

3. Design method signatures carefully
- Class不要有太多method
- Method不要有太長的parameters < 4
- 方法: break it, Helper, Builder
- 參數favor interfaces over classes

4. Use overloading judiciously
- 使用哪一個overload method是在compile時決定的
- Overriden method則是在run-time決定的
- 盡量不要使用參數數量相同的overload method
- 或是兩個決定不相容的參數也可
- 小心autoboxing/autounboxing
- 常因為class evolusion導致產生兩個互通參數的overload method, 只要的做的事相同即可

5. Use varargs judiciously
- 用 (int first, int ... remain)可以省去檢查參數的麻煩
- 不要把原本用array傳參數的改成 varargs,會出錯
- performance issue

6. Return empty arrays or collections, not nulls
- client可以不用檢查null, 避免錯誤

7. Document for all exposed API elements
- summary description
- precondition, postcondition
- @param, @return @throws
- Thread safety
- Serialization
- Class for inheritance
- @code, @literal
- Generic ,Annotation, enum

星期日, 7月 13, 2008

Effective Java - Chapter 5 Generics

0 意見

  1. Dont' use raw type in new code
    - Wildcard : to provide a simple interface to client

  2. Eliminate unchecked warnings- Always use the Suppress-Warnings annotation on the smallest scope possible

  3. Prefer lists to ararys
    - Arrays check type at runtime, generics check type at compile time
    - type reified / type erasure
    - can't create generic type array => type not safe because of type erasure
    - type erasure: 不能直接 new E[], new List[], new List[] (non-reifiable type)
    因為runtime沒辦法保證Array裡面存在一致的物件

  4. Favor Generic Type
    - 盡量使用Generic Type,而不要讓Client端自己cast

  5. Favor Generic Method
    - type inference
    - for static factory method
    - recursive type bound

  6. Use bounded wildcards to increase API flexibility
    - PECS
    - 如果Signature中只有一個type parameter, 就用wildcard

  7. Consider typesafe heterogeneous containers


Help:

Class.asSubClass
Class.cast