Overall I was impressed with the advice given in the book. In order to make better practical use of it, I wanted to create a basic "outline" view that I could easily find and scan, making reference back to the book if needed. That is really all this post is. I thought about adding "notes"... either to each item or to each section, but they just cluttered it up. I want this to be more a quick cheat sheet of the guidelines, not a full blown Readers Digest edition of the book...
- Consider static factory methods instead of constructors
- Consider a builder when faced with many constructor parameters
- Enforce the singleton property with a private constructor or an enum type
- Enforce noninstantiability with a private constructor
- Avoid creating unnecessary objects
- Eliminate obsolete object references
- Avoid finalizers
- Obey the general contract when overriding equals
- Always override hashCode when you override equals
- Always override toString
- Override clone judiciously
- Consider implementing Comparable
- Minimize the accessibility of classes and members
- In public classes, use accessor methods, not public fields
- Minimize mutability
- Favor composition over inheritance
- Design and document for inheritance or else prohibit it
- Prefer interfaces to abstract classes
- Use interfaces only to define types
- Prefer class hierarchies to tagged classes
- Use function objects to represent strategies
- Favor static member classes over nonstatic
- Don't use raw types in new code
- Eliminate unchecked warnings
- Prefer lists to arrays
- Favor generic types
- Favor generic methods
- Use bounded wildcards to increase API flexibility
- Consider typesafe heterogeneous containers
- Use enums instead of int constants
- Use instance fields instead of ordinals
- Use EnumSet instead of bit fields
- Use EnumMap instead of ordinal indexing
- Emulate extensible enums with interfaces
- Prefer annotations to naming patterns
- Consistently use the Override annotation
- Use marker interfaces to define types
- Check parameters for validity
- Make defensive copies when needed
- Design method signatures carefully
- Use overloading judiciously
- Use varargs judiciously
- Return empty arrays or collections, not nulls
- Write doc comments for all exposed API elements
- Minimize the scope of local variables
- Prefer for-each loops to traditional for loops
- Know and use the libraries
- Avoid float and double if exact answers are required
- Prefer primitive types to boxed primitives
- Avoid strings where other types are more appropriate
- Beware the performance of string concatenation
- Refer to objects by their interfaces
- Prefer interfaces to reflection
- Use native methods judiciously
- Optimize judiciously
- Adhere to generally accepted naming conventions
- Use exceptions only for exceptional conditions
- Use checked exceptions for recoverable conditions and runtime exceptions for programming errors
- Avoid unnecessary use of checked exceptions
- Favor the use of standard exceptions
- Throw exceptions appropriate to the abstraction
- Document all exceptions thrown by each method
- Include failure capture information in detail message
- Strive for failure atomicity
- Don't ignore exceptions
- Synchronize access to shared mutable data
- Avoid excessive synchronization
- Prefer executors and tasks to threads
- Prefer concurrency utilities to wait and notify
- Document thread safety
- Use lazy initialization judiciously
- Don't depend on the thread scheduler
- Avoid thread groups
- Implement Serializable judiciously
- Consider using a custom serialized form
- Write readObject methods defensively
- For instance control, prefer enum types to readResolve
- Consider serialization proxies instead of serialized instances
Additions in the third edition, with the section in blue representing an entirely new category of items related to Streams and Lambdas, which were introduced in Java 8:
- Prefer dependency injection to hard wiring resources
- Avoid cleaners
- Prefer try-with-resources to try-finally
- Design interfaces for posterity (aka Avoid default methods on interfaces)
- Limit source files to a single top-level class
- Combine generics and varargs judiciously
- Prefer lambdas to anonymous classes
- Prefer method references to lambdas
- Favor the use of standard functional interfaces
- User streams judiciously
- Prefer site-effect-free functions in streams
- Prefer Collection to Stream as a return type
- Use caution when making streams parallel
- Return optionals judiciously
- Prefer alternatives to Java serializations
Josh Bloch gives a talk on the second edition which is well worth a watch:
No comments:
Post a Comment