15-441 Recitation 3 - PowerPoint PPT Presentation

About This Presentation
Title:

15-441 Recitation 3

Description:

How do you start thinking about how a program should work? ... Make it so you can turn it on/off. Project 1 Standalone IRC Server. Homework 1. Doubts/ Question ? ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 25
Provided by: davidan
Learn more at: http://www.cs.cmu.edu
Category:

less

Transcript and Presenter's Notes

Title: 15-441 Recitation 3


1
Design, Modularity Testing
  • 15-441 Recitation 3
  • Dave Andersen
  • Carnegie Mellon University

2
Recitation 3
  • Design Principle
  • Guidelines for Projects
  • Testing
  • QA - Project 1/HW 1.

3
Thinking about Design
  • How do you start thinking about how a program
    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)

4
Design Principles
  • Goal once again, pain management
  • Be able to develop independently
  • Avoid the big brick end-of-semester wall
  • Stay motivated

5
P1 Dont Repeat Yourself
  • Aka DRY
  • Like factoring out common terms
  • If youre copy/pasting code or writing similar
    feeling code, perhaps it should be extracted
    into its own chunk.
  • Small set of orthogonal interfaces to modules

6
Exercise 1
  • int send_usr_join_msg(char ch_name, char msg)
  • struct user u
  • for (u userlist u ! NULL u u-gtnext)
  • if (!strcmp(u-gtusername, uname)
  • int send_usr_quit_msg(char ch_name, char msg)
  • struct user u
  • for (u userlist u ! NULL u u-gtnext)
  • if (!strcmp(u-gtusername, uname)

7
Exercise 1 Contd..
  • 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, easier to test

8
P2 Hide Unnecessary Details
  • aka, write shy code
  • Doesnt expose itself to others
  • Doesnt stare at others privates
  • Doesnt have too many close friends
  • Benefit
  • Can change those details later without worrying
    about who cares about them

9
Exercise 2
  • int send_message_to_user(
  • struct user u,
  • char message)
  • int send_message_to_user(
  • int user_num,
  • int user_sock,
  • char message)

10
P3 Keep it Simple
  • We covered in previous recitation, but
  • Dont prematurely optimize
  • Even in optimization contest, program speed is
    rarely a bottleneck
  • Robustness is worth more points than speed!
  • Dont add unnecessary features
  • (Perhaps less pertinent in 441)

11
P3.1 Make a few bits good
  • Some components youll use again
  • Lists, containers, algorithms, etc.
  • Spend the time to make these a bit more reusable
  • Spend 20 more time on component during project 1
  • Save 80 time on project 2

12
P4 Be consistent
  • Naming, style, etc.
  • Doesnt 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 leaks

13
Exercise 3
  • Void Usr_function()
  • char src 15441
  • char dst
  • strcpy(src,dst)
  • free(dst)
  • Void strcpy(char src, char dst)
  • dst malloc(strlen(src))
  • While((dst src) ! \0)
  • dst
  • src

What is Wrong with this program ??
14
Exercise 3 Contd..
Void Usr_function() char src 15441 char
dst malloc(strlen(src)) strcpy(src,dst)
free(dst) Void strcpy(char src, char
dst) While((dst src) ! \0) dst
src Avoid using strcpy, strlen,
sprintf,..instead use safer version strncpy(),
strnlen(), snprintf().
15
1 Error handling
  • Detect at low level, handle high
  • Bad
  • malloc() if (NULL) abort()
  • Appropriate action depends on program
  • Be consistent in return codes and consistent
    about who handles errors

16
2 Incremental 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

17
3 Development Chunks
  • Identify building blocks (structures, algos)
  • Classical modules with clear functions
  • Should be able to implement some with rough
    sketch 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?

18
Testability
  • Test at all levels
  • Recall goal reduced pain!
  • Bugs easiest to find/correct early and in small
    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 here

19
441 Testability
  • Unit test examples
  • Your hash, list, etc., classes
  • Machinery that buffers input for line-based
    processing
  • Command parser
  • Routing table insert/lookup/etc.
  • Others?

20
Bigger tests
  • More structured test framework early
  • Connect test (does it listen?)
  • Alternate port test (cmd line listen)

21
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!
  • Its to ensure that 5 days later, you dont spend
    5 hours tracking down a bug in it
  • Think about the code and then write tests that
    exercise it. Hit border cases.

22
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

23
Design Debugging
  • Covering more next week, but
  • Strongly, strongly encourage people to use a
    consistent DEBUG()-like macro for debugging
  • Leave your debugging output in
  • Make it so you can turn it on/off

24
  • Project 1 Standalone IRC Server
  • Homework 1
  • Doubts/ Question ?
Write a Comment
User Comments (0)
About PowerShow.com