Princeton University Computer Science 217: Introduction to Programming Systems. Modularity design principles

Similar documents
Princeton University Computer Science 217: Introduction to Programming Systems. Modularity design principles

Princeton University Computer Science 217: Introduction to Programming Systems. iclicker Question (from last time) Modularity design principles

3/5/17. String Module. Stack Module. Princeton University Computer Science 217: Introduction to Programming Systems. Module Design Principles

In class, Wednesday March 14; split between here and CS 105

Princeton University Computer Science 217: Introduction to Programming Systems. Modules and Interfaces

Goals of this Lecture

Jacobs University Bremen February 7 th, 2017 Dr. Kinga Lipskoch. Practice Sheet. First Name:... Last Name:... Matriculation Number:...

Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

Pointers, Arrays, and Strings. CS449 Spring 2016

Student Number: Instructor: Reid Section: L0201 (12:10-1:00pm)

Review: Constants. Modules and Interfaces. Modules. Clients, Interfaces, Implementations. Client. Interface. Implementation

Computer Programming: Skills & Concepts (CP) Strings

SYSTEM AND LIBRARY CALLS. UNIX Programming 2015 Fall by Euiseong Seo

Lecture 05 Pointers ctd..

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

CSC 209H1 S 2012 Midterm Test Duration 50 minutes Aids allowed: none. Student Number: # 1: / 7

File IO and command line input CSE 2451

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

csc/cpe 357 C Quiz Archive

Stream Model of I/O. Basic I/O in C

CSC 209H1 S 2012 Midterm Test Duration 50 minutes Aids allowed: none. Student Number: # 1: / 6

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

[6 marks] All parts of this question assume the following C statement. Parts (b) through (e) assume a variable called ptrs.

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

8. Characters, Strings and Files

Introduction to Programming Systems

Getting Started. Project 1

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Fundamentals of Programming & Procedural Programming

COMP 2355 Introduction to Systems Programming

Standard I/O in C, Computer System and programming in C

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

Administrative Stuff. More on Strings, File I/O. Last Time. Java API

More on Strings, File I/O. September 8, 2016

CSC209H1S Day Midterm Solutions Winter 2010

Memory, Arrays & Pointers

2009 S2 COMP File Operations

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

Arrays and Strings. CS449 Fall 2017

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Process Management! Goals of this Lecture!

Program Security and Vulnerabilities Class 2

File Access. FILE * fopen(const char *name, const char * mode);

Input/Output and the Operating Systems

COMP 2355 Introduction to Systems Programming

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

Characters and Strings

ECE551 Midterm Version 1

ECE551 Midterm Version 1

Princeton University Computer Science 217: Introduction to Programming Systems. I/O Management

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

ECE551 Midterm Version 2

UNIX System Programming

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

Characters, Character Strings, and string-manipulation functions in C

System Programming. Standard Input/Output Library (Cont d)

CS201 Lecture 2 GDB, The C Library

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

Programming in C. 4. Misc. Library Features, Gotchas, Hints and Tips. Dr. Neel Krishnaswami University of Cambridge

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

Princeton University Computer Science 217: Introduction to Programming Systems I/O Management

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

Lecture 9 Assertions and Error Handling CS240

Arrays and Strings (2H) Young Won Lim 3/7/18

Programming in C. Session 7. Seema Sirpal Delhi University Computer Centre

Fundamentals of Computer Security

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

File I/O, Project 1: List ADT. Bryce Boe 2013/07/02 CS24, Summer 2013 C

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

Procedural Programming

Operating Systems CS 217. Provides each process with a virtual machine. Promises each program the illusion of having whole machine to itself

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

25.2 Opening and Closing a File

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Winter 2017 March 2, 2017 CS107 2 nd Midterm Examination

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

For Your Amusement. Beware of bugs in the above code; I have only proved it correct, not tried it. Donald Knuth. Program Verification

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

Computer Programming Unit v

DAY 3. CS3600, Northeastern University. Alan Mislove

Winter 2017 CS107 Midterm #2 Examination (Practice #2) Problem Points Score Grader TOTAL 30

Project 2 : User Level Thread Library Operating Systems September 19, 2003

CS 137 Part 6. ASCII, Characters, Strings and Unicode. November 3rd, 2017

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

EL2310 Scientific Programming

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

C PROGRAMMING Lecture 6. 1st semester

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

Principles of C and Memory Management

Transcription:

Princeton University Computer Science 217: Introduction to Programming Systems Modularity design principles 1

Module Design Principles We propose 7 module design principles And illustrate them with 4 examples List, string, stdio, SymTable 2

Stack Module List module (wrong) list.h struct List {int len; int *data}; struct List * new(void); void insert (struct List *p, int key); void concat (struct List *p, struct List *q); int nth_key (struct List *p, int n); void free (List *p); List module (abstract) list.h struct List; struct List * new(void); void insert (struct List *p, int key); void concat (struct List *p, struct List *q); int nth_key (struct List *p, int n); void free (List *p); 3

String Module string module (from C90) /* string.h */ size_t strlen(const char *s); char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n); char *strcat(char *dest, const char *src); char *strncat(char *dest, const char *src, size_t n); int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t n); char *strstr(const char *haystack, const char *needle); void *memcpy(void *dest, const void *src, size_t n); int memcmp(const void *s1, const void *s2, size_t n); 4

Stdio Module stdio module (from C90, vastly simplified) /* stdio.h */ typedef struct _iobuf { int cnt; /* characters left */ char *ptr; /* next character position */ char *base; /* location of buffer */ int flag; /* mode of file access */ int fd; /* file descriptor */ } FILE; Note: no #define OPEN_MAX 1024 FILE _iob[open_max]; #define stdin (&_iob[0]); #define stdout (&_iob[1]); #define stderr (&_iob[2]); Don t be concerned with details 5

Stdio Module stdio (cont.) FILE *fopen(const char *filename, const char *mode); int fclose(file *f); int fflush(file *f); int int int int int int int int fgetc(file *f); getchar(void); fputc(int c, FILE *f); putchar(int c); fscanf(file *f, const char *format, ); scanf(const char *format, ); fprintf(file *f, const char *format, ); printf(const char *format, ); int sscanf(const char *str, const char *format,...); int sprintf(char *str, const char *format,...); 6

SymTable Module SymTable module (from Assignment 3) /* symtable.h */ typedef struct SymTable *SymTable_T; SymTable_T SymTable_new(void); void SymTable_free(SymTable_T t); size_t SymTable_getLength(SymTable_T t); int SymTable_put(SymTable_T t, const char *key, const void *value); void *SymTable_replace(SymTable_T t, const char *key, const void *value); int SymTable_contains(SymTable_T t, const char *key); void *SymTable_get(SymTable_T t, const char *key); void *SymTable_remove(SymTable_T t, const char *key); void SymTable_map(SymTable_T t, void (*pfapply)(const char *key, void *value, void *extra), const void *extra); 7

Agenda A good module: Encapsulates data Is consistent Has a minimal interface Detects and handles/reports errors Establishes contracts Has strong cohesion (if time) Has weak coupling (if time) 8

Encapsulation A well-designed module encapsulates data An interface should hide implementation details A module should use its functions to encapsulate its data A module should not allow clients to manipulate the data directly Why? Clarity: Encourages abstraction Security: Clients cannot corrupt object by changing its data in unintended ways Flexibility: Allows implementation to change even the data structure without affecting clients 9

Encapsulation Example 1 List (nonabstract) list.h struct List {int len; int *data}; struct List * new(void); Structure type definition in.h file void insert (struct List *p, int key); void concat (struct List *p, struct List *q); int nth_key (struct List *p, int n); void free_list (List *p); Interface reveals how List object is implemented That is, as an array Client can access/change data directly; could corrupt object 10

Encapsulation Example 1 List (abstract) list.h struct List; struct List * new(void); void insert (struct List *p, int key); Place declaration of struct Stack in interface; move definition to implementation void concat (struct List *p, struct List *q); int nth_key (struct List *p, int n); void free_list (List *p); Interface does not reveal how List object is implemented Client cannot access data directly That s better 11

Encapsulation Example 1 List (abstract, with typedef) typedef struct List *List_T; List_T new(void); void insert (List_T p, int key); Opaque pointer type void concat (List_T p, List_T q); int nth_key (List_T p, int n); void free_list (List_T p); Interface provides List_T abbreviation for client Interface encourages client to think of objects (not structures) and object references (not pointers to structures) Client still cannot access data directly; data is opaque to the client That s better still 12

Encapsulation Examples 2, 4 string Doesn t encapsulate the string data: user can access the representation directly. This is not an ADT, it is just a (nonabstract) data type. SymTable Uses the opaque pointer-to-type pattern Encapsulates state properly 13

Encapsulation Example 3 stdio /* stdio.h */ struct FILE { int cnt; /* characters left */ char *ptr; /* next character position */ char *base; /* location of buffer */ int flag; /* mode of file access */ int fd; /* file descriptor */ }; Violates the abstraction principle Programmers can access data directly Can corrupt the FILE object Can write non-portable code But the functions are well documented, so Few programmers examine stdio.h Few programmers are boneheaded enough to access the data directly Structure type definition in.h file If your neighbors leave their door unlocked, does that make it OK to steal their stuff? 14

Encapsulation Example 3 stdio /* stdio.h */ struct FILE { int cnt; /* characters left */ char *ptr; /* next character position */ char *base; /* location of buffer */ int flag; /* mode of file access */ int fd; /* file descriptor */ }; Structure type definition in.h file Why did its designers violate the abstraction principle? Two reasons: 1. In 1974 when stdio.h was first written, the abstraction principle was not widely understood (Barbara Liskov at MIT was just then inventing it) 2. Because function calls were expensive, getchar() and getc() were implemented as macros that accessed the FILE struct directly. But in the 21 st century, function calls are not expensive anymore; getchar() isn t a macro anymore in most implementations of stdio.h. 15

When s the right time... to make an opaque structure definition in stdio.h? /* stdio.h */ struct FILE; Well, suppose there are 100 million C modules that import stdio.h. Let s suppose 99% use FILE as if it were opaque, and would not be broken by changing to an opaque structure definition. In which year is it OK to break 1 million C modules? Summary: stdio.h is practically an ADT, and you should treat it as if struct FILE were opaque. 16

Agenda A good module: Encapsulates data Is consistent Has a minimal interface Detects and handles/reports errors Establishes contracts Has strong cohesion (if time) Has weak coupling (if time) 17

Consistency A well-designed module is consistent A function s name should indicate its module Facilitates maintenance programming Programmer can find functions more quickly Reduces likelihood of name collisions From different programmers, different software vendors, etc. A module s functions should use a consistent parameter order Facilitates writing client code 18

Consistency Examples 1 List (-) Each function name begins with List_ (+) First parameter identifies List object typedef struct List *List_T; List_T List_new(void); Oops, let s fix that! void List_insert (List_T p, int key); void List_concat (List_T p, List_T q); int List_nth_key (List_T p, int n); void List_free (List_T p); List (revised) (+) Each function name begins with List_ (+) First parameter identifies List object 19

Consistency Examples 1, 4 List (+) Each function name begins with List_ (+) First parameter identifies List object SymTable (+) Each function name begins with SymTable_ (+) First parameter identifies SymTable object 20

Consistency Example 2 string /* string.h */ Are function names consistent? size_t strlen(const char *s); char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n); char *strcat(char *dest, const char *src); char *strncat(char *dest, const char *src, size_t n); int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t n); char *strstr(const char *haystack, const char *needle); void *memcpy(void *dest, const void *src, size_t n); int memcmp(const void *s1, const void *s2, size_t n); Is parameter order consistent? 21

Consistency Example 3 stdio FILE *fopen(const char *filename, const char *mode); int fclose(file *f); int fflush(file *f); int int int int fgetc(file *f); getchar(void); fputc(int c, FILE *f); putchar(int c); Are function names consistent? Is parameter order consistent? int int int int fscanf(file *f, const char *format, ); scanf(const char *format, ); fprintf(file *f, const char *format, ); printf(const char *format, ); int sscanf(const char *str, const char *format,...); int sprintf(char *str, const char *format,...); 22

Agenda A good module: Encapsulates data Is consistent Has a minimal interface Detects and handles/reports errors Establishes contracts Has strong cohesion (if time) Has weak coupling (if time) 23

Minimization A well-designed module has a minimal interface Function declaration should be in a module s interface if and only if: The function is necessary to make objects complete, or The function is convenient for many clients Why? More functions higher learning costs, higher maintenance costs 24

Minimization Example 2 string /* string.h */ Should any functions be eliminated? size_t strlen(const char *s); char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n); char *strcat(char *dest, const char *src); char *strncat(char *dest, const char *src, size_t n); int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t n); char *strstr(const char *haystack, const char *needle); void *memcpy(void *dest, const void *src, size_t n); int memcmp(const void *s1, const void *s2, size_t n); 25

Minimization Example 3 stdio FILE *fopen(const char *filename, const char *mode); int fclose(file *f); int fflush(file *f); int int int int fgetc(file *f); getchar(void); fputc(int c, FILE *f); putchar(int c); Should any functions be eliminated? int int int int fscanf(file *f, const char *format, ); scanf(const char *format, ); fprintf(file *f, const char *format, ); printf(const char *format, ); int sscanf(const char *str, const char *format,...); int sprintf(char *str, const char *format,...); 26

Minimization Example 4 SymTable Declares SymTable_get() in interface Declares SymTable_contains() in interface Should SymTable_contains() be eliminated? 27

Minimization Example 4 SymTable Defines SymTable_hash() in implementation Should SymTable_hash() be declared in interface? Incidentally: In C any function should be either: Declared in the interface and defined as non-static, or Not declared in the interface and defined as static 28

Agenda A good module: Encapsulates data Is consistent Has a minimal interface Detects and handles/reports errors Establishes contracts Has strong cohesion (if time) Has weak coupling (if time) 29

Error Handling A well-designed module detects and handles/reports errors A module should: Detect errors Handle errors if it can; otherwise Report errors to its clients A module often cannot assume what error-handling action its clients prefer 30

Handling Errors in C C options for detecting errors if statement assert macro C options for handling errors Write message to stderr Impossible in many embedded applications Recover and proceed Sometimes impossible Abort process Often undesirable 31

Reporting Errors in C C options for reporting errors to client (calling function) Set global variable? int successful; int div(int dividend, int divisor) { if (divisor == 0) { successful = 0; return 0; } successful = 1; return dividend / divisor; } quo = div(5, 3); if (! successful) /* Handle the error */ Easy for client to forget to check Bad for multi-threaded programming 32

Reporting Errors in C C options for reporting errors to client (calling function) Use function return value? int div(int dividend, int divisor, int *quotient) { if (divisor == 0) return 0; *quotient = dividend / divisor; return 1; } successful = div(5, 3, &quo); if (! successful) /* Handle the error */ Awkward if return value has some other natural purpose 33

Reporting Errors in C C options for reporting errors to client (calling function) Use call-by-reference parameter? int div(int dividend, int divisor, int *successful) { if (divisor == 0) { *successful = 0; return 0; } *successful = 1; return dividend / divisor; } quo = div(5, 3, &successful); if (! successful) /* Handle the error */ Awkward for client; must pass additional argument 34

Reporting Errors in C C options for reporting errors to client (calling function) Call assert macro? int div(int dividend, int divisor) { assert(divisor!= 0); return dividend / divisor; } quo = div(5, 3); Asserts could be disabled Error terminates the process! 35

Reporting Errors in C C options for reporting errors to client (calling function) No option is ideal What option does Java provide? 36

User Errors Our recommendation: Distinguish between (1) User errors Errors made by human user Errors that could happen Example: Bad data in stdin Example: Too much data in stdin Example: Bad value of command-line argument Use if statement to detect Handle immediately if possible, or Report to client via return value or call-by-reference parameter Don t use global variable 37

Programmer Errors (2) Programmer errors Errors made by a programmer Errors that should never happen Example: pointer parameter should not be NULL, but is For now, use assert to detect and handle More info later in the course The distinction sometimes is unclear Example: Write to file fails because disk is full Example: Divisor argument to div() is 0 Default: user error 38

Error Handling Example 1 List typedef struct List *List_T; List_T List_new(void); void List_insert (List_T p, int key); void List_concat (List_T p, List_T q); int List_nth_key (List_T p, int n); void List_free (List_T p); add assert(p) in each of the functions... try to protect against bad clients void List_insert (List_T p, int key) { assert(p);... } 39

Error Handling Example 1 List typedef struct List *List_T; List_T List_new(void); void List_insert (List_T p, int key); void List_concat (List_T p, List_T q); int List_nth_key (List_T p, int n); Operation nth_key(p,n ), if p represents σ 1 i σ 2 where the length of σ 1 is n, returns i ; otherwise (if the length of the string represented by p is n), it returns an arbitrary integer. void List_free (List_T p); This error-handling in List_nth_key is a bit lame. How to fix it? Some choices: int List_nth_key (List_T p, int n, int *error); Or, perhaps better: add an interface function, int List_length (List_T p); and then, Operation nth_key(p,n ), if p represents σ 1 i σ 2 where the length of σ 1 is n, returns i ; otherwise (if the length of the string represented by p is n), it fails with an assertion failure or abort( ). 40

Error Handling Examples 2, 3, 4 string No error detection or handling/reporting Example: strlen() parameter is NULL seg fault (if you re lucky*) stdio Detects bad input Uses function return values to report failure Note awkwardness of scanf() Sets global variable errno to indicate reason for failure SymTable (See assignment specification for proper errors that should be detected, and how to handle them) 41

Agenda A good module: Encapsulates data Is consistent Has a minimal interface Detects and handles/reports errors Establishes contracts Has strong cohesion (if time) Has weak coupling (if time) 42

Establishing Contracts A well-designed module establishes contracts A module should establish contracts with its clients Contracts should describe what each function does, esp: Meanings of parameters Work performed Meaning of return value Side effects Why? Facilitates cooperation between multiple programmers Assigns blame to contract violators!!! If your functions have precise contracts and implement them correctly, then the bug must be in someone else s code!!! How? Comments in module interface 43

Contracts Example 1 List /* list.h */ /* Return the n th element of the list p, if it exists. Otherwise (if n is negative or >= the length of the list), abort the program. */ int List_nth_key (List_T p, int n); Comment defines contract: Meaning of function s parameters p is the list to be operated on; n is the index of an element Obligations of caller make sure n is in range; (implicit) make sure p is a valid list Work performed Return the n th element. Meaning of return value Side effects (None, by default) 44

Contracts Example 1b List /* list.h */ /* If 0 <= n < length(p), return the n th element of the list p and set success to 1. Otherwise (if n is out of range) return 0 and set success to 0. */ int List_nth_key (List_T p, int n, int *success); Comment defines contract: Meaning of function s parameters p is the list to be queried; n is the index of an element; success is an error flag Obligations of caller (implicit) make sure p is a valid List Work performed Return the n th element; set success appropriately Meaning of return value Side effects Set success 45

Contracts Examples 2, 3, 4 string See descriptions in man pages stdio See descriptions in man pages SymTable See descriptions in assignment specification 46

Agenda A good module: Encapsulates data Is consistent Has a minimal interface Detects and handles/reports errors Establishes contracts Has strong cohesion (if time) Has weak coupling (if time) 47

Strong Cohesion A well-designed module has strong cohesion A module s functions should be strongly related to each other Why? Strong cohesion facilitates abstraction 48

Strong Cohesion Examples List (+) All functions are related to the encapsulated data string (+) Most functions are related to string handling (-) Some functions are not related to string handling: memcpy(), memcmp(), (+) But those functions are similar to string-handling functions stdio (+) Most functions are related to I/O (-) Some functions don t do I/O: sprintf(), sscanf() (+) But those functions are similar to I/O functions SymTable (+) All functions are related to the encapsulated data 49

Agenda A good module: Encapsulates data Is consistent Has a minimal interface Detects and handles/reports errors Establishes contracts Has strong cohesion (if time) Has weak coupling (if time) 50

Weak Coupling A well-designed module has weak coupling Module should be weakly connected to other modules in program Interaction within modules should be more intense than interaction among modules Why? Theoretical observations Maintenance: Weak coupling makes program easier to modify Reuse: Weak coupling facilitates reuse of modules Why? Empirical evidence Empirically, modules that are weakly coupled have fewer bugs Examples (different from previous) 51

Weak Coupling Example 1 Design-time coupling Function call Airplane Airplane getlat() Simulator f() move() getlat() getlon() getalt() setlat() Simulator f() getlon() getalt() setlat() setlon() setlon() setalt() setalt() move() Simulator module calls many functions in Airplane Strong design-time coupling Simulator module calls few functions in Airplane Weak design-time coupling 52

Weak Coupling Example 2 Run-time coupling Many function calls One function call Client f() sort() Collection getn() setn() Client f() Collection getn() setn() sort() Client module makes many calls to Collection module Strong run-time coupling Client module makes few calls to Collection module Weak run-time coupling 53

Weak Coupling Example 3 Maintenance-time coupling Changed together often Client MyModule Client MyModule f1() f1() f2() f2() f3() f3() Maintenance programmer changes Client and MyModule together frequently Strong maintenance-time coupling Maintenance programmer changes Client and MyModule together infrequently Weak maintenance-time coupling 54

Achieving Weak Coupling Achieving weak coupling could involve refactoring code: Move code from client to module (shown) Move code from module to client (not shown) Move code from client and module to a new module (not shown) 55

Summary A good module: Encapsulates data Is consistent Has a minimal interface Detects and handles/reports errors Establishes contracts Has strong cohesion Has weak coupling 56