Skip to content

Latest commit

 

History

History
28 lines (16 loc) · 1.16 KB

m4.md

File metadata and controls

28 lines (16 loc) · 1.16 KB

Design for Robustness

Design by Contract

  • Precondition: It's the caller's responsibility to pass good data

Use assert to check and @pre in the docstring for things that should never happen. Do not use them for error handling.

  • Postcontition: What the function is guaranteed to do
  • Class invariants: A condition that is true when function exits

Heisenbug: When the debugging changes the behavior of the system being debugged.

Exceptions

Exceptions should never be used for control flow. A well-designed API must not force its clients to use exceptions for ordinary control flow. Implement state-testing methods like hasNext() to help client avoid try-catch.

Checked exceptions

Used for abnormal cases that can be recovered at runtime

  • Client: catch or declare the exception
  • API: declare in method signature

Unchecked exceptions

Recovery is impossible and continued execution would do more harm than good. Use runtime exceptions to indicate precondition violations (ex: ArrayIndexOutOfBoundsException). All unchecked throwable should subclass RuntimeException

Turn a checked exception into an uncheched exception by using if-else instead of try-catch