CS257 Software Design Friday, 9 May 2025 def clean_text(s): s = s.replace("'", "''") s = s.replace(...) ... @app.route('/whatever') search_text = clean_text(flask.request.args('search_text')) s query = ''' SELECT title, surname FROM books, authors, books_authors WHERE books.id = books_authors.book_id AND authors.id = books_authors.author_id''' params = [] if there's a "search_text" GET parameter: query = query + " AND author.surname LIKE '...%s...'" params.append(search_text) cursor.execute(query, params) ... print(cursor.query) + Questions? + Readings - writing good functions - in the *calling code* a function invocation should make sense - every function should do one thing "functional cohesion" # 2 things in one function: BAD def is_prime(n): ...do some stuff... if it_is_prime: # assumes that what the caller wants print(n) # is to print primes return it_is_prime - keep it brief (but not too brief) # good def is_prime(n): ... (<= 150 lines) return result # bad def add_one(n): return n + 1 - "does the routine's parameter list, taken as a whole present a consistent interface abstraction?" Jeff's version: "does the name of the function tell me roughly what the function does, and does this list of parameters make sense as 'the info the function needs to do its job'" Here, the info the function needs to do its job consists of the integer in question. That's the parameter. The info the caller needs back (via the return value) is "yes or no, it's prime". def is_prime(n): ''' Returns True if the integer n is a prime integer, and False otherwise ''' # Ugh. This fails on many levels. def do_something(operation, param1, param2, param3): ''' if operation = 0, do a Caesar cipher encryption of string param1 using shift param2, and ignore param3 if operation = 1, do some other damn thing ... ''' - writing (or not) good comments - Section headers in a long function are good - Don't just repeat the code; give a higher-level description of what's going on. (Mostly, I use this kind of comment to document functions.) - Both of the above, clarifying relationship between info coming in and info going out is important - Top-of-file "what does this file do?" + copyright/authorship - Bad: comment repeats what the code says x = x + 1 # increment x - If the code is clear enough at a glance, don't comment it - Comments can go out of date; this is very bad - If you need heavy summarizing comments, maybe the code itself is bad + Web pages: what happens when? - clients, servers, sequence of events - curl - browser developer tools - impact of multi-processing - what's it all mean for - desktop vs phone - speed of loading - what's going on on scam or ad-heavy sites? + Happy Friday!