Chapter 21

Tantrum

image

21.1 Constraints

  • Every single procedure and function checks the sanity of its arguments and refuses to continue when the arguments are unreasonable.
  • All code blocks check for all possible errors, possibly log context-specific messages when errors occur, and pass the errors up the function call chain.

21.2 A Program in this Style

  1 #!/usr/bin/env python
  2
  3 import sys, re, operator, string, traceback
  4
  5 #
  6 # The functions
  7 #
  8 def extract_words(path_to_file):
  9 assert(type(path_to_file) is str), "I need a string!"
 10 assert(path_to_file), "I need a non-empty string!"
 11
 12 try:
 13  with open(path_to_file) as f:
 14 str_data ...

Get Exercises in Programming Style now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.