Lecture 2: Project Structure
In which we discuss the basic ideas of components and how to
produce good ones.
What It Does
Not the place to get clever with what you remember from
Remember instead the interfaces
What are the operations it must perform?
For a stack:
- Push: Put an item on "top" of the stack.
- Pop: Remove the item that was most recently "pushed."
Don't forget the boundary cases: What happens with an empty
Operational Semantics: A Bad Thing
Specifications are not implementations
"It does what it does" and "Just read the code" are not
answers to "Where is the spec?"
Instead of a tree being two pointers, think of the more
In fact, the latter is still executable code. However, it
divorces itself from questions like memory layout, or even
memory addresses. A full spec requires more - a prose
explanation of the intent.
A Simple Example
A Tree is a collection containing some kind of
data. A Tree is a homogenous, immutable data
structure: all data within it must be of the same type, and no
tree can be altered once created (new ones can be created from
A Tree can either be an empty collection, called a
Leaf, or a collection containing three items, called
an InternalNode. The three items in an
InternalNode are as follows:
- A single datum of an appropriate type.
- Two Tree's. The first is usually called the left
subtree, while the second is called the right
subtree. Either subtree can be a Leaf or an
InternalNode-the InternalNode containing
the subtrees does not need
If the type of data contained in a Tree is identified
by the type variable SomeData, then this collection can
be succinctly described with the following Haskell
data Tree SomeData =
What is a component?
Distinguished from other components
Handle to "grab on to it"
At very least, existance
Alternatives for behavior
What can it do?
What guarantees does it provide?
Programming by contract: API of a component provides
Contract: Pre-conditions and Post-conditions
Pre-condition: What must be true before this behavior can be
Post-condition: What will be true once the behavior
Some PLs have mechanisms for this
Your spec wants them whether the implementation language has
that mechanism or not
What do you do with a button?
Presence of a control affords certain actions
Good components afford only the right actions
What's a good design for a doorknob?
Round: It affords turning
But which way?
Then what? Push or pull?
What everyone says is the big deal with encapsulation
Yes, it's important: Don't look under the hood to drive the
Show only the controls you need for the task at hand
Wait, Sounds Like Affordances!
Ideal: A component can only be used correctly
Poor affordances: A flat API with constraints on the order
of the calls
Better affordances: If order is required, build it in
If unknown steps come in between, that's polymorphism....
Literally, Many Shapes
Greek roots poly and morphus
Here, being able to mix and match components
New code using old: Reusing an API
Old code using new: Polymorphism
Ad Hoc: Fitting a New Peg into an Old Hole
Start from a known "shape"
As long as something new "fits", it works
In OOP, this is base classes and inheritance
Parametric: Flexible Holes for Many Kinds of Pegs
Build a structure assuming we'll find something to fit in
No assumptions about the something
In programming languages, these are template mechanisms
And for Next Week....
Class Mailing List
Please sign up for the class mailing list.
This is the place for timely announcements, questions about
assignments, and an excellent place to put together a group
for your class projects.
Homework Assignment: man ls
Read the Unix manual page for the command /bin/ls.
- A Unix manual page is documentation for an existing
application. Explain whether this differs from a
specification. Why? This is a prose question, length
no more than one page formatted.
- Using man ls and your critique of it as a model,
write a preliminary specification for a graphical equivalent
of /bin/ls. Instead of producing a textual listing of
the contents of a directory, "gls" produces a picture showing
the contents of the directory. It need not have any
interaction beyond the level of setting options (i.e., a shell
command is satisfactory), but the options supported should
provide equivalent functionality to those of
/bin/ls. This is a prose question, length no
more than comparable to the output of man ls.
Created: Mon Sep 15 17:07:15 EDT 2002
Last modified: Mon Sep 16 14:45:26 EDT 2002