.
- ,
Program as recipe
A program is a description of a computation
Its like a recipe
Tells out to make an output (food) From inputs (ingredients) Through a series of operations (Some of which may be other recipes)
Okay, so what are computations like?
Computation
input output
Computations have inputs and outputs
Inputs are also known as arguments or parameters Outputs are also known as results or return values
Computation as call and response
input output
Procedures are called with their inputs And respond with their return values
Computation as transformation
input output
Its often useful to think of the computation as transforming
the input into the output
Photoshop turns images into brighter/darker/funkier images
WinAmp turns the CD representation of sound (sound amplitudes)
into the MP3 representation of sound (sound spectra)
Computation as transformation
Many computations are cascaded transforms Netscape turns
A mouse click into A link to follow, into An HTML file, into Colors for the pixels on the screen
Objects
We said a computers memory is ultimately implemented as binary text
In practice, that text is really a bunch of separate
representations of different pieces of data
Each of those representations of a piece of data is known as an object
Everything is an object
So from now on, everything in your computers memory is an object
Primitive (indivisible) data objects Numbers (1, 7 -3.5) Text strings (bla bla bla) Procedures Compound data objects Tables of data Lists of objects Bitmaps (contain many little color objects, one per pixel) Sounds (contain 1 number per sound sample)
Objects and computation
When we do computation, we traffic in objects
Objects are the inputs to procedures Objects are the outputs of procedures
Data types
Object
Procedure
Number Boolean String Exception Picture Color Point
ArgumentCountException ArgumentTypeException
Integer
Float
Computers have a taxonomy of types of data objects Every object has a type
The objects type determines what procedures it can be used with Can add numbers, but not pictures or procedures
Procedure calls
When we run a procedure/program, we
Specify one data object for each of its inputs Receive one data object as its output What if the procedure needs to output several data objects?
This is so common, there are a lot of synonyms for it
Run: execute, call, apply, invoke Input: argument, parameter Or: pass (as in pass the procedure 2 as an argument) Output: result, return value
Data flow diagrams
Most programming languages use textual
notations to describe what procedures to call with what arguments notation called a data flow diagram
1 + 3
We will start out by using a graphical
Easy to understand what is an input to what
and whats happening to its output Doesnt scale well to complicated programs, though this is
concat
this is a test
a test
Were using this as an informal learning tool:
dont get hung up on trying to memorize the details of it
Changing the inputs changes the output
woof woof
prefix
woof
4 woof woof
prefix
woo
Chaining calls
1 + 2 4 3 this
concat
this is a
concat
is a string
this is a string
In functional programming, programs/procedures are built
up by chaining together calls of existing procedures
The output of one becomes the input of another
Data flows through the chain, from left to right
Hence the name data flow diagram
Chaining chains
this
concat
this is a
concat
is a tedious
this is a tedious
concat
this is a tedious example of chaining
example of
concat
example of chaining
chaining
A single object can be used as input to many procedures
to be
concat
to be or not
concat
to be or not to be
or not
Order of inputs (usually) matters
this is
concat
this is a test
a test
a test
concat
a testthis is
this is
Different diagrams can produce the same result
this
concat
this is a
concat
is a string
this is a string
this is a
concat concat
this is a string
is a string
string
What about this one?
Gives the first however many characters of the input string woof woof
prefix
woof
woof woof
length prefix
woof
??? 5
How about this one?
woof woof
length prefix
Exceptions
woof woof
length prefix
ArgumentCountException
5
Procedures fail if theres something wrong with their inputs They signal an exceptional condition
Or more commonly: throw an exception
This typically stops the program
And places you in a debugger program to let you find out whats happening
In this case, the problem is that there are too few arguments (inputs), so we get an argument count exception
What about this one?
4
concat
What about this one?
4
concat
ArgumentTypeException
The most common kind of problem with a procedure call is sending the wrong type (kind) of input to a procedure Each input needs to have the right type of data Sending something the wrong kind of input will produce an argument type exception
What about this one?
length
4 woof woof
prefix
What about this one?
length
4 woof woof
prefix
ArgumentTypeException
One of the reasons type errors are so common is that its
easy for other kinds of mistakes to trigger them In this case, we switched the order of the inputs to prefix
Prefix really wants the first argument to be a string, not a number And it isnt smart enough to realize it just needs to swap the two
arguments
The call procedure
1 + 3 1 +
call
Static checkers are too complicated to use as an example,
though So lets use a simpler example: a procedure that calls other procedures
Inputs: the procedure and its arguments Output: the output of the call to the input procedure
Big ideas
Data consists of objects Objects come in different types
Determines what operations can be performed on it
Inputs and outputs of procedures are objects Most computation is done by chaining procedure calls Procedures need
The right number of inputs With the right types In the right order Otherwise, it generates an error called an exception
Procedures are also data objects
They can be passed as inputs or returned as outputs
Skills to build
Look at a data flow diagram and: Understand what the type of each object is Recognize when a procedure has
An input is of the wrong type
The wrong number of inputs
Inputs in the wrong order
Which of these has problems?
1 + 2 4 + 1 2 +
call
+
+ 1 1 1 2
call +
Which of these has problems?
1 + 2 4 + 1 2 +
call
+
+ 1 1 BadProcedureException (1 isnt a procedure) 1 2
call +
Which of these has problems?
woof woof
+ 1 2 5
call call
length prefix
+ 1
call
+
call
1 +
call
Which of these has problems?
IndexOutOfRangeException
woof woof
+ 1 2
call
(string doesnt have 14 characters)
length
3
call
9 +
prefix
BadProcedureException (3 isnt a procedure)
14
+ 1
call
+
call
1 +
call
Where do they fit in?
Life Cycle Phases
Planning Feasibility Study
Analysis (What do we do?) Fact finding
Analysis
Design Code and Unit test
investigate business process and the current system
modelling the current
and required systems deliverables
requirements specification logical models of the required system
Data Flow Diagrams (DFD)
DFDs describe the flow of data or information into
and out of a system
what does the system do to the data?
A DFD is a graphic representation of the flow of data
or information through a system
4 Main Elements
external entity - people or organisations that send data into the system or receive data from the system
process - models what happens to the data i.e. transforms incoming data into outgoing data data store - represents permanent data that is used by the system data flow - models the actual flow of the data between the other elements
Notation
Data Flow
Process External Entity
Data Flow
Process box
External Entity D Data Store
Data Store
Levelled DFDs
Even a small system could have many processes and
data flows and DFD could be large and messy
use levelled DFDs - view system at different levels of
detail one overview and many progressively greater detailed views
Level 0 - Context Diagram
models system as one process box which represents
scope of the system identifies external entities and related inputs and outputs Additional notation - system box
External entity
Data flow out Data flow in
System box
Level 1 - overview diagram
gives overview of full system
identifies major processes and data flows between
them identifies data stores that are used by the major processes boundary of level 1 is the context diagram
Level 2 - detailed diagram
level 1 process is expanded into more detail
each process in level 1 is decomposed to show its
constituent processes boundary of level 2 is the level 1 process
Figure 8-4 Context diagram of Hoosier Burgers Food ordering system
8.41
Level-0 DFD of Hoosier Burgers food ordering system
8.42
Joes Yard
Joes builders suppliers has a shop and a yard. His system is entirely manual. He has a stock list on the wall of his shop, complete with prices. When a builder wants to buy supplies, he goes into the shop and picks the stock items from the list. He writes his order on a duplicate docket and pays Joe, who stamps the docket as paid. The builder takes the duplicate docket and he goes to the yard and hands it to the yard foreman. The yard foreman gets the ordered items from the yard and gives them to the builder. The builder signs the duplicate docket and leaves one copy with the foreman and takes one copy as a receipt. Every week, Joe looks around the yard to see if any of his stock is running low. He rings up the relevant suppliers and reorders stock. He records the order in his order book, which is kept in the yard. The yard foreman takes delivery of the new stock and checks it against what has been ordered. He pays for it on delivery and staples the receipt into the order book. At the end of every month, Joe goes through all the dockets and the order book and produces a financial report for the shareholders. Draw a context level DFD and a level-1 DFD for this system.
Context Diagram
Find the people who send data into the system
Often data is part of a PHYSICAL transaction When handing a bar of chocolate to a shopkeeper, you ar
e handing him/her a barcode.
Find the people who get data out of the system.
The only data you need is data that is transformed or sen
t completely out of the system not data that is handled by an operator within the system.
Joes Yard
Joes builders suppliers has a shop and a yard. His system is entirely manual. He has a stock list on the wall of his shop, complete with prices. When a builder wants to buy supplies, he goes into the shop and picks the stock items from the list. He writes his order on a duplicate docket and pays Joe, who stamps the docket as paid. The builder takes the duplicate docket and he goes to the yard and hands it to the yard foreman. The yard foreman gets the ordered items from the yard and gives them to the builder. The builder signs the duplicate docket and leaves one copy with the foreman and takes one copy as a receipt. Every week, Joe looks around the yard to see if any of his stock is running low. He rings up the relevant suppliers and reorders stock. He records the order in his order book, which is kept in the yard. The yard foreman takes delivery of the new stock and checks it against what has been ordered. He pays for it on delivery and staples the receipt into the order book. At the end of every month, Joe goes through all the dockets and the order book and produces a financial report for the shareholders. Draw a context level DFD and a level-1 DFD for this system.
Context diagram
Shareholders financial report
Docket & Payment
Supply needs Joe
Joe's Yard Customer Signed docket
Supply order & payment
Supply invoice
Supplier
Level-1 DFD processes
Joes builders suppliers has a shop and a yard. His system is entirely manual. He has a stock list on the wall of his shop, complete with prices. When a builder wants to buy supplies, he goes into the shop and picks the stock items from the list. He writes his order on a duplicate docket and pays Joe, who stamps the docket as paid. The builder takes the duplicate docket and he goes to the yard and hands it to the yard foreman. The yard foreman gets the ordered items from the yard and gives them to the builder. The builder signs the duplicate docket and leaves one copy with the foreman and takes one copy as a receipt. Every week, Joe looks around the yard to see if any of his stock is running low. He rings up the relevant suppliers and reorders stock. He records the order in his order book, which is kept in the yard. The yard foreman takes delivery of the new stock and checks it against what has been ordered. He pays for it on delivery and staples the receipt into the order book. At the end of every month, Joe goes through all the dockets and the order book and produces a financial report for the shareholders.
Verbs from script
Has (passive) Buy supplies Picks stock items Writes order Pays joe Stamps docket Takes docket to yard Hands it to foreman Gets items Gives them to builder
Builder signs docket Takes copy as receipt Looks around yard and reorders Records order in order book Foreman takes delivery checks Foreman pays supplier Staples receipt to order book Produces financial report
Remove passive verbs and queries
Passive: has stock list Buy supplies
Picks stock items (views list) Writes orders Pays joe Stamps docket
Joe then
Looks around yard and reorders Records order in order book
Foreman
takes delivery checks Foreman pays supplier Staples receipt to order book
Customer then
Takes docket to yard Hands it to foreman
Joe
Produces financial report
Gets items
Gives them to builder Builder signs docket Takes copy as receipt
Level 1 current physical
20 Buy supplies Docket & Payment Customer Money Payment Payment Restock Docket M1 Docket supplies Supply order M3 stock 12 builder signature 11 Take to yard Supply order get items required stock completed docket copy Supplier receipt Joe 21 Reorder supplies Signed docket 5 Joe's Office Produce financial report Foreman financial report * Shareholders Supply needs Order book 22 Supplier receipt Payment supplies Supplier completed docket copy required stock Signed docket
completed docket copy
Signed docket
Buy Supplies
20 Buy supplies Customer Docket 20.1 Docket Writes Order * Docket 20.3 Stamp Docket(signature) 20.2 Pays Joe * Payment Payment
M1
Docket
* Money
Get Items
12 get items 12.2 Foreman Signed docket 12.1 Give items to customer * required stock stock 12.3 Give copy as receipt completed docket copy Get builder signature * Signed docket builder signature Customer completed docket copy
Reorder supplies
21 Reorder supplies 21.1 Joe Supply needs Reorder from yard *
Supply needs 21.2 Record order * Supply order M3 Order book
Restock
22 Restock M3 Order book Supply order 22.1 supplies Take delivery * Supply order Money Payment 22.2 Pay supplier * Supplier receipt Payment Supplier supplies stock