On this page:
All the Design Guidelines
7.7

Design Principles

All the Design Guidelines

By the end of the course, you will be expected to have a strong grasp of all of the design principles listed below, and be able to discuss and apply them in your own work. A few notes: First, many of them overlap with each other. For example, several are simply more specific reinforcements or examples of a broader principle. Second, some of them may contradict each other. With design, there is often no one right answer, no silver bullet, but a matter of balancing tradeoffs and meeting the needs of the specific situation you are designing for. Third, many of them are prescriptive: things you should always or never do; others are guidelines: things to aspire to but that cannot always be adhered to.

  1. Javadoc all public classes and methods. Class comment should be at least two sentences, and provide information not already clear from its definition.

  2. Use interface types over concrete classes wherever possible. Exception: immutable “value” objects. Classes with no interface.

  3. Fields must always be private. Exception: constants. Methods, classes should be as private as possible.

  4. Class should never have public methods not in the interface (aside from constructor).

  5. Composition over inheritance.

  6. Catch and handle/report errors as early as possible. Use Java compiler checks, enums, final first, runtime checks second.

  7. Use class types over strings.

  8. Check inputs.

  9. Use exceptions only for exceptional situations – not for flow control.

  10. Checked vs unchecked: checked: reasonable expectation that the program can recover. Unchecked: programmer error (may still be recoverable).

  11. Don’t leave things in an inconsistent state for any substantive length of time.

  12. Beware of references, copies, and mutation. Make defensive copies.

  13. Separate responsibilities: one class, one responsibility.

  14. Use class hierarchies and dynamic dispatch over tagged classes, complex if/switch statements.

  15. Do not duplicate code.

  16. Open for extension, closed for modification: make changes without modifying existing code; write code to support later changes without modification.

  17. Extensibility: design to make likely later changes easier.

  18. Write tests first, cover the range of situations, edge cases. Write code to be testable (avoid System.out); do not expose fields or add public methods just to allow for testing.

  19. Loose coupling over tight coupling (avoid System.out). Write reusable components when possible.

  20. You can’t change an interface once it’s published.

  21. If you override equals(), override hashCode(), and vice-versa.

  22. Reuse existing exceptions, classes, libraries, and designs.