info4 02 modularity reusability -...
TRANSCRIPT
![Page 1: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/1.jpg)
Programming in the large - Lecture 2
1
Chair of Software Engineering
Programming in the large
Bertrand Meyer
Last update: 16 April 2004
![Page 2: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/2.jpg)
Programming in the large - Lecture 2
2
Chair of Software Engineering
Lecture 2: Modularity, Reusability
![Page 3: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/3.jpg)
Programming in the large - Lecture 2
3
Chair of Software Engineering
Agenda for today
Software qualityModularityReusability
![Page 4: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/4.jpg)
Programming in the large - Lecture 2
4
Chair of Software Engineering
Software quality
External factors: visible to customers
(not just end users but e.g. purchasers)
Examples: ease of use, extendibility
Internal factors: perceptible only to developers
Examples: good programming style, information hiding
Only external factors count in the end, but the internal factors make it possible to obtain them.
![Page 5: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/5.jpg)
Programming in the large - Lecture 2
5
Chair of Software Engineering
External quality factors
CORRECTNESSROBUSTNESSINTEGRITY
EASE OF USEREUSABILITYEXTENDIBILITYPORTABILITYEFFICIENCY…
Correctness:The ability of a software system to perform according to specification, in cases defined by the specification.
Robustness:The ability of a software system to react in a reasonable manner to cases not covered by the specification.
Correctness
RobustnessIntegrity
HOSTILE USEERRORSSPECIFICATION
![Page 6: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/6.jpg)
Programming in the large - Lecture 2
6
Chair of Software Engineering
Reliability
Correctness + Robustness
Techniques will be studied in detail: typing, Design by Contract, …
![Page 7: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/7.jpg)
Programming in the large - Lecture 2
7
Chair of Software Engineering
Modularity
Reusability + Extendibility
Favored by architectural techniques tending to ensure decentralization of modules
![Page 8: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/8.jpg)
Programming in the large - Lecture 2
8
Chair of Software Engineering
Modularity
Some principles of modularity:Decomposability ComposabilityContinuity Information hiding The open-closed principle The single choice principle
![Page 9: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/9.jpg)
Programming in the large - Lecture 2
9
Chair of Software Engineering
Decomposability
Method helps decompose complex problems into subproblems.
COROLLARY: Division of labor. Example: Top-down design method (see next). Counter-example: General initialization module.
![Page 10: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/10.jpg)
Programming in the large - Lecture 2
10
Chair of Software Engineering
Top-down functional design
A
B D C
C1 I1 C2 I2I
Topmost functional abstraction
Loop Conditional
Sequence
![Page 11: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/11.jpg)
Programming in the large - Lecture 2
11
Chair of Software Engineering
Top-down design
See Niklaus Wirth, “Program Construction by Stepwise Refinement”, Communications of the ACM, 14, 4, (April 1971), p 221-227.
http://www.acm.org/classics/dec95/
![Page 12: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/12.jpg)
Programming in the large - Lecture 2
12
Chair of Software Engineering
Method favors production of software elements that may be freely combined with each other to produce new software.
Example: Unix shell conventionsProgram1 | Program2 | Program3
Composability
![Page 13: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/13.jpg)
Programming in the large - Lecture 2
13
Chair of Software Engineering
Direct mapping
Method yields software systems whose modular structure remains compatible with any modular structure devised in the process of modeling the problem domain.
![Page 14: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/14.jpg)
Programming in the large - Lecture 2
14
Chair of Software Engineering
Few interfaces principle
Every module communicates with as few others as possible.
(A) (B) (C)
![Page 15: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/15.jpg)
Programming in the large - Lecture 2
15
Chair of Software Engineering
Small interfaces principle
If two modules communicate, they exchange as little information as possible.
x, y
z
![Page 16: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/16.jpg)
Programming in the large - Lecture 2
16
Chair of Software Engineering
Explicit interfaces principle
Whenever two modules A and B communicate, this is obvious from the text of A or B or both.
Module A Module B
Data item
x
Modifies Accesses
![Page 17: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/17.jpg)
Programming in the large - Lecture 2
17
Chair of Software Engineering
Continuity
Method ensures that small changes in specifications yield small changes in architecture.
Design method: Specification → Architecture
Example: Principle of Uniform Access (see next)
Counter-example: Programs with patterns after the physical implementation of data structures.
![Page 18: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/18.jpg)
Programming in the large - Lecture 2
18
Chair of Software Engineering
Uniform Access Principle
Facilities managed by a module are accessible to its clients in the same way whether implemented by computation or by storage.
Definition: A client of a module is any module that uses its facilities.
![Page 19: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/19.jpg)
Programming in the large - Lecture 2
19
Chair of Software Engineering
Uniform Access: An example
balance = list_of_deposits.total – list_of_withdrawals.total
Ada, Pascal, C/C++, Java, C#: Simula, Eiffel:a.balance a.balancebalance (a) a.balance()
list_of_deposits
list_of_withdrawals
balance
list_of_deposits
list_of_withdrawals(A2)
(A1)
![Page 20: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/20.jpg)
Programming in the large - Lecture 2
20
Chair of Software Engineering
Information hiding
Underlying question: how does one “advertise” the capabilities of a module?
Every module should be known to the outside world through an official, “public” interface. The rest of the module’s properties comprises its “secrets”. It should be impossible to access the secrets from the outside.
![Page 21: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/21.jpg)
Programming in the large - Lecture 2
21
Chair of Software Engineering
Information Hiding Principle
The designer of every module must select a subset of the module’s properties as the official information about the module, to be made available to authors of client modules.
![Page 22: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/22.jpg)
Programming in the large - Lecture 2
22
Chair of Software Engineering
Information hiding
Secret part
Public part
![Page 23: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/23.jpg)
Programming in the large - Lecture 2
23
Chair of Software Engineering
Information hiding
Justifications:ContinuityDecomposability
![Page 24: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/24.jpg)
Programming in the large - Lecture 2
24
Chair of Software Engineering
The Open-Closed Principle
Modules should be open and closed.
Definitions:Open module: May be extended. Closed module: Usable by clients. May be approved, baselined and (if program unit) compiled.
The rationales are complementary:For closing a module (manager’s perspective): Clients need it now. For keeping modules open (developer’s perspective): One frequently overlooks aspects of the problem.
![Page 25: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/25.jpg)
Programming in the large - Lecture 2
25
Chair of Software Engineering
An object
startforth
put_right before after
item index
has an interface
![Page 26: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/26.jpg)
Programming in the large - Lecture 2
26
Chair of Software Engineering
An object
startforth
put_right before after
item index
has an implementation
![Page 27: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/27.jpg)
Programming in the large - Lecture 2
27
Chair of Software Engineering
Information hiding
startforth
put_right before after
item index
![Page 28: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/28.jpg)
Programming in the large - Lecture 2
28
Chair of Software Engineering
The Open-Closed principle
F A’
G
H I
A C E
D
B
![Page 29: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/29.jpg)
Programming in the large - Lecture 2
29
Chair of Software Engineering
Closing modules prematurely
type PUBLICATION =record
author, title: STRING;publication_year: INTEGERcase pubtype: (book, journal, conference) of
book: (publisher: STRING);journal: (editor: STRING);conference: (place, chair: STRING)
endend
Use in clients:
p: PUBLICATION ;case p.pubtype of
book: ... p.publisher ...;journal: ... p.editor ...;conference: ... p.place ...
end
![Page 30: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/30.jpg)
Programming in the large - Lecture 2
30
Chair of Software Engineering
The Single Choice principle
Whenever a software system must support a set of alternatives, one and only one module in the system should know their exhaustive list.
Editor: set of commands (insert, delete etc.) Graphics system: set of figure types (rectangle, circle etc.) Compiler: set of language constructs (instruction, loop, expression etc.)
![Page 31: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/31.jpg)
Programming in the large - Lecture 2
31
Chair of Software Engineering
Reusability issues
Organizational and managerial issues:(Not covered here.)
Technical issues: what form of components? Routine librariesPackages (Ada)Class librariesWhat form of classes?
![Page 32: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/32.jpg)
Programming in the large - Lecture 2
32
Chair of Software Engineering
Reusability: Technical issues
The general pattern for a searching routine:
has (t: TABLE; x: ELEMENT): BOOLEAN is-- Does item x appear in table t?
localpos: POSITION
dofrom
pos := initial_position (t, x) until
exhausted (t, pos) or else found (t, x, pos)loop
pos := next (t, x, pos)endResult := found (t, x, pos)
end
![Page 33: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/33.jpg)
Programming in the large - Lecture 2
33
Chair of Software Engineering
Issues for a general searching module
Type variation:What are the table elements?
Routine grouping:A searching routine is not enough: it should be coupled with routines for table creation, insertion, deletion etc.
Implementation variation:Many possible choices of data structures and algorithms: sequential table (sorted or unsorted), array, binary search tree, file, ...
![Page 34: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/34.jpg)
Programming in the large - Lecture 2
34
Chair of Software Engineering
Issues
Representation independence:
Can a client request an operation such as table search (has) without knowing what implementation is used internally?
has (t1, y)
![Page 35: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/35.jpg)
Programming in the large - Lecture 2
35
Chair of Software Engineering
Issues
Factoring out commonality:How can the author of supplier modules take advantage of commonality within a subset of the possible implementations?
Example: the set of sequential table implementations. A common routine text for has:
has (....; x: T): BOOLEAN is-- Does x appear in the table?
dofrom start until after or else found (x) loop
forthendResult := found (x)
end
![Page 36: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/36.jpg)
Programming in the large - Lecture 2
36
Chair of Software Engineering
Factoring out commonality
1
item
index
forthcount
start
after
back
before
![Page 37: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/37.jpg)
Programming in the large - Lecture 2
37
Chair of Software Engineering
Factoring out commonality
TABLE
SEQUENTIAL_TABLE
TREE_TABLE
HASH_TABLE
ARRAY_TABLE
LINKED_TABLE
FILE_TABLE
has
startafterfoundforth
![Page 38: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/38.jpg)
Programming in the large - Lecture 2
38
Chair of Software Engineering
Implementation variants
Array
Linked list
File
start forth after found (x)
c := first_cell
rewind
i := 1
c := c.right
i := i + 1
read end_of_file
c = Void
f = ξ
c.item = x
i > count t [i] = x
![Page 39: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/39.jpg)
Programming in the large - Lecture 2
39
Chair of Software Engineering
Encapsulation languages (“Object-based”)
Ada, Modula-2, CLU...
Basic idea: gather a group of routines serving a related purpose, such as has, insert, remove etc., together with the appropriate data structure descriptions.
This addresses the Related Routines issue.
Advantages:
For supplier author: Get everything under one roof. Simplifies configuration management, change of implementation, addition of new primitives.
For client author: Find everything at one place. Simplifies search for existing routines, requests for extensions.
![Page 40: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/40.jpg)
Programming in the large - Lecture 2
40
Chair of Software Engineering
Complementary material
OOSC2:Chapter 3: ModularityChapter 4: Approaches to reusability
![Page 41: info4 02 modularity reusability - se.inf.ethz.chse.inf.ethz.ch/.../ss2004/0004/slides/02_modularity_reusability_1up.pdf · Programming in the large - Lecture 2 5 Chair of Software](https://reader035.vdocuments.site/reader035/viewer/2022063018/5fdc92a9108c1d38f2251eab/html5/thumbnails/41.jpg)
Programming in the large - Lecture 2
41
Chair of Software Engineering
End of lecture 2