1 / 37

From Modules to Objects

From Modules to Objects. Professor James Landay CS169: Software Engineering Spring 2001 February 14, 2001. Outline. Review Modules Cohesion Administrivia Coupling Encapsulation & ADTs. Review. Testing doesn’t show the absence of bugs Major assumptions of testing

craigk
Download Presentation

From Modules to Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. From Modules to Objects Professor James Landay CS169: Software Engineering Spring 2001 February 14, 2001

  2. Outline • Review • Modules • Cohesion • Administrivia • Coupling • Encapsulation & ADTs

  3. Review • Testing doesn’t show the absence of bugs • Major assumptions of testing • inference from behavior, known environment, choosing the right test cases • Why shouldn’t SEs do all their own testing? • What to test? • utility, reliability, robustness, performance, correctness • Why is correctness not enough? • the spec might be bad! • When are correctness proofs useful? • if the benefits outweigh the costs (lives, space, etc.)

  4. Modules • What is a module? • lexically contiguous sequence of program statements, bounded by boundary elements, with aggregate identifier • Examples • procedures & functions in classical PLs • objects & methods within objects in OO PLs

  5. i) CPU using 3 chips (modules) ii) CPU using 3 chips (modules) Good vs. Bad Modules • Modules in themselves are not “good” • Must design them to have good properties What is the problem here?

  6. Good vs. Bad Modules • Two designs functionally equivalent, but the 2nd is • hard to understand • hard to locate faults • difficult to extend or enhance • cannot be reused in another product. Implication? • -> expensive to perform maintenance • Good modules must be like the 1st design • maximal relationships within modules (cohesion) • minimal relationships between modules (coupling) • this is the main contribution of structured design

  7. Objects with high cohesion and low coupling Ý Objects Ý Information hiding Ý Abstract data types Ý Data encapsulation Ý Modules with high cohesion and low coupling Ý Modules Modules to Objects

  8. Cohesion ? • Degree of interaction within module • Seven levels of cohesion • 7. Functional | Informational (best) • 5. Communicational • 4. Procedural • 3. Temporal • 2. Logical • 1. Coincidental (worst)

  9. 1. Coincidental Cohesion • Def. ? • module performs multiple, completely unrelated actions • Example • module prints next line, reverses the characters of the 2nd argument, adds 7 to 3rd argument • How could this happen? • hard organizational rules about module size • Why is this bad? • degrades maintainability & modules are not reusable • Easy to fix. How? • break into separate modules each performing one task

  10. 2. Logical Cohesion • Def. • module performs series of related actions, one of which is selected by calling module • Example function code = 7; new operation (op code, dummy 1, dummy 2, dummy 3); // dummy 1, dummy 2, and dummy 3 are dummy variables, // not used if function code is equal to 7 • Why is this bad? • interface difficult to understand • code for more than one action may be intertwined • difficult to reuse

  11. 3. Temporal Cohesion • Def. ? • module performs series of actions related in time • Initialization example open old db, new db, transaction db, print db, initialize sales district table, read first transaction record, read first old db record • Why is this bad? • actions weakly related to one another, but strongly related to actions in other modules • code spread out -> not maintainable or reusable • Initialization example fix • define these intializers in the proper modules & then have an initialization module call each

  12. 4. Procedural Cohesion • Def. ? • module performs series of actions related by procedure to be followed by product • Example • update part number and update repair record in master db • Why is this bad? • actions are still weakly related to one another • not reusable • Solution • break up!

  13. 5. Communicational Cohesion • Def. • module performs series of actions related by procedure to be followed by product, but in addition all the actions operate on same data • Example 1 update record in db and write it to audit trail • Example 2 calculate new coordinates and send them to window • Why is this bad? • still leads to less reusability -> break it up

  14. 7. Informational Cohesion • Def. • module performs a number of actions, each with its own entry point, with independent code for each action, all performed on the same data structure This is an ADT!

  15. 7. Functional Cohesion • Def. • module performs exactly one action • Examples • get temperature of furnace • compute orbital of electron • calculate sales commission • Why is this good? • more reusable • corrective maintenance easier • fault isolation • reduced regression faults • easier to extend product

  16. Coupling ? • Degree of interaction between two modules • Five levels of coupling • 5. Data (best) • 4. Stamp • 3. Control • 2. Common • 1. Content (worst)

  17. 1. Content Coupling • Def. • one module directly references contents of the other • Example • module a modifies statements of module b • module a refers to local data of module b in terms of some numerical displacement within b • module a branches into local label of module b • Why is this bad? • almost any change to b requires changes to a

  18. 2. Common Coupling • Def. • two modules have write access to the same global data • Example • two modules have access to same database, and can both read and write same record • use of Java public statement • Why is this bad? • resulting code is unreadable • modules can have side-effects • must read entire module to understand • difficult to reuse • module exposed to more data than necessary • while (global variable == 0) • if (argument xyz > 25) • module 3 (); • else • module 4 ();

  19. 3. Control Coupling • Def. • one module passes an element of control to the other • Example • control-switch passed as an argument • Why is this bad? • modules are not independent • module b must know the internal structure of module a • affects reusability

  20. 4. Stamp Coupling • Def. • data structure is passed as parameter, but called module operates on only some of individual components • Example calculate withholding (employee record) • Why is this bad? • affects understanding • not clear, without reading entire module, which fields of record are accessed or changed • unlikely to be reusable • other products have to use the same higher level data structures • passes more data than necessary • e.g., uncontrolled data access can lead to computer crime

  21. 5. Data Coupling • Def. • every argument is either a simple argument or a data structure in which all elements are used by the called module • Example display time of arrival (flight number) get job with highest priority (job queue) • Slippery slope between stamp & data (see queue) • Why is this good? • maintenance is easier • good design has high cohesion & weak coupling

  22. Objects with high cohesion and low coupling Ý Objects Ý Information hiding Ý Abstract data types Ý Data encapsulation Ý Modules with high cohesion and low coupling Ý Modules Modules to Objects

  23. Data Encapsulation ? • Def. • data structure together with actions to be performed on that structure • Bad job queue example • define job queue w/ 1 op together in module (repeat for init, add, etc.) • low cohesion • Better job queue example • put all job queue ops together w/ 1 definition of job queue in 1 module • informational cohesion & data encapsulation • Abstraction or stepwise refinement • easier development & maintenance • helps us cope w/ change

  24. Abstract Data Types • Def. • data type together with actions to be performed on instantiations of that data type • subtle difference with encapsulation • Example class JobQueue { publicint queueLength; // length of job queue publicint queue = new int[25]; // up to 25 jobs   // methods publicvoid initializeJobQueue (){ // body of method } publicvoid addJobToQueue (int jobNumber){ // body of method } } // JobQueue

  25. Information Hiding • Really “details hiding” • hiding implementation details, not information • Example • design modules so that items likely to change are hidden • future change localized • change cannot affect other modules • data abstraction • designer thinks at level of ADT

  26. Information hiding • Example class JobQueue { privateint queueLength; // length of job queue privateint queue = new int[25]; // up to 25 jobs   // methods publicvoid initializeJobQueue (){ // body of method } publicvoid addJobToQueue (int jobNumber){ // body of method } } // JobQueue • Now queue and queueLength are inaccessible

  27. Objects • Def. • class – abstract data type which supports inheritance • objects are instantiations of classes • Inheritance • new data types defined as extensions to previously defined types • don’t have to define from scratch -> provides more data abstraction • Example Define humanBeing to be a class A humanBeing has attributes, such as age, height, gender Assign values to attributes when describing object Define parent to be a subclass of humanBeing A parent has all attributes of humanBeing, plus attributes of his/her own (name of oldest child, number of children) A parent inherits all attributes of humanBeing

  28. HumanBeing (base class) inherits from (“ isA ”) Parent derived (derived class) part incremental part Inheritance (UML) UML notation: boxes represent classes open triangle represent inheritance

  29. Inheritance (Java) class HumanBeing { privateint age; private float height; public // declarations of operations on HumanBeing } // class HumanBeing class Parent extends HumanBeing { privateString nameOfOldestChild; private int numberOfChildren; public // declarations of operations on Parent } // class Parent

  30. Inheritance • Method Foo (b : Base) can be applied to objects of any subclass of Base

  31. PersonalComputer Printer CPU Monitor Keyboard Aggregation (UML) UML notation: diamond (1 or more) no diamond (1) Aggregation refers to the components of a class - used to group related items - each of these (CPU, etc.) would be instance var

  32. function draw_rectangle function draw_circle function draw_line Polymorphism & Dynamic Binding • Classical paradigm • must explicitly invoke the correct version

  33. GraphObjClass method draw LineClass CircleClass RectClass Implementation of Implementation of Implementation of method draw method draw method draw for a rectangle for a circle for a line Polymorphism & Dynamic Binding • Object-oriented paradigm • all that is needed is myGraphObj.draw() • Dynamic binding • correct method invoked at run-time (dynamically) • Polymorphic • method draw can be applied to objects of different classes

  34. Polymorphism & Dynamic Binding • Advantages ? • can build for the future • easy to add a new shape w/o changing previous code • don’t ever want to mess w/ code that works • easy to iterate over similar but distinct objects • draw all GraphObjs • Disadvantages ? • performance (have to look things up at run time) • have to design classes better • have to understand how it will be extended over time • harder to understand the code • must understand multiple possibilities for specific method

  35. Advantages of Objects • Same as abstract data types • information hiding • data abstraction • procedural abstraction • start code design at high level & continue down levels until using primitives • Inheritance provides further data abstraction • easier and less error-prone product development • easier maintenance

  36. Summary • Good modules have strong cohesion and weak coupling • Data encapsulation, ADTs, information hiding, & objects ease maintenance & reuse • Polymorphism has advantages & disadvantages

  37. Next Time • Questions? • Friday’s lecture on Requirements • read Chapter 9 from Schach

More Related