Software Design Fall 2004 For next time: 1) Read Chapter 12 2) Work on Homework 4 3) Prepare for the exam by organizing materials. Bugs are good for you --------------------- Debugging is the most important programming skill; if you can't debug, you can't program :( Every bug you create is an opportunity to practice debugging; don't waste them! When you find one, ask yourself what you could have done to find it faster. Any good bug reports? Allen's Law of Debugging ------------------------ "The best kind of debugging is the kind you don't have to do." 1) test components in isolation 2) play with new features until you are sure you understand them many of the worst bugs are caused by errors in your mental model of the program 3) develop code incrementally 4) reuse idioms! Examples? Homework 3 Solutions -------------------- There were two points to this homework: 1) take advantage of primitives def is_palindrome(word): t1 = list(word) t2 = list(word) t2.reverse() return t1 == t2 def has_no_e(word): return 'e' not in word 2) practice "design by pattern" Many problems can be expressed as a search. a) look at the elements of a sequence in order b) if you find what you are looking for, return immediately c) if you get to the end, that means you didn't find it This is called the "Eureka pattern" Some filters obviously fit this pattern: def has_e(word): for c in word: if c == 'e': return True return False In other cases, you have to flip the return values to make the pattern fit: def avoids(word, forbidden): for c in word: if c in forbidden: return False return True Or flip the sense of the condition: def uses_only(word, available): for c in word: if c not in available: return False return True Or flip the relationship between the word and the string: def uses_all(word, required): for c in required: if c not in word: return False return True The hardest part of "design by pattern" is pattern recognition. def uses_all(word, required): return uses_only(required, word) Most common way to mess up the eureka pattern: def has_e(word): for c in word: if c == 'e': return True else: return False Homework 4 Tips --------------- What is the point of Homework 4? 1) practice with dictionaries 2) reading documentation "getting intimate with the primitives" ahem. 4) incremental development Always always always always always always always always always always TEST NEW THINGS IN ISOLATION BEFORE TRYING TO USE THEM IN A PROGRAM. Reminder -------- Most string functions are fruitful! s = s.upper() # RIGHT s.upper() # WRONG Most list functions are not! t.reverse() # RIGHT t = t.reverse() # WRONG