Similar documents
Programmer's Dozen. Thirteen Recommendations for Reviewing, Kevlin Henney

Good Object- Oriented Development

Patterns in Java Unfinished Symmetry

Refactoring to

Procedural Programming. It's Back? It Never Went

The Good, the Bad, and the Koyaanisqatsi

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 5 Diploma in IT OBJECT ORIENTED PROGRAMMING

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

It Is Possible to Do Object-Oriented Programming in Java

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

Today's Agenda. References. Open Closed Principle. CS 247: Software Engineering Principles. Object-Oriented Design Principles

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

PROCESS DEVELOPMENT METHODOLOGY The development process of an API fits the most fundamental iterative code development

Concurrent Programming in the D Programming Language. by Walter Bright Digital Mars


Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

Menu. In general, what are possible advantages of one design over another? Class 27: Exam 2. Exam 2 Parenthesizing the Expression. Design A.

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Abstract Data Types. Different Views of Data:

CSC 172 Intermediate Progamming

COURSE 2 DESIGN PATTERNS

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

ECOM 2324 COMPUTER PROGRAMMING II

Software Construction

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CS3500: Object-Oriented Design Fall 2013

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

Concurrent Programming using Threads

Inheritance and Polymorphism

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

+ Today. Lecture 26: Concurrency 3/31/14. n Reading. n Objectives. n Announcements. n P&C Section 7. n Race conditions.

THREADS AND CONCURRENCY

Object-Oriented Design

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

1: Introduction to Object (1)

CSc 328, Spring 2004 Final Examination May 12, 2004

Coordinator. Example. Prashant Jain Corporate Technology, Siemens AG Munich, Germany

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

Will the Real OO Please Stand Up?

Lecture 10 Notes Linked Lists

Concurrent Models of Computation for Embedded Software

CIS 120 Midterm II November 16, 2012 SOLUTIONS

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

CMSC 202 Final May 19, Name: UserID: (Circle your section) Section: 101 Tuesday 11: Thursday 11:30

Object-Oriented Concepts and Design Principles

BCS THE CHARTERED INSTITUTE FOR IT. BCS Higher Education Qualifications BCS Level 6 Professional Graduate Diploma in IT EXAMINERS' REPORT

[ L5P1] Object-Oriented Programming: Advanced Concepts

MITOCW watch?v=kz7jjltq9r4

Liskov Substitution Principle

Design Principles: Part 2

15CS45 : OBJECT ORIENTED CONCEPTS

EINDHOVEN UNIVERSITY OF TECHNOLOGY

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

Lecture 10 Notes Linked Lists

Deviations are things that modify a thread s normal flow of control. Unix has long had signals, and these must be dealt with in multithreaded

Beyond Reduction... Busy Acquire. Busy Acquire. alloc. alloc. Non-Serial Execution: Serial Execution: Atomic but not reducible

Part X. Advanced C ++

Concepts of Programming Languages

Principles of Object-Oriented Design

Assertions. Assertions - Example

Introduction to Object-Oriented Programming

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Design Approaches for Concurrent Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 02/09/2012

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Concurrent Objects and Linearizability

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

CS514: Intermediate Course in Computer Systems. Lecture 38: April 23, 2003 Nested transactions and other weird implementation issues

Analysis and Design with the Universal Design Pattern

CS 617 Object Oriented Systems Lecture 3 Object Abstractions, Encapsulation, Abstract Data Types 3:30-5:00pm Thu, Jan 10

JAVABEANS CLASS TO BE A COMPONENT WHAT MAKES A DETAILED VIEW. Tomas Cerny, Software Engineering, FEE, CTU in Prague,

Designing Computer Systems Boolean Algebra

Exception Handling Alternatives (Part 2)

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Interview Questions of C++

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Advanced MEIC. (Lesson #18)

So far. Specifications, conclusion. Abstract Data Types (ADTs) Comparison by Logical Formulas. Outline

Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Conformance. Object-Oriented Programming Spring 2015

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Programming Languages and Techniques (CIS120)

Appendix A - Glossary(of OO software term s)

Lecture 7: Data Abstractions

Programming 2. Object Oriented Programming. Daniel POP

The New Java Technology Memory Model

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Actor-Oriented Design: Concurrent Models as Programs

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Object-Oriented Design I - SOLID

Transcription:

Immutability @KevlinHenney

Change is the only constant. Heraclitus

When it is not necessary to change, it is necessary not to change. Lucius Cary

Mutable Unshared mutable data needs no synchronisation Shared mutable data needs synchronisation Unshared Unshared immutable data needs no synchronisation Shared immutable data needs no synchronisation Shared Immutable

Mutable The Synchronisation Quadrant Unshared mutable data needs no synchronisation Shared mutable data needs synchronisation Unshared Unshared immutable data needs no synchronisation Shared immutable data needs no synchronisation Shared Immutable

Software is applied thought. Alan O'Callaghan

Never try to discourage thinking for you are sure to succeed. Bertrand Russell

LSP

A type hierarchy is composed of subtypes and supertypes. The intuitive idea of a subtype is one whose objects provide all the behavior of objects of another type (the supertype) plus something extra. What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T. Barbara Liskov "Data Abstraction and Hierarchy"

A type hierarchy is composed of subtypes and supertypes. The intuitive idea of a subtype is one whose objects provide all the behavior of objects of another type (the supertype) plus something extra. What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T. Barbara Liskov "Data Abstraction and Hierarchy"

A type hierarchy is composed of subtypes and supertypes. The intuitive idea of a subtype is one whose objects provide all the behavior of objects of another type (the supertype) plus something extra. What is wanted here is something like the following substitution property: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2, then S is a subtype of T. Barbara Liskov "Data Abstraction and Hierarchy"

public class Ellipse { private double semimajor, semiminor; public Ellipse(double a, double b) public double semimajoraxis() public double semiminoraxis() public void semimajoraxis(double a) public void semiminoraxis(double b) } public class Circle extends Ellipse { public Circle(double r) public double radius() public void radius(double r) }

public class Ellipse { public void semimajoraxis(double a) public void semiminoraxis(double b) } public class Circle extends Ellipse { @Override public void semimajoraxis(double a) { throw new UnsupportedOperationException(); } @Override public void semiminoraxis(double b) }

The reason a solution is so hard to come by is because the problem is poorly stated: mathematics tells us that a circle is an ellipse, so I can substitute a circle wherever an ellipse is required, suggesting that a circle is a subtype of an ellipse. Kevlin Henney "Vicious Circles", Overload 8, June 1995 http://accu.org/var/uploads/journals/overload08.pdf

The reason a solution is so hard to come by is because the problem is poorly stated: mathematics tells us that a circle is an ellipse, so I can substitute a circle wherever an ellipse is required, suggesting that a circle is a subtype of an ellipse. The troubles start when we introduce any state modifying functions, such as assignment or the ability to change the major and minor axes independently. Kevlin Henney "Vicious Circles", Overload 8, June 1995 http://accu.org/var/uploads/journals/overload08.pdf

The reason a solution is so hard to come by is because the problem is poorly stated: mathematics tells us that a circle is an ellipse, so I can substitute a circle wherever an ellipse is required, suggesting that a circle is a subtype of an ellipse. The troubles start when we introduce any state modifying functions, such as assignment or the ability to change the major and minor axes independently. We are so confident that we understand the mathematical concepts behind circles and ellipses that we have not bothered to ask any more questions of that domain. Kevlin Henney "Vicious Circles", Overload 8, June 1995 http://accu.org/var/uploads/journals/overload08.pdf

The first observation is that there is no way to change circles and ellipses once you have created them. Kevlin Henney "Vicious Circles", Overload 8, June 1995 http://accu.org/var/uploads/journals/overload08.pdf

The first observation is that there is no way to change circles and ellipses once you have created them. This is the correct mathematical model: there are no side effects in maths, conic sections do not undergo state changes, and there are no variables in the programming sense of the word. Kevlin Henney "Vicious Circles", Overload 8, June 1995 http://accu.org/var/uploads/journals/overload08.pdf

The first observation is that there is no way to change circles and ellipses once you have created them. This is the correct mathematical model: there are no side effects in maths, conic sections do not undergo state changes, and there are no variables in the programming sense of the word. Readers who are comfortable and familiar with functional programming and data flow models will recognise the approach. Kevlin Henney "Vicious Circles", Overload 8, June 1995 http://accu.org/var/uploads/journals/overload08.pdf

The first observation is that there is no way to change circles and ellipses once you have created them. This is the correct mathematical model: there are no side effects in maths, conic sections do not undergo state changes, and there are no variables in the programming sense of the word. Readers who are comfortable and familiar with functional programming and data flow models will recognise the approach. In the case of circles and ellipses, the circle is simply an ellipse with specialised invariants. There is no additional state and none of the members of an ellipse need overriding as they apply equally well to a circle. Kevlin Henney "Vicious Circles", Overload 8, June 1995 http://accu.org/var/uploads/journals/overload08.pdf

public class Ellipse { private double semimajor, semiminor; public Ellipse(double a, double b) public double semimajoraxis() public double semiminoraxis() } public class Circle extends Ellipse { public Circle(double r) public double radius() }

Pure Interface Layer Interfaces may extend interfaces, but there is no implementation defined in this layer. «interface» UsageInterface Common Code Layer Only abstract classes are defined in this layer, possibly with inheritance, factoring out any common implementation. CommonCode Concrete Class Layer Only concrete classes are defined, and they do not inherit from one another. ConcreteLeaf ConcreteLeaf ConcreteLeaf

public interface Ellipse { double semimajoraxis(); double semiminoraxis(); } public interface Circle extends Ellipse { double radius(); }

public class??? implements Ellipse { private double semimajoraxis, semiminoraxis; } public class??? implements Circle { private double radius; }

public class??? implements Ellipse { private double semimajoraxis, semiminoraxis; } The Naming of Cats is a difficult matter, It isn't just one of your holiday games; You may think at first I'm as mad as a hatter When I tell you, a cat must have THREE DIFFERENT NAMES. [] But above and beyond there's still one name left over, And that is the name that you never will guess; The name that no human research can discover But THE CAT HIMSELF KNOWS, and will never confess. public [] class??? implements Circle { private double radius; } T S Eliot

public class Ellipse { private double semimajor, semiminor; public Ellipse(double a, double b) public double semimajoraxis() public double semiminoraxis() } public class Circle { private double radius; public Circle(double r) public double radius() public Ellipse toellipse() }

public class Ellipse { private double semimajor, semiminor; public Ellipse(double a, double b) public double semimajoraxis() public double semiminoraxis() public boolean iscircle() }

Phenomenon: An element of what we can observe in the world. Phenomena may be individuals or relations. Individuals are entities, events, or values. Relations are roles, states, or truths. Individual: An individual is a phenomenon that can be named and is distinct from every other individual: for example, the number 17, George III, or Deep Blue's first move against Kasparov. Value: A value is an intangible individual that exists outside time and space, and is not subject to change.

Immutable Value Define a value object type whose instances are immutable. The internal state of a value object is set at construction and no subsequent modifications are allowed.

public class Date implements { public int getyear() public int getmonth() public int getdayinmonth() public void setyear(int newyear) public void setmonth(int newmonth) public void setdayinmonth(int newdayinmonth) }

public class Date implements { public int getyear() public int getmonth() public int getweekinyear() public int getdayinyear() public int getdayinmonth() public int getdayinweek() public void setyear(int newyear) public void setmonth(int newmonth) public void setweekinyear(int newweek) public void setdayinyear(int newdayinyear) public void setdayinmonth(int newdayinmonth) public void setdayinweek(int newdayinweek) }

public final class Date implements { public int getyear() public int getmonth() public int getweekinyear() public int getdayinyear() public int getdayinmonth() public int getdayinweek() }

public final class Date implements { public int year() public int month() public int weekinyear() public int dayinyear() public int dayinmonth() public int dayinweek() }

public final class Date implements { public int year() public int month() public int weekinyear() public int dayinyear() public int dayinmonth() public int dayinweek() }

public final class Date implements { public int year() public int month() public int weekinyear() public int dayinyear() public int dayinmonth() public int dayinweek() public Date withyear(int newyear) }

Builder Introduce a builder that provides separate methods for constructing and disposing of each different part of a complex object, or for combining cumulative changes in the construction of whole objects.

public final class Date implements { public int year() public int month() public int weekinyear() public int dayinyear() public int dayinmonth() public int dayinweek() public Date withyear(int newyear) { return new Date(newYear, month(), dayinmonth()); } }

public final class Date implements { public int year() public int month() public int weekinyear() public int dayinyear() public int dayinmonth() public int dayinweek() public Date withyear(int newyear) { return newyear == year()? this : new Date(newYear, month(), dayinmonth()); } }

public final class Date implements { public int year() public int month() public int weekinyear() public int dayinyear() public int dayinmonth() public int dayinweek() public Date withyear(int newyear) public Date withmonth(int newmonth) public Date withweekinyear(int newweekinyear) public Date withdayinyear(int newdayinyear) public Date withdayinmonth(int newdayinmonth) public Date withdayinweek(int newdayinweek) }

Copied Value Define a value object type whose instances are copyable. When a value is used in communication with another thread, ensure that the value is copied.

class date { public: date(int year, int month, int day_in_month); date(const date &); date & operator=(const date &); int year() const; int month() const; int day_in_month() const; void year(int); void month(int); void day_in_month(int); };

class date { public: date(int year, int month, int day_in_month); date(const date &); date & operator=(const date &); int year() const; int month() const; int day_in_month() const; void set(int year, int month, int day_in_month); }; today.set(2016, 5, 17);

class date { public: date(int year, int month, int day_in_month); date(const date &); date & operator=(const date &); int year() const; int month() const; int day_in_month() const; }; today = date(2016, 5, 17);

class date { public: date(int year, int month, int day_in_month); date(const date &); date & operator=(const date &); int year() const; int month() const; int day_in_month() const; }; today = { 2016, 5, 17 };

Referential transparency is a very desirable property: it implies that functions consistently yield the same results given the same input, irrespective of where and when they are invoked. That is, function evaluation depends less ideally, not at all on the side effects of mutable state. Edward Garson "Apply Functional Programming Principles"

Idempotence is the property of certain operations in mathematics and computer science, that they can be applied multiple times without changing the result beyond the initial application. The concept of idempotence arises in a number of places in abstract algebra [] and functional programming (in which it is connected to the property of referential transparency). http://en.wikipedia.org/wiki/idempotent

Asking a question should not change the answer. Bertrand Meyer

Asking a question should not change the answer, and nor should asking it twice!

In computing, a persistent data structure is a data structure that always preserves the previous version of itself when it is modified. Such data structures are effectively immutable, as their operations do not (visibly) update the structure in-place, but instead always yield a new updated structure. (A persistent data structure is not a data structure committed to persistent storage, such as a disk; this is a different and unrelated sense of the word "persistent.") http://en.wikipedia.org/wiki/persistent_data_structure

I still have a deep fondness for the Lisp model. It is simple, elegant, and something with which all developers should have an infatuation at least once in their programming life. Kevlin Henney "A Fair Share (Part I)", CUJ C++ Experts Forum, October 2002

https://twitter.com/mattpodwysocki/status/393474697699921921

Concurrency

Concurrency Threads

Concurrency Threads Locks

All computers wait at the same speed.

Shared memory is like a canvas where threads collaborate in painting images, except that they stand on the opposite sides of the canvas and use guns rather than brushes. The only way they can avoid killing each other is if they shout "duck!" before opening fire. Bartosz Milewski "Functional Data Structures and Concurrency in C++" http://bartoszmilewski.com/2013/12/10/functional-data-structures-and-concurrency-in-c/

Instead of using threads and shared memory as our programming model, we can use processes and message passing. Process here just means a protected independent state with executing code, not necessarily an operating system process. Russel Winder "Message Passing Leads to Better Scalability in Parallel Systems"

Languages such as Erlang (and occam before it) have shown that processes are a very successful mechanism for programming concurrent and parallel systems. Such systems do not have all the synchronization stresses that shared-memory, multithreaded systems have. Russel Winder "Message Passing Leads to Better Scalability in Parallel Systems"

Multithreading is just one damn thing after, before, or simultaneous with another. Andrei Alexandrescu

Actor-based concurrency is just one damn message after another.

Sender Message 3 Message 1 Receiver A Receiver B In response to a message that it receives, an actor can make local decisions, create more actors, send more messages, and determine how to respond to the next message received. http://en.wikipedia.org/wiki/actor_model

Stack

alphabet(stack) = {push, pop, popped, empty}

trace(stack) = {, push, pop, empty, push, push, push, pop, popped, push, push, pop, popped, push, pop, popped, pop, empty, }

push Empty Non-Empty pop / popped pop / empty push pop / popped

empty() -> receive {push, Top} -> non_empty(top); {pop, Return} -> Return! empty end, empty(). non_empty(value) -> receive {push, Top} -> non_empty(top), non_empty(value); {pop, Return} -> Return! {popped, Value} end.

Stack = spawn(stack, empty, []). Stack! {pop, self()}. Stack! {push, 42}. Stack! {pop, self()}. Stack! {push, 20}. Stack! {push, 16}. Stack! {pop, self()}. Stack! {pop, self()}. empty {popped, 42} {popped, 16} {popped, 20}