Fondamenti della Programmazione: Metodi Evoluti Prof ... · 1-INTRODUZIONE Rev. 1.1.5 (2015-16) by...

Post on 19-Feb-2019

216 views 0 download

Transcript of Fondamenti della Programmazione: Metodi Evoluti Prof ... · 1-INTRODUZIONE Rev. 1.1.5 (2015-16) by...

Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico Nardelli

Lezione 1: Introduction

versione originale: http://touch.ethz.ch

Obiettivi formativi

• Padroneggiare la programmazione in modo professionale.

• Chiunque può scrivere programmi funzionanti • Non ci si sporca, né si suda • Scrivere programmi funzionanti in modo robusto

ed affidabile, anche e soprattutto man mano che li si modifica ed aggiorna, richiede strumenti e tecniche adeguate.

• Cosa usiamo nel corso: • il linguaggio orientato agli oggetti Eiffel • la metodologia di progetto Design by Contract • l'approccio outside-in allo sviluppo di programmi

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 2

Schedule

Lezioni: Martedi, 9:00 – 11:00, Aula L3 Venerdi, 9:00 – 11:00, Aula L3

Spiegazioni: Al termine delle lezioni Su richiesta, previo appuntamento per e-mail

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 3

About me

Contact: E-mail: nardelli@mat.uniroma2.it Telephone: 06 7259 4204 Office: 0123, piano terra, dente 1, Dipartimento di Matematica

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 4

Course web page (in italiano): http://www.mat.uniroma2.it/nardelli/fondamenti-programmazione-metodi-evoluti.html

Informazioni organizzative Materiale didattico: Informazioni e risultati esami Ricevimento studenti

The original page (in english): http://se.inf.ethz.ch/old/teaching/2009-H/eprog-0021/english_index.html

Lecture slides (link: slides and video recordings) • Also audio/video recording of original lectures by B.Meyer

Exercise material • Text and solutions • Supplementary slides

Advanced topics

Course information

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 5

Dalla mia pagina web del corso

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 6

The textbook

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 7

The software

The exercises rely on: the EiffelStudio “development environment” the Traffic “library” Application domain: Transportation system in a city (Paris is used as an example) You will need to download:

EiffelStudio: http://dev.eiffel.com/ Traffic: http://traffic.origo.ethz.ch/download

(see instructions on exercise sheet of week 1). It’s useful to have a PC…

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 8

Discovering Traffic

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 9

Warning

Not everything is perfect... The software probably has mistakes (“bugs”), and the textbook does, too… BUT:

Mistakes will be corrected, as quickly as possible. If you try something, don’t blame the software

first. It may be doing just what you told it to.

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 10

Why this approach?

• Many software issues become really tough for big systems

• With other approaches, in a fundamentals course, you only see small programs

• Here you get lots of software; use it as model & inspiration

• You learn to use software through abstract interfaces (also known as contracts)

• You go from consumer to producer: outside-in • Traffic is graphical and fun! • You should at the end be able to understand all of it. • Then you can add to it yourself

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 11

Basic advice

Attend all lectures Read material — textbook, slides — before lecture Bring a printout of the slides to the lecture, take notes Attend all exercise sessions Do all exercises If you don’t understand, ask

(again: there are no stupid questions) Don’t prepare for the exam at the last minute Keep a critical, probing attitude

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 12

Previous programming experience

If you have already programmed, take advantage of it, but using a fresh look; explore Traffic If you are new to programming, don’t be afraid; it can be hard at the beginning but you’ll get the hang of it. Mathematics is as useful a preparation as programming experience

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 13

THE INDUSTRY OF PURE IDEAS

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 14

Topics

What is software? Objects & programs Interfaces and the notion of class Logic and contracts The run-time model: object creation, references Describing syntax Control structures

Inheritance Genericity Recursion Data structures Event-driven programming & agents Topological sort Intro to software engineering

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 15

Software engineers build machines

• You can’t touch, kick or drop our machines: they’re immaterial

• But they are machines anyway • We call them programs or systems

• To operate (or run or execute) a program you

need a physical machine: a computer

• Computers and related devices: hardware

• Programs and associated intellectual value: software

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 16

Software everywhere

• Banking: manage millions of accounts • Trading: decide to sell or buy • Transportation: control trains, track planes...

Some cars have millions of lines of program code • Travel: air, train, hotel reservations • Communication: phones, Internet, … • Government: manage taxes, track laws... • Health care: keep health record, control devices • Education • Entertainment • Information • etc.

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 17

Computers everywhere

Banks Airplanes, cars… Washing machines Cell phones (70% of value) Printers Tomorrow: your shirt…

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 18

Computers come in all sizes, colors, flavors

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 19

Computers

Computers are universal machines. They execute the program that you feed them

The only limit is your imagination

The good news:

Your computer will do exactly what your program says

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 20

Building and running programs

Programmer: writes programs User: runs programs

A programmer writes a

a user

which

runs on

a computer

Program

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 21

Program using

a computer A programmer

which

runs on

a computer

writes a

a user

Building and running programs

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 22

users

their computers

using a computer

run on

which

Program

Building and running programs

writes a

A programmer

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 23

Programmers

which

their computers

using computers

run on

Program

users

Building and running programs

write a

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 24

Computers

Computers are universal machines. They execute the program that you feed them.

The only limit is your imagination.

The good news:

Your computer will do exactly what your program says.

It will do it very fast.

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 25

Moore’s “Law”

Approximate doubling of computing power, for comparable price, every eighteen months (Is this what Moore’s law says?) (No: approximate doubling of the number of transistors)

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 26

Moore’s law (source: Intel)

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 27

Moore’s “Law”

Approximate doubling of computer power, for comparable price, every eighteen months

2000

1 MHz

10 MHz

1 GHz

100 MHz

1990 1980 1970

8008: < 1 MHz

80386: 33 MHz 80486: 50 MHz

Pentium: 133 MHz

Pentium IV: 1.3 GHz

to 1 GHz: 26 years from 1 to 2 GHz: 8 months

Speed of Intel processors

(1 Hertz = 1 clock cycle per second)

3.8 GHz

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 28

Microprocessors (source: Intel)

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 29

Common myths and excuses

“Computers are intelligent” Fact: Computers are neither intelligent nor stupid.

They execute programs devised by humans. These programs reflect the intelligence of their authors.

The basic computer operations are extremely elementary (store this value, add these two numbers…).

“The computer has crashed” “The computer doesn’t allow this” “The computer lost your record” “The computer messed up your record”

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 30

Computers don’t make mistakes *....

Programs don’t make mistakes either Programmers do make mistakes

*Actually, hardware can malfunction, but this is much more rare than program errors

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 31

Computers

Computers are universal machines. They execute the program that you feed them

The only limit is your imagination

The good news:

Your computer will do exactly what your program says

It will do it very fast

The bad news:

Your computer will do exactly what your program says

It will do it very fast

“To err is human, but to really mess

things up takes a computer”

and your carefulness

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 32

The “Blue Screen Of Death”

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 33

Writing software is tough

• Programs “crash” • Programs that don’t crash don’t necessarily work • Incorrect programs have killed people, e.g. in

medical devices

• Programmers are responsible for the correct functioning of their programs

• The purpose of this course is to teach you not just programming but good programming

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 34

Learning to program well

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 35

An highly expensive software error

Look at the consequences of a program error! http://www.youtube.com/watch?v=kYUrqdUyEpI Ariane 5 rocket, 1996: 370 million US$ lost because of a simple program error (7 billion US$ is the total cost of the rocket development) Jean-Marc Jézéquel & Bertrand Meyer: Design by

Contract: The Lessons of Ariane, in Computer (IEEE), vol. 30, no. 1, January 1997, pages 129-130, archive.eiffel.com/doc/manuals/technology/contract/ariane/.

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 36

Worst software failures

10 worst software bugs (until 2005): http://www.wired.com/software/coolapps/news/2005/11/69355?currentPage=all A more recent account http://www.computerworld.com/s/article/9183580/Epic_failures_11_infamous_software_bugs 10 worst software failures in 2011 http://www.testmagazine.co.uk/2011/12/top-ten-worst-software-failures-of-2011/ 10 worst software failures in 2012 http://www.testmagazine.co.uk/2012/12/the-highest-profile-software-failures-of-2012/ 1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 37

What computers do

Memories, processors and communication devices are the hardware

Rest of the world

Processors

Memories

Communication

devices

Storage and retrieval Memories

Operations Processors

Communication Communication devices

Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 1-INTRODUZIONE 38

General organization

Rest of the world

Processors

Memories

Communication

devices

“Core” memory, disks...

“Persistent” or not

Keyboard, mouse, video display, network connector...

Also “CPU”

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 39

Computers

Computers are universal machines. They execute the program that you feed them.

Program

(Universal machine)

+ = Specialized

machine

Software

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 40

•Data: collections of symbols held in a computer • Information: interpretation of data for human purposes

Information and data

Information is what you want, e.g. a text or music Data is how it is encoded for the computer, e.g. MP3 audio format

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 41

Information and data processing

Data is stored in memory Input devices produce data from information Output devices produce information from data Information Information

Data

Data

Data Input Output

Process

Computer

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 42

Where’s the program?

Stored-program computer: the program is in memory “Executable data”

A program can appear in memory in different forms:

Source: human-readable form (programming language) Target form, machine code, object form: form executable

by the computer A compiler transforms source text to machine code The computer (more precisely the platform: computer + operating system) finds your program in memory to execute it

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 43

Software Engineering

Writing software that’s Correct

Does what it’s supposed to! Extendible

Easy to change! Readable

by humans! Reusable

Don’t reinvent the wheel! Robust

React appropriately to errors Secure

Defeat attackers 1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 44

Operating systems: source size

Unix V7: 10K

1990 1992

1995 1998 2000

Red Hat 7.1: 30

Linux: 10 K

10

20

40

30

Lines of code (millions)

Windows 3.1: 3 M Windows NT: 4 M

Windows 95: 15 Windows 98: 18

Windows 2000: 40

Red Hat 6.2: 17 Solaris 7: 12

Windows XP: 45

2006

50 Vista: 50

Debian 2.2: 55

Debian 3.1: 213!

2001

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 45

Writing software is tough

It is difficult to get a program right Trial-and-error approach very inefficient

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 46

Writing software is fun

Design and build your own machines Exert your creativity and imagination Programs save lives and help make the world better Experience the feeling of a program that you wrote, and that works

1-INTRODUZIONE Rev. 1.1.5 (2015-16) by Enrico Nardelli from B.Meyer's originals 47