代码改变世界

Effective Java 40 Design method signatures carefully

2014-04-03 08:53  小郝(Kaibo Hao)  阅读(432)  评论(0编辑  收藏  举报

Principle

  1. Choose method names carefully.
  2. Don't go overboard in providing convenience methods.
  3. Avoid long parameter lists.
    1. Break the method up into multiple methods. Such as sublist element of List interface.
    2. Create helper classes to hold groups of parameters.
    3. Adapt the builder pattern form object construction to method invocation. See item 2.
  4. For parameter types, favor interfaces over classes.
  5. Prefer two-element enum types to boolean parameters.

    public enum TemperatureScale { FAHRENHEIT, CELSIUS }