Hierarchy and Modular Programming
Hierarchy and Modular Programming
Modularity
(real problems are always too big for your
head)
(some material due to Zhiao Shi, ACCRE)
• First refinement
Initialise variables
• Repeat refining…
A second example
• divides into
Initialize passes to zero
Initialize failures to zero
Initialize student counter to one
A second example
• divides into
While student counter is <= ten
Input the next exam result
If the student passed
Add one to passes
else
Add one to failures
Add one to student counter
A second example
• divides into
Print the number of passes
Print the number of failures
If more than eight students passed
Print “Make class harder”
A second example
• So the present pseudocode
No
becomes:
te
an introduction
functions =
outsourcing
• Break large computing tasks into
smaller ones - a bit like top-down
refinement.
• But, importantly, the details of how
each section works is shuffled off to
another part of the file, so that you
can see the flow of the overall
program more clearly.
• The main program then becomes a
series of calls to
functions - why?
• promotes modular and ‘top-down’
coding
• promotes ‘standard libraries’ from
colleagues, or experts from rest of
world
• hides implementation - makes the
important points of the code more
clear
• makes code change/debugging
easier
functions - why?
• you have already seen this done !
• print(“hello world”)
• for example :
def cat_age(human_age):
"""Translate human to cat ages."""
multiplier = 4
if (human_age==1): return 15
if (human_age==2): return 24
return human_age*multiplier + 16
functions - calling
• functions are defined before use,
and then called by :
...
my_age = 46
cat_age(12) # pointless, ignores answer
felix_age = cat_age(4) # felix_age now 32
print(cat_age(12) # output ancient!
...
or,
import numpy as np
dir(np)
# list of 'names'
# (functions, variables, classes)
help(np)
# help pages - but too long!
Object Oriented
Programming
the more modern way to partition a program
DigitalOut led1(LED1); in
te
int main() { rf
while (true) { ac
led1 = !led1; e
// or led1.write( !led1.read() );
wait(0.5);
}
}
cl ob
as je
s c
Example you've
seen
interface
creation stuff
function like
interface
operator like
interface
OOP
• Using objects is usually a doddle,
especially if some other expert
coder made them.
• A useful object is a pleasure to use
again...
• Making objects is harder. Good
design is never easy.
• Firstly we'll use objects 🙂,only later
will we try writing them.
OOP structure...
• definition :
class Car():
"""Model a Car."""
def __init__(self, make, model):
"""Initialise"""
self.make = make
self.model = model
self.tank_cap = 60 #60 litre tank
self.tank = 0 #initial fuel fill
def fillup(self):
"""fill tank to brim"""
self.tank = self.tank_cap
print("Full!")
print(myCar.make)
print(myCar.model)
myCar.fillup()
print(myOtherCar.make)
print(myOtherCar.model)
OOP: back to
numpy
• carefully only mentioned functions
last time
import numpy as np
two_d = np.array([(1,2,3),(4,5,6),(7,8,9)])
# create a 2D array