记录一些Java编码条款,方便查看
Effective Java(3rd Edition)中的编码条款
Create and Destroying Objects
Item1: Consider static factory methods instead of constructors
Item2: Consider a builder when faced with many constructors
Item3: Enforce the singleton property with a private constructor or an enum type
Item4: Enforce noninstantiability with a private constructor
Item5: Prefer dependency injection to hardwiring resources
Item6: Avoid creating unnecessary objects
Item7: Eliminate absolete object references
Item8: Avoid finalizers and cleaners
Item9: Prefer try-with-resources to try-finally
Mothods Common to All Objects
Item10: Obey the general construct when overriding equals
Item11: Always override hashCode when you override equals
Item12: Always override toString
Item13: Override clone judiciously
Item14: Consider implementing Comparable
Classes and Interfaces
Item15: Minimize the accessibility of classes and members
Item16: In public classes, use accessor methods, not public fields
Item17: Minimize mutability
Item18: Favor composition over inheritance
Item19: Design and document for inheritance or else prohibit it
Item20: Prefer interfaces to abstract classes
Item21: Design interfaces for posterity
Item22: Use interfaces only to define types
Item23: Prefer class hierarchies to tagged classes
Item24: Favor static member classes over nonstatic
Item25: Limit source fields to a single top-level class
Generics
Item26: Don’t use raw types
Item27: Eliminate unchecked warnings
Item28: Prefer lists to arrays
Item29: Favor generic types
Item30: Favor generic methods
Item31: Use bounded wildcards to increase API flexibility
Item32: Combine generics and varargs judiciously
Item33: Consider typesafe heterogeneous containers
Enums and Annotations
Item34: Use enums instead of int constants
Item35: Use instance fields instead of ordinals
Item36: Use EnumSet instead of bit fields
Item37: Use EnumMap instead of ordinal indexing
Item38: Emulate extensible enums with interfaces
Item39: Prefer annotations to naming patterns
Item40: Consistently use the Override annotation
Item41: Use marker interfaces to define types
Lambdas and Streams
Item42: Prefer lambdas to anonymous classes
Item43: Prefer method reference to lambdas
Item44: Favor the use of standard functional interfaces
Item45: Use streams judiciously
Item46: Prefer side-effect-free functions in streams
Item47: Prefer Collections to Stream as a return type
Item48: Use caution when making streams parallel
Methods
Item49: Check parameters for validity
Item50: Make defensive copies when needed
Item51: Design method signatures carefully
Item52: Use overloading judiciously
Item53: Use varargs
Item54: Return empty collections or arrays, not nulls
Item55: Return optionals judiciously
Item56: Write doc comments for all exposed API elements
General Programming
Item57: Minimize the scope of local variables
Item58: Prefer for-each loops to traditional for loops
Item59: Know and use the libraries
Item60: Avoid float and double if exact answers are required
Item61: Prefer primitive types to boxed primitives
Item62: Avoid strings where other types are more appropriate
Item63: Beware the performance of string concatenation
Item64: Refer to objects by their interfaces
Item65: Prefer interfaces to reflection
Item66: Use native methods judiciously
Item67: Optimize judiciously
Item68: Adhere to generally accepted naming conventions
Exceptions
Item69: Use exceptions only for exceptional conditions
Item70: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors
Item71: Avoid unnecessary use of checked exceptions
Item72: Favor the use of standard exceptions
Item73: Throw exceptions appropriate to the abstraction
Item74: Document all exceptions thrown by each method
Item75: Include failure-capture information in detail messages
Item76: Strive for failure atomicity
Item77: Don’t ignore exceptions
Concurrency
Item78: Synchronize access to shared mutable data
Item79: Avoid excessive synchronization
Item80: Prefer executors, tasks, and streams to threads
Item81: Prefer concurrency utilities to wait and notify
Item82: Document thread safety
Item83: Use lazy initialization judiciously
Item84: Don’t depend on the thread scheduler
Serialization
Item85: Prefer alternatives to Java serialization
Item86: Implement Serializable with great caution
Item87: Consider using a custom serialized form
Item88: Write readObject methods defensively
Item89: For instance control, prefer enum types to readResolve
Item90: Consider serialization proxies instead of serialized instances
参考
Effective Java, Third Edition