26
The Art of Programming The Art of Programming Program design and implementation

The Art of Programming Program design and implementation

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The Art of Programming Program design and implementation

The Art of ProgrammingThe Art of Programming

Program design and implementation

Page 2: The Art of Programming Program design and implementation

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

1. Go straight to the keyboard and start typing.

3. Check that someone else hasn’t already done it! *

*(and if they have, it doesn’t cost too much)??

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

Page 3: The Art of Programming Program design and implementation

Writing a program – design, Writing a program – design, strategies and implementationstrategies 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

Page 4: The Art of Programming Program design and implementation

Problem analysisProblem 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.

Apart from bioinformatic sources, the CPAN site contains an extensive list of free Perl libraries and routines: http://www.cpan.org

Page 5: The Art of Programming Program design and implementation

Unstructured programmingUnstructured programming

All the program code written in a single continuous main program.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

• ...

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

• ...

Page 6: The Art of Programming Program design and implementation

Structured programmingStructured 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)

• 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)

Page 7: The Art of Programming Program design and implementation

Stuctured programmingStuctured programming

Ideally each block should be a black box –should carry out a well-defined task, interacting with other blocks only through definite inputs and outputs.

input

output

PR

OG

RA

M F

LO

W

Page 8: The Art of Programming Program design and implementation

Structured programmingStructured programming

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 !

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 !

StrategyStrategy

Page 9: The Art of Programming Program design and implementation

Structured Programming - ExampleStructured Programming - Example

Problem: From a FASTA file, extract the DNA sequence data and translate in all six reading frames.

Problem: From a FASTA file, extract the DNA sequence data and translate in all six reading frames.

Page 10: The Art of Programming Program design and implementation

DNA translation – pseudo codeDNA translation – pseudo code

• read sequence data from file– read what is in file– extract sequence data (i.e. remove

FASTA headers and comments)

• for each reading frame– translate DNA to peptide

– loop over codons and translate each codon to aminoacid or stop

• print results

• read sequence data from file– read what is in file– extract sequence data (i.e. remove

FASTA headers and comments)

• for each reading frame– translate DNA to peptide

– loop over codons and translate each codon to aminoacid or stop

• print results

Page 11: The Art of Programming Program design and implementation

DNA Translation -Block diagramDNA Translation -Block diagram

get sequence data from file

translate each OF to protein

print results

read file

extract sequence

select codon

translate to aa

loop over codons

loop over OFs

Page 12: The Art of Programming Program design and implementation

Each box has well-defined input and outputs Each box has well-defined input and outputs – information should only pass through these – information should only pass through these pointspoints

translate each OF to protein

CCGGTAGCCTCCAGGTC..

PVTPSELPRPRRPLPTQQQPQ..

DNA input

protein output

Page 13: The Art of Programming Program design and implementation

Implementing structural designImplementing structural design

• In terms of program code, there is usually a main program and calls are made to the routine from this loop (which may call other routines, etc.)

• After each routine has finished the computer returns to the point right after the call.

• In terms of program code, there is usually a main program and calls are made to the routine from this loop (which may call other routines, etc.)

• After each routine has finished the computer returns to the point right after the call.

Page 14: The Art of Programming Program design and implementation

DNA translation – advanced DNA translation – advanced pseudo codepseudo code# NOT VALID PERL

# main programcall get sequence

loop over OFscall convertDNA

end loop

call results

end program

# NOT VALID PERL

# main programcall get sequence

loop over OFscall convertDNA

end loop

call results

end program

get sequence

call read data

call del_headers

get sequence

call read data

call del_headers

convertDNA

loop over codons

call tr_codon

end loop

convertDNA

loop over codons

call tr_codon

end loop

results

printresults

results

printresults

read_dataread_data

del headersdel headers

tr_codontr_codon

Page 15: The Art of Programming Program design and implementation

complex block diagramscomplex block diagrams

EXIT

EX

IT

utility routine

START

Page 16: The Art of Programming Program design and implementation

Structured programming - summaryStructured programming - summary

• Structured programs divide the problem into smaller sub-units or

blocks, then divided into smaller blocks.. eventually reaching the

level of program code.

• The blocks should ideally be self-contained – interactions with other

blocks should be explicit

• ALL programming languages support Structured Programming to

some extent

• Useful model for small-medium sized projects.

• Becomes unmanageable for larger, more complex projects, with

many programmers involved → Object Oriented ProgrammingObject Oriented Programming

(OOP).

• Structured programs divide the problem into smaller sub-units or

blocks, then divided into smaller blocks.. eventually reaching the

level of program code.

• The blocks should ideally be self-contained – interactions with other

blocks should be explicit

• ALL programming languages support Structured Programming to

some extent

• Useful model for small-medium sized projects.

• Becomes unmanageable for larger, more complex projects, with

many programmers involved → Object Oriented ProgrammingObject Oriented Programming

(OOP).

Page 17: The Art of Programming Program design and implementation

The object-oriented approachThe object-oriented approachEvolution of Program DesignEvolution of Program Design

1950s-1960s

1970s -1980s

1990’s-

“simple” program models

Zenith of structured programming- especially Pascal.

First object-oriented languages (esp C++)

traditional languages “converted” to OOP

COBOL, Algol, BASIC

Pascal, C, Fortran, Perl

Ada, C++, JAVA, SmallTalk

Visual Basic, Delphi (Object Pascal), Perl 5, (e.g. BioPerl)

Examples

Page 18: The Art of Programming Program design and implementation

The object-oriented approachThe object-oriented approach

The program is written as a collection of interacting objects.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.

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.

Page 19: The Art of Programming Program design and implementation

Example objects - chemistryExample objects - chemistryelectron

neutron

proton

molecules are made of ..

.. atoms which consist of ..

.. protons, neutrons and electrons ..

molecules

atoms

nuclei electrons

protons neutrons

object hierarchyobject hierarchy

Page 20: The Art of Programming Program design and implementation

OOP and structured programmingOOP 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!

Page 21: The Art of Programming Program design and implementation

What is a software object?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.

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.

Page 22: The Art of Programming Program design and implementation

Software representation of a cat Software representation of a cat objectobject

STATE

• Name

• Breed

• Weight

• Age

• Asleep

STATE

• Name

• Breed

• Weight

• Age

• Asleep

BEHAVIOUR

• Sleeps a lot

• Scratches furniture

• Catches mice

• Fights other cats

BEHAVIOUR

• Sleeps a lot

• Scratches furniture

• Catches mice

• Fights other cats

Page 23: The Art of Programming Program design and implementation

Implementation of objectsImplementation of objects

State is usually held as local variables (also called properties), quantities not visible outside the object (data hiding)

Behaviour controlled by method functions or subroutines which act on the local variables and interface with the outside.

STATE

BEHAVIOUR

Page 24: The Art of Programming Program design and implementation

Key feature of OOP - InheritanceKey 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

nerve cell skin cell

white blood cell

Page 25: The Art of Programming Program design and implementation

Examples of OO languagesExamples 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.

Page 26: The Art of Programming Program design and implementation

OOP - SummaryOOP - 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 experienced programmers Implementation of objects often use complicated

syntax/semantics OOP not famous for efficiency (memory or execution

time) C++ once famous for being slow, now much better Java still famous for being slow

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 experienced programmers Implementation of objects often use complicated

syntax/semantics OOP not famous for efficiency (memory or execution

time) C++ once famous for being slow, now much better Java still famous for being slow