1Design & Modularity15-441 Recitation 3Dave AndersenCarnegie Mellon UniversityThinking about Design• How do you start thinking about how aprogram should work?• Data-centric programs:– What data does it operate on?– How does it store it?– Examples?• Protocol-centric programs– How they interact with the rest of the world– (Maybe “Interface-centric”)• (Not exclusive! Think about IRC server)Design Principles• Goal: once again, pain management• Be able to develop independently• Avoid the big brick end-of-semester wall• Stay motivatedP1: Don’t Repeat Yourself• Aka “DRY”• Like factoring out common terms…• If you’re copy/pasting code or writing“similar feeling” code, perhaps it shouldbe extracted into its own chunk.• Small set of orthogonal interfaces tomodulesP2: Hide Unnecessary Details• aka, “write shy code”– Doesn’t expose itself to others– Doesn’t stare at others’ privates– Doesn’t have too many close friends• Benefit:– Can change those details later withoutworrying about who cares about themExample 1:• int send_message_to_user( struct user *u, char *message)• int send_message_to_user( int user_num, int user_sock, char *message)2Example 2int send_to_user(char *uname, char *msg){ … struct user *u; for (u = userlist; u != NULL; u = u->next) { if (!strcmp(u->username, uname) …Consider factoring into: struct user *find_user(char *username)• Hides detail that users are in a list– Could re-implement as hash lookup if bottleneck• Reduces size of code / duplication / bug count– Code is more self-explanatory (“find_user” obvious), easier to read, easierto testP3: Keep it Simple• We covered in previous recitation, but– Don’t prematurely optimize• Even in “optimization contest”, program speedis rarely a bottleneck• Robustness is worth more points than speed!– Don’t add unnecessary features• (Perhaps less pertinent in 441)P3.1: Make a few bits good• Some components you’ll use again– Lists, containers, algorithms, etc.• Spend the time to make these a bitmore reusable– Spend 20% more time on componentduring project 1– Save 80% time on project 2…P4: Be consistent• Naming, style, etc.– Doesn’t matter too much what you choose– But choose some way and stick to it– printf(str, args) fprintf(file,str, args)– bcopy(src, dst, len) memcpy(dst,src, len)• Resources: Free where you allocate– Consistency helps avoid memory leaksError handling• Detect at low level, handle high– Bad: malloc() { … if (NULL) abort(); }– Appropriate action depends on program– Be consistent in return codes andconsistent about who handles errorsIncremental Happiness• Not going to write program in one sitting• Cycle to go for:– Write a bit– Compile; fix compilation errors– Test run; fix bugs found in testing• Implies frequent points of “kinda-working-ness”3Development Chunks• Identify building blocks (structures, algos)– Classical modules with clear functions– Should be able to implement some with roughsketch of program design• Identify “feature” milestones– Pare down to bare minimum and go from there– Try to identify points where testable– Helps keep momentum up!• Examples from IRC server?Testability• Test at all levels– Recall goal: reduced pain!– Bugs easiest to find/correct early and insmall scope. Ergo:• Unit tests only test component (easier to locate)• Early tests get code while fresh in mind• Write tests concurrently with code. Or before!– Also need to test higher level functions• Scripting languages work well here441 Testability• Unit test examples:– Your hash, list, etc., classes– Machinery that buffers input for line-basedprocessing– Command parser– Routing table insert/lookup/etc.– Others?Bigger tests• More structured test framework early– “Connect” test (does it listen?)– Alternate port # test (cmd line + listen)– …Testing Mindset• Much like security: Be Adversarial• Your code is the enemy. Break it!– Goal of testing is not to quickly say “phew,it passes test 1, it must work!”– It’s to ensure that 5 days later, you don’tspend 5 hours tracking down a bug in it• Think about the code and then writetests that exercise it. Hit border cases.Testing a Hash Table• Insert an item and retrieve it– Why?• Insert two items and retrieve both– Why?[help me fill in this list!]Note ordering: Simple to complex…4Design & Debugging• Covering more next week, but…• Strongly, strongly encourage people touse a consistent DEBUG()-like macrofor debugging• Leave your debugging output in• Make it so you can turn it
View Full Document