Lecture #11: Modularization Reading: none Handouts: Coupling and Cohesion 1. Greetings and felicitations 2. Interface € Where do you split things up? € look for parts of problems and places where higher-level stuff need not know about lower-level stuff ex. database program for addresses € look for places where program is likely to change ex. machine-dependent code, organization-dependent code (use news, i/o buffers as examples) Then hide these using an interface which passes minimum, as few globals as possible € MAXIMUZE COHESION, MINIMIZE COUPLING (do handout) € ADVICE: KISS; "everything should be made as simple as possible, but no simpler" 3. Modularity: how do you break things up? … do "stack" as a module … show breakdown: what are operations? … what is data structure? … Coupling and Cohesion This handout presents the definitions of coupling and cohesion. Levels of Coupling best 5 . Data Parameter lists. 4 . Stamp ³Named common,² that is, public variables shared only by those procedures that need to use them. 3 . Control Flags passed between procedures to indicate what sections of code they should execute (what functions they should perform). 2 . Common All procedures access all public variables. worst 1 . Content Procedures modify each others¹ internal variables or code. (Assembly language). Nowadays, a combination of Data Coupling and Stamp Coupling is considered the best solution to medium-sized programming problems: Stamp Coupling is used to create shared data structures for abstract data types, and Data Coupling for everything else. In some rare instances, Stamp Coupling may be used in place of Data Coupling when efficiency problems make parameter passing too slow or large. This is, however, a rather unsatisfactory state of affairs. Levels of Cohesion best 6 . Functional A procedure that accomplishes one clear function. Examples: a quicksort, a square root routine, a roman- numeral evaluator. Informational A group of procedures that work on the same data structure. Example: a module that implements an abstract data type. 5 . Communicational A series of operations as in procedural cohesion ‹ but applied to the same data. Example: a procedure that reads the input stream and sorts it. 4 . Procedural A series of operations that have something to do with each other, in terms of the overall problem being solved. Example: a procedure that reads the input stream, breaks it into roman numerals, and sorts the roman numerals. 3 . Temporal A series of operations related in time (only). Common examples: a procedure that initializes all data structures used in a program, one that opens all files, etc. These operations typically have rather little to do with each other, but have strong connections with other parts of the program. (For example, a file must be opened here before it can be used in other parts of the program.) 2 . Logical A set of related operations, where the caller chooses which to invoke. Example: edit(op, string1, string2) where op == 1 means replace, op == 2 means delete, and so forth. Historically this has sometimes been the poor man¹s form of Informational Cohesion but is no longer necessary. worst 1 . Coincidental No particular relation among operations. Common example: a large program arbitrarily hacked into ³modular² sections. Lecture Notes ECS 40 ­ FALL 1997 Page 1 Coupling and Cohesion ECS 40 ­ FALL 1997 Page 4