Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of...

96
Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering

Transcript of Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of...

Page 1: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

Eiffel in Depth

Bertrand Meyer

Emmanuel Stapf (Eiffel Software)

Pei Yu

& members of the ETH Chair of Software Engineering

Chair ofSoftware Engineering

Page 2: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

2

Plan of these slides

1 Overview 2 The environment(s) 3 Method overview 4 Language basics 5 Dynamic model 6 Genericity &

inheritance 7 Design by Contract™

8 External interface 9 Agents 10 Advanced design 11 Advanced mechanisms 12 Conclusion 13 Supplements

Page 3: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

3

Course organization

Course page:http://se.inf.ethz.ch/teaching/2009-H/eiffel-0291/index.html

Teaching staff: Lecturer: Bertrand Meyer & members of the Chair

of Software Engineering

Course assistant: Yu (Max) Pei

Grading: 70% project, 30% examProject will be a web-based system using the new EiffelWebExam will be on last lecture slot of the semester: 15 Dec.

Page 4: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

4

Purpose of this course

To give you an in-depth understanding of a software method, language and environment: Eiffel (and EiffelStudio)

To improve your understanding of software engineering and software architecture

To give you a feel for the challenges involved in both software design and language design

Page 5: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

5

The software of the future

Product quality Correctness Robustness Security

Process quality Fast development No semantic gap (“impedance mismatch”) between developers and other stakeholders Self-validating, self-testing Ease of change Reusability

Page 6: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

6

- 1 -

Overview

Page 7: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

7

Why Eiffel?

Productivity: faster time to market, fewer developers

Reliability: fewer bugs

Extendibility: be responsive to customer needs

Reuse: stand on the shoulder of giants

Efficiency: make the best use of hardware resources

Maintainability: spend your time on new developments

Page 8: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

8

Language versions

Eiffel 1, 1986Classes, contracts, genericity, single and multipleinheritance, garbage collection, …

Eiffel 2, 1988 (Object-Oriented Software Construction)Exceptions, constrained genericity

Eiffel 3, 1990-1992 (Eiffel: The Language)Basic types as classes, infix & prefix operators…

Eiffel 4, 1997“Precursor” and agents

Eiffel 5, ECMA Standard, 2005, revised 2006, and ISO standard, November 2006www.ecma-international.org/publications/standards/Ecma-367.htm

Attached types, conversion, assigner commands…

Page 9: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

9

Eiffel: Method, Language, Environment

Method : Applicable throughout the lifecycle Object-oriented to the core Seamless development Based on Design by Contract™ principles

Language : Full power of object technology Simple yet powerful, numerous original features ISO standard (2006)

Environment : Integrated, provides single solution, including

analysis and modeling Lots of platforms (Unix, Windows, VMS, .NET…) Open and interoperable

Page 10: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

10

Some typical users

Axa RosenbergInvestment management: from $2 billion to >$100 billion 2 million linesChicago Board of Trade Price reporting system Eiffel + CORBA + Solaris + Windows + …Xontech (for Boeing) Large-scale simulations of missile defenseNorthrop-Grumman

Swedish social security: accident reporting & management

(Eiffel) Price Reporting System

The Chicago Board of Trade

See: eiffel.co

m

Page 11: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

11

Learning Eiffel

Simple syntax, no cryptic symbols

Eiffel programmers know all of Eiffel Wide variety of user backgrounds

“If you can write a conditional,you can write a contract”

Fast learning curve Lots of good models to learn from Strong style rules

May need to “unlearn” needless tricks

Page 12: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

12

The Eiffel method: some principles

Abstraction Information hiding Seamlessness Reversibility Design by Contract Open-Closed principle Single choice principle Single model principle Uniform access principle Command-query separation principle Option-operand separation principle Style matters ... See next...

Page 13: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

13

The Eiffel language

Classes

Uniform type system, covering basic types

Genericity

Inheritance, single and multiple

Conversion

Covariance

Statically typed

Built-in Design by Contract mechanisms

Agents: objects encapsulating behavior

“Once” mechanisms, replacing statics and globals

Void safety (new!)

Page 14: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

14

Libraries

Fundamental data structures and algorithms

Portable graphics

Internet, Web

Lexical analysis, parsing

Database interfaces

Page 15: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

15

Dogmatism and flexibility

Dogmatic where it counts:

Information hiding (e.g. no x.a := b) Overloading “One good way to do anything” Style rules

Flexible when it makes no point to harass programmers:

Give standard notations (e.g. a + b) an O-O interpretation

Syntax, e.g. semicolon

Page 16: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

16

The Eiffel language: there is a hidden agenda

That you forget it even exists

Page 17: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

17

- 2 -

The environment

Page 18: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

18

EiffelStudio

Serialization

EiffelStore

EiffelStudio

Ansi C

Executable system

IL

EiffelBase

WEL

EiffelVision

EiffelNet

EiffelWeb

EiffelMath

EiffelCOM

Persistent objects

Eiffel Runtime

Databases (Rel,

OO)

C compilation

JitterEiffel compilation

User classes

General library

Win32 library

Networking

Web development

Advanced numerics

External C/C++/Java

.NET Assemblies

EiffelBuild

GUI builder

Multiplatform GUI library

Browsing, fast compiling (Melting Ice™), debugging, diagrams, metrics...

Page 19: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

19

EiffelStudio: Melting Ice™ Technology

Fast recompilation: time depends on size of change, not size of program

Full type checking

“Freeze” once in a while

Optimized compilation: finalize.

Small program

Large program

Smallchange

Bigchange

Page 20: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

20

Melting Ice Technology

FROZEN

MELTED

EiffelStudio Your system

Machine code(from C code)Edit, browse,

execute,debug, test…

Freeze

Melt

Page 21: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

21

Performance

“Finalization” mode of compilation applies extensive optimizations:

Inlining Dead code removal Contract removal ...

Optimizations are compiler-applied and automatic; no need for manual hacking

Compacting garbage collection takes care of memory issues

Intended to match the most exacting demands of industry applications

Page 22: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

22

EiffelStudio browsing and debugging

You are dealing with “development objects”:

Classes Features Run-time objects (for debugging)

To work on an object, “pick-and-drop” it into an appropriate tool

Page 23: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

23

Openness

Eiffel can be used as “component combinator” to package elements from different sources:

Mechanisms for integrating elements in C, C++, Java, CIL (.NET)

Interfaces and libraries: SQL, XML, UML (XMI), CORBA, COM, others

Particularly sophisticated mechanisms for C/C++ interfacing

Outside of .NET, compiles down to ANSI C code, facilitates support for C and C++ easier.

On .NET, seamless integration with C#, VB .NET etc.

Page 24: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

24

C/C++ support

Functions, macros, include files, setters, getters, constructors, destructors etc.

Inline C

From the outside into Eiffel: CECIL (C-Eiffel Common Interface Library)

Page 25: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

25

Portability

Source-code portability across:

Windows NT, 2000, XP, Vista Windows 98, Me .NET Solaris, other commercial Unix variants Linux Mac OS X (forthcoming) BSD (Berkeley System Distribution) VMS

Page 26: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

26

- 3 -

The method

Page 27: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

27

The waterfall model of the lifecycle

Feasibility study

Requirements

Global design

Detailed design

Deployment

V & V

Specification

Implementation

Page 28: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

28

Traditional lifecycle model

Rigid model:Waterfall: separate tasks,

impedance mismatchesVariants, e.g. spiral, retain

some of the problemsSeparate tools:

Programming environmentAnalysis & design tools, e.g. UML

Consequences:Hard to keep model, implementation,

documentation consistent Constantly reconciling viewsInflexible, hard to maintain systemsHard to accommodate bouts of late wisdomWastes effortsDamages quality

Feasibility study

Requirements

Global design

Detailed design

Deployment

V & V

Specification

Implementation

Page 29: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

29

The Eiffel model

Seamless development:Single notation, tools, concepts, principles throughout Eiffel is as much for analysis & design as implementation & maintenanceContinuous, incremental developmentKeep model, implementation and documentation consistentReversibility: go back & forthSaves money: invest in single set of toolsBoosts quality

Example classes:PLANE, ACCOUNT, TRANSACTION…

STATE, COMMAND…

HASH_TABLE…

TEST_DRIVER…

TABLE…

Analysis

Design

Implemen-

tation

V&V

Generali-zation

Page 30: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

30

Seamlessness

Seamlessness Principle

Software development should relyon a single set of notations & tools

Page 31: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

31

Reversibility

Reversibility Principle

The software development process,notations and tools

should allow making changesat any step in the process

Page 32: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

32

The seamless, reversible model

Example classes:PLANE, ACCOUNT, TRANSACTION…

STATE, COMMAND…

HASH_TABLE…

TEST_DRIVER…

TABLE…

Analysis

Design

Implemen-

tation

V&V

Generali-zation

Page 33: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

33

Class invariant

Postcondition

Precondition

Specified, notimplemented

Analysis classesdeferred class VAT inherit

TANKfeature

in_valve, out_valve : VALVE

fill -- Fill the vat.

requirein_valve.openout_valve.closed

deferred

ensurein_valve.closed

out_valve.closedis_full

end

empty, is_full, is_empty, gauge, maximum,

invariantis_full = (gauge >= 0.97 * maximum)  and  (gauge <= 1.03 * maximum)

end

Page 34: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

34

Single model

Use a single base for everything: analysis, design, implementation, documentation...

Use tools to extract the appropriate views.

Single Model Principle

All the informationabout a software system

should be in the software text

Page 35: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

35

The seamless, reversible model

Analysis

Design

Implemen-

tation

V&V

Generali-zation

Page 36: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

36

Generalization

Prepare for reuse:Remove built-in limitsRemove dependencies on specifics of project

Improve documentation, contracts...

Abstract Extract commonalities, revamp inheritance hierarchy

36

A D I V G

A *

B

Y *

X Z

T

U

Page 37: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

37

The cluster model

A

D

I

V

G

Permits dynamic reconfiguration

A

D

I

V

G

A

D

I

V

G

A

D

I

V

G

A

D

I

V

G

A

D

I

V

G

Mix of sequential and concurrent engineering

Page 38: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

38

Tool support for seamless development

Diagram Tool• System diagrams can be produced automatically from software text

• Works both ways: update diagrams or update text – other view immediately updated

No need for separate UML tool Metrics Tool Profiler Tool Documentation generation tool ...

Page 39: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

39

EiffelStudio diagram tool

Page 40: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

40

Text-graphics equivalence

Page 41: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

41

Equivalence

Equivalence Principle

Textual, graphical and other viewsshould all represent the same model

Page 42: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

42

Eiffel mechanisms

Classes, objects, ... Single and multiple inheritance Inheritance facilities: redefinition, undefinition,

renaming Genericity, constrained and unconstrained Safe covariance Disciplined exception handling, based on

principles of Design by Contract Full GC Agents (power of functional programming in O-O!) Unrestricted streaming: files, databases,

networks...

Page 43: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

43

What is not in Eiffel

Goto Functions as arguments Pointer arithmetic Special increment syntax, e.g. x++, ++x

In-class feature overloading

Page 44: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

44

Syntax conventions

Semicolon used as a separator (not terminator)It’s optional almost all the time. Just forget about it!

Style rules are an important part of Eiffel: Every feature should have a header comment Every class should have an indexing clause Layout, indentation Choice of names for classes and features

Page 45: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

45

The class

From the module viewpoint: Set of available services (“features”) Information hiding Classes may be clients of each other

From the type viewpoint: Describes a set of run-time objects (the instances

of the class) Used to declare variables (more generally,

entities ), e.g. x : C

Possible type checking Notion of subtype

Page 46: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

46

Information hiding

Information Hiding principle

Every module should have a publicspecification,

listing a subset of its properties

Page 47: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

47

prepend

animate

append

An object has an interface

count stations

first last

Page 48: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

48

prepend

animate

appendcount

first

An object has an implementation

count stations

first last

Page 49: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

49

Information hiding

prepend

animate

appendcount stations

first last

Page 50: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

50

Information Hiding

The designer of every module must select a subset of its properties as the official information about the module, made available to authors of client modules

Public

Private

Page 51: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

51

Uniform access

Uniform access principle

It does not matter to the clientwhether you look up or compute

Page 52: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

52

Uniform Access: an example

balance = list_of_deposits.total – list_of_withdrawals.total

(A1)200 100 500 1000

800 100 100

(A2)

200 300 500 1000

800 100 100

list_of_deposits

list_of_withdrawals

list_of_deposits

list_of_withdrawals

balance 1000

Page 53: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

53

Uniform access

A call such as

your_account.balance

could use an attribute or a function

Uniform access principle

It does not matter to the clientwhether you look up or compute

Page 54: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

54

POINT : as an abstract data type

x : POINT REAL

y : POINT REAL

: POINT REAL

: POINT REAL

Class POINT: Choose a representation (polar, cartesian)

In polar representation, and are attributes, x and y are routines.

y

x

Page 55: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

55

POINT: as a class

class POINT feature

x, y : REAL-- Cartesian coordinates

move (a, b : REAL)-- Move by a horizontally and b vertically.

dox := x + ay := y + b

end

scale (factor : REAL)-- Scale by factor.

dox := factor xy := factor y

end

Page 56: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

56

Class POINT

distance (p : POINT ): REAL-- Distance to p

doResult := sqrt ((x – p.x)^2 + (y –

p.y)^2)end

ro : REAL-- Distance to origin (0, 0)

doResult := sqrt (x ^ 2 + y ^ 2)

end

theta : REAL-- Angle to horizontal axis

do…

endend

Page 57: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

57

Uniform access through feature call

To access a feature of a point, same notation regardless of representation.

Example:

p1.x

Cartesian representation: attribute call Polar representation: function call

No difference for clients (except possibly performance)

Page 58: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

58

Uniform access in practice

Class COMPLEX, switching silently and on demand between cartesian and polar representation

Secret attributes:

cartesian_uptodate, polar_uptodate : BOOLEAN

Representation invariant:

invariantat_least_one : cartesian_uptodate or polar_uptodate

Page 59: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

59

Updating representation: secret routine

update_cartesianrequire

polar_ok: polar_uptodatedo

if not cartesian_uptodate theninternal_x := ro cos (theta)internal_y := ro sin (theta)

endensure

cart_ok: cartesian_uptodatepolar_ok: polar_uptodate

end

Page 60: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

60

Public query

x : REAL -- Abscissa of current pointdo

if not cartesian_available thenupdate_cartesian

endResult := x_internal

ensurecart_ok : cartesian_uptodate

same_as_internal : Result = x_internal

end

Page 61: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

61

Adding two complex numbers

plus (other : COMPLEX )

-- Add other to current complex number. do

update_cartesian

x_internal := x_internal + other.xy_internal := y_internal + other.y

ensurecartesian_ok : cartesian_uptodate

end

Page 62: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

62

Beyond information hiding

Single choice principle

If a system supports a set of choices,only one of its elements should know the list

Page 63: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

63

Single choice: examples

Graphic system: set of figures

Editor: set of commands

Compiler: set of language constructs

Single choice principle

If a system supports a set of choices,only one of its elements should know the list

Page 64: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

64

Without dynamic binding!

display (f : FIGURE )do

if ‘‘f is a CIRCLE’’ then ...

elseif ‘‘f is a POLYGON’’ then...

endend

and similarly for all other routines!

Tedious; must be changed whenever there’s a new figure type

Page 65: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

65

With inheritance &associated techniques

f : FIGUREc : CIRCLEp : POLYGON

create c.make (...)create p.make (...)

if ... then f := c

else f := p

end

f.move (...)f.rotate (...)f.display (...)

-- and so on for every -- operation on f !

With:

Initialize:

and:

Then just use:

Page 66: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

66

Memory management

Memory management principle

It is the implementation’s responsibilityto reclaim unused objects

Page 67: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

67

What to do with unreachable objects

Reference assignmentsmay make some objects useless.

Two possible approaches: Manual “free” (C++). Automatic garbage collection

“Almaviva”namelandlor

dloved_one

aO1

“Figaro”O2

“Susanna”O3

Page 68: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

68

The C programmer’s view

Newsgroup posting by Ian Stephenson, 1993 (as cited in Object-Oriented Software Construction, 2nd edition):

I say a big NO ! Leaving an unreferenced object around is BAD PROGRAMMING. Object pointers ARE like ordinary pointers — if you allocate an object you should be responsible for it, and free it when its finished with. (Didn't your mother always tell you to put your toys away when you'd finished with them?)

Page 69: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

69

Arguments for automatic collection

Manual reclamation is dangerous for reliability.

Wrong “frees” are among the most difficult bugs to detect and correct.

Manual reclamation is tedious.

Modern garbage collectors have acceptable performance overhead.

GC is tunable: disabling, activation, parameterization....

Page 70: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

70

Properties of a garbage collector (GC)

Soundness: If the GC reclaims an object, it is unreachable

Completeness : If an object is unreachable, the GC will reclaim it

Soundness is an absolute requirement. Better no GC than an unsound GC

But: safe automatic garbage collection is hard in C-based languages

Page 71: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

71

Language style

Consistency principle

The language should offerone good way to do anything useful

Page 72: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

72

Language style

Compatibility principle

Traditional notations should be supportedwith an O-O semantics

Page 73: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

73

Infix and prefix operators

In

a − bthe − operator is “infix”

(written between operands)

In

− b

the − operator is “prefix” (written before the operand)

Page 74: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

74

The object-oriented form of call

some_target.some_feature (some_arguments)

For example:

my_figure.display

my_figure.move (3, 5)

x := a.plus (b) ???????

Page 75: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

75

Operator features

expanded class INTEGER feature

plus alias "+" (other : INTEGER): INTEGER-- Sum with other

do ... end

times alias "" (other : INTEGER): INTEGER -- Product by other

do ... end

minus alias "-" : INTEGER-- Unary minus

do ... end...end

Calls such as i.plus ( j ) can now be written i + j

Page 76: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

76

Assignment commands

It is possible to define a query as

temperature: REAL assign set_temperature

Then the syntaxx.temperature := 21.5

is accepted as an abbreviation for

x.set_temperature (21.5)

Retains contracts and any other supplementary operations

Not an assignment, but a procedure call

Page 77: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

77

Command-query separation

Command-Query Separation Principle

A function must not changeits target object’s state

Page 78: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

78

Command-Query separation

A command (procedure) does something but does not return a result.

A query (function or attribute) returns a result but does not change the state.

Page 79: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

79

Command-Query Separation

Asking a questionshould not change the answer!

Page 80: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

80

Command-query separation

Command-Query Separation Principle

A function must not changeits target object’s state

This principle excludes many common schemes, such as using functions for input (e.g. C’s getint or equivalent).

Page 81: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

81

Referential transparency

If two expressions have equal value, one may be substituted for the other in any context where that other is valid.

If a = b, then f (a) = f (b) for any f. Prohibits functions with side effects. Also:

For any integer i, normally i + i = 2 x i But even if getint () = 2, getint () + getint () is

usually not equal to 4.

Page 82: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

82

Command-query separation

Input mechanism using EiffelBase(instead of n := getint ()):

io.read_integer

n := io.last_integer

Page 83: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

83

A discipline of development

Reuse Principle

Design with reuse in mind

Page 84: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

84

Typical API in a traditional library (NAG)

nonlinear_ode(equation_count : in INTEGER; epsilon : in out DOUBLE; func : procedure

(eq_count : INTEGER; a : DOUBLE; eps : DOUBLE; b : ARRAY [DOUBLE]; cm : pointer Libtype);

left_count, coupled_count : INTEGER …)

[And so on. Altogether 19 arguments, including:• 4 in out values;• 3 arrays, used both as input and output;• 6 functions, each with 6 or 7 arguments, of

which 2 or 3 arrays!]

Ordinary differential equation

Page 85: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

85

The EiffelMath routine

... Create e and set-up its values (other than defaults) ...

e.solve

... Answer available in e.x and e.y ...

Page 86: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

86

The Consistency Principle

Two components: Top-down and deductive (the overall design). Bottom-up and inductive (the conventions).

Consistency Principle

All the components of a libraryshould proceed from an overall

coherent design, and followa set of systematic, explicitand uniform conventions.

Page 87: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

87

The key to building a library

Devising a theory of the underlying domain

Page 88: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

88

Some of the theory behind EiffelBase

CONTAINER

BOX

FINITE INFINITE

BOUNDED UNBOUNDED

FIXED RESIZABLE

COLLECTION

BAG SET

TABLE ACTIVE SUBSET

DISPENSERINDEXABLE CURSOR_STRUCTURE

SEQUENCE

TRAVERSABLE

HIERAR_CHICAL LINEAR

BILINEAR

*

* * *

*

*

*

*

* *

* * * * * *

* * * * * *

COUNTABLE*

RepresentationAccess

Iteration

Page 89: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

89

The size of feature interfaces

More relevant than class size for assessing complexity.

Statistics from EiffelBase and associated libraries:

Number of features 4408

Percentage of queries 66%

Percentage of commands 34%

Average number of arguments to a feature 0.5

Maximum number 5

No arguments 57%

One argument 36%

Two arguments 6%

Three or more arguments 1%

Page 90: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

90

Operands and options

Two possible kinds of argument to a feature: Operands: values on which feature will operate Options: modes that govern how feature will

operateExample (non-O-O): printing a real number

print (real_value, number_of_significant_digits, zone_length,

number_of_exponent_digits, ...)The number is an operand; format properties (e.g. number of significant digits, width) are options

O-O example:

my_window.display (x_position, y_position, height, width, text, title_bar_text, color, ...)

Page 91: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

91

Recognizing options from operands

Two criteria to recognize an option:

There is a reasonable default value.

During the evolution of a class, operands will normally remain the same, but options may be added.

Page 92: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

92

Option-Operand separation

Option values: Defaults (specified universally, per type, per

object) To set specific values, use appropriate “setter”

procedures

Example:

my_window.set_background_color ("blue")... my_window.display

Option-Operand Principle

Only operands should appearas arguments of a feature

Page 93: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

93

Naming (classes, features, variables…)

Traditional advice (for ordinary application programming):

Choose meaningful variable names!

Page 94: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

94

enter

push

add

insert

Original

Class

ARRAY

STACK

QUEUE

HASH_TABLE

entry

top

oldest

value

pop

remove_oldest

delete

Features

names for EiffelBase classes

put

put

put

put

item

item

item

item

remove

remove

remove

Final

enter

push

add

insert

Class

ARRAY

STACK

QUEUE

HASH_TABLE

remove_oldest

delete

Features

put

put

put

item

item

item

item

remove

remove

remove

entry

top

oldest

value

put

New and old names for EiffelBase classes

Page 95: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

95

Naming rules

Achieve consistency by systematically using a set of standardized names.

Emphasize commonality over differences.

Differences will be captured by: Signatures (number and types of arguments &

result) Assertions Comments

Page 96: Eiffel in Depth Bertrand Meyer Emmanuel Stapf (Eiffel Software) Pei Yu & members of the ETH Chair of Software Engineering Chair of Software Engineering.

96

Some standard namesQueries (non-boolean):

count, capacityitem

to_X, from_X

Queries (boolean) :writable, readable, extendible, prunableis_empty, is_full

-- Usual invariants:0 <= count ; count <= capacityis_empty = (count = 0) ; is_full = (count =

capacity)

if s.deletable thens.delete (v)

end

-- Some rejected names:

if s.addable thens.add (v)

end

Commands:put, extend, replace, forcewipe_out, remove, prunemake -- For creation