You are on page 1of 21

The Art of Programming

Program design and


implementation
I have a computational problem.
What should I do first ?

1. Go straight to the keyboard and start


typing. 
2. Think about the problem, design on
paper the structure of the program,
then start typing.

? 3. Check that someone else hasn’t


already done it! *
*(and if they have, it doesn’t cost too much)

Writing a program – design,
strategies and implementation
1. Analysing the problem
 What do I need to do?
2. Unstructured programming
3. Program design with structured programming
 pseudo code
 program blocks and flow-schemes
4. Object-oriented programming (OOP) for
complex projects
 defining the problem in terms of objects
Problem analysis
• Define carefully what you want to do, writing it
down if necessary.
• Ask yourself:
– Is it feasible? (Protein folding is difficult,
particularly in Perl!)
– Has it been done before ?
• Check literature, web etc for similar work.
• Check software archives or libraries for programs that do
similar things.
Unstructured programming
All the program code written in a single continuous
main program.

Many disadvantages for large programs


• difficult to follow logic
• if something needs to be done more than
once must be re-typed
• hard to incorporate other code
• not easily modified
• difficult to test particular portions of the code
• ...
Structured programming
• Structured or procedural programming attempts
to divide the problem into smaller blocks or
procedures which interact with other.

• The aim is to clearly define the structure of the


program before writing program code. (At this
stage we could also decide to attach pre-written
libraries or other programs)
Stuctured programming
Ideally each block should be
a black box –should carry
out a well-defined task,
PROGRAM FLOW

interacting with other blocks


only through definite inputs
and outputs.

input

output
Structured programming
Strategy
1. Write pseudo-code if desired to define the
problem.
2. Decide on the program blocks, specifying
the inputs and outputs for each blocks.
3. Refine and see if the larger blocks can be
in turn sub-divided into smaller blocks.
4. Now you can start programming..just fill in
the blocks with computer code !
complex block diagrams
START
EXIT

utility routine

EXIT
Structured programming - summary

•• Structured
Structured programs
programs divide the
the problem into smaller sub-units or
blocks, then divided
divided into smaller blocks.. eventually reaching the
level of program code.
•• The blocks should ideally be self-contained – interactions with
with other
blocks should be
be explicit
•• ALL programming
programming languages
languages support Structured
Structured Programming
Programming to
some extent
•• Useful model
model for small-medium sized
sized projects.
•• Becomes
Becomes unmanageable for larger, more complex projects,
projects, with
many programmers involved → Object
Object Oriented
Oriented Programming
Programming
(OOP).
The object-oriented approach
Evolution of Program Design

Examples
1950s-
1960s “simple” program models COBOL, Algol,
BASIC

1970s Zenith of structured Pascal, C, Fortran,


-1980s programming- especially Perl
Pascal.

First object-oriented Ada, C++, JAVA,


languages (esp C++) SmallTalk

1990’s- traditional Visual Basic, Delphi


languages (Object Pascal), Perl 5,
“converted” to OOP (e.g. BioPerl)
The object-oriented approach

The program is written as a collection of


interacting objects.

Q: Why objects?
A: Because the world is made from distinct objects,
which consist of other objects, etc., and this is often
a natural and powerful way of representing a
situation or problem.
Example objects - chemistry
e le c tr o n

n e u tr o n

p r o to n

molecules .. atoms .. protons,


are made which neutrons and
of .. consist of .. electrons ..
molecules

atoms
object hierarchy
nuclei electrons

protons neutrons
OOP and structured programming
In structured programs the blocks are pieces of code which
are executed as the program is run

In object-oriented programs objects have lives of their own


– they can be created, copied, destroyed or even lost!
What is a software object?

An object has two components:


1. state information which describes the
current characteristics of the object and
2. behaviour which describes how it
interacts with other objects.
Software representation of a cat
object
STATE
STATE
•• Name
Name
•• Breed
Breed
•• Weight
Weight
•• Age
Age
•• Asleep
Asleep

BEHAVIOUR
BEHAVIOUR
•• Sleeps
Sleepsaalot
lot
•• Scratches
Scratchesfurniture
furniture
•• Catches
Catchesmice
mice
•• Fights
Fightsother
othercats
cats
Implementation of objects
State is usually held as local
variables (also called properties),
STATE quantities not visible outside the
object (data hiding)

Behaviour controlled by
method functions or
BEHAVIOUR subroutines which act on
the local variables and
interface with the outside.
Key feature of OOP - Inheritance
Important ability of any OOP is the ability to derive one
object from a more general class of related objects: this is
called inheritance.

A standard eukaryotic cell


• nucleus, cell membrane,
cytoplasm, alive or dead
• undergoes division, makes
proteins from DNA

white blood
cell
nerve cell skin cell
Examples of OO languages
• C++
– Classic example, uses C syntax
• Java
– Based on C++, often used for Web and graphics
• Visual Basic, Visual C++
– Windows programming
• Perl ?
– Possible, e.g. BioPerl, but not originally designed for
objects. Implementation is a bit ad-hoc.

Wanna know more ? For experts C++, for semi-experts Java and
Visual Basic for beginners.
OOP - Summary

 Objects provide a powerful and natural approach to
representing many problems
 Features such as inheritance allow already written
objects to be re-used – program modification easier.

 Certainly more difficult than conventional programming
 Some concepts hard, even for
for experienced
experienced programmers
programmers
 Implementation of objects often use complicated
syntax/semantics
syntax/semantics
 OOP not famous for efficiency (memory or execution
time)
 C++ once famous for being slow, now
now much better
 Java still
still famous for
for being slow
A family tree of languages

Fortran Cobol
LISP

BASIC Algol 60
PL/1
Simula ML
Algol 68
C Pascal
Scheme
Smalltalk
Dylan
Ada
C++ Perl
Modula 3 Prolog
Java
C# Python Ruby

You might also like