dpc2007 objects of desire (kevlin henney)

24
Objects of Desire Objects of Desire A Few Questions (and Answers) on A Few Questions (and Answers) on Modern Object Modern Object - - Oriented Development Oriented Development [email protected] [email protected] Kevlin Henney Kevlin Henney

Upload: dpc

Post on 05-Dec-2014

5.925 views

Category:

Technology


0 download

DESCRIPTION

Dutch PHP Conference 2007

TRANSCRIPT

Page 1: DPC2007 Objects Of Desire (Kevlin Henney)

Objects of DesireObjects of DesireA Few Questions (and Answers) onA Few Questions (and Answers) onModern ObjectModern Object--Oriented DevelopmentOriented Development

[email protected]@curbralan.comKevlin HenneyKevlin Henney

Page 2: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 22

AgendaAgenda

•• IntentIntentSome answers concerning OO and related topicsSome answers concerning OO and related topics

•• ContentContentA brief history of objectsA brief history of objectsLinguisticsLinguisticsEncapsulationEncapsulationClass hierarchiesClass hierarchiesPatternsPatternsTestingTesting

Page 3: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 33

A Brief History of ObjectsA Brief History of Objects

History rarely happens in the right order or at the right time, but the job of a historian is to make it appear as if it did.

James Burke

History rarely happens in the right order or at the right time, but the job of a historian is to make it appear as if it did.

James Burke

Page 4: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 44

Back to the FutureBack to the Future

•• How old is OO?How old is OO?40 years old this year (40 years old this year (SimulaSimula 67, Norway, 1967)67, Norway, 1967)

•• When did OO become mainstream?When did OO become mainstream?Around the mid 1990s (1997 is often quoted)Around the mid 1990s (1997 is often quoted)

•• Where is OO used?Where is OO used?Everywhere... and then someEverywhere... and then some

•• What about the future?What about the future?Objects will remain mainstream, but they are not Objects will remain mainstream, but they are not the only game in townthe only game in town

Page 5: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 55

LinguisticsLinguistics

Language is not simply a reporting device for experience but a defining framework for it.

Benjamin Whorf

Language is not simply a reporting device for experience but a defining framework for it.

Benjamin Whorf

Page 6: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 66

General LinguisticsGeneral Linguistics

•• Is there a relationship between language and Is there a relationship between language and OO style?OO style?

In spite of rumours to the contrary, there is a strong In spite of rumours to the contrary, there is a strong relationship between language features and relationship between language features and appropriate OO styleappropriate OO style

•• What can we learn from other languages?What can we learn from other languages?A lot!A lot!

•• Which is the best OO language?Which is the best OO language?It dependsIt depends

Page 7: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 77

Type SystemsType Systems

•• How do static and dynamic type systems How do static and dynamic type systems compare?compare?

They each both strengths and weaknessesThey each both strengths and weaknesses•• How does PHP5's type system measure up?How does PHP5's type system measure up?

It is inherently dynamic, so some of the features It is inherently dynamic, so some of the features borrowed from static type systems are surprisingborrowed from static type systems are surprising

•• What about type hints?What about type hints?These appear to combine all the problems of static These appear to combine all the problems of static typing with all the weaknesses of dynamic typingtyping with all the weaknesses of dynamic typing

Page 8: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 88

EncapsulationEncapsulation

encapsulate enclose (something) in or as if in a capsule.express the essential feature of (someone or something) succinctly.enclose (a message or signal) in a set of codes which allow use by or transfer through different computer systems or networks.provide an interface for (a piece of software or hardware) to allow or simplify access for the user.

The New Oxford Dictionary of English

encapsulate enclose (something) in or as if in a capsule.express the essential feature of (someone or something) succinctly.enclose (a message or signal) in a set of codes which allow use by or transfer through different computer systems or networks.provide an interface for (a piece of software or hardware) to allow or simplify access for the user.

The New Oxford Dictionary of English

Page 9: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 99

In a CapsuleIn a Capsule

•• How do you use How do you use publicpublic, , protectedprotected and and privateprivate??Declare data Declare data privateprivate, steer clear of , steer clear of protectedprotected, and only , and only make methods make methods publicpublic if you know why and where if you know why and where you're going to use them and how to test themyou're going to use them and how to test them

•• So, encapsulation is about data privacy?So, encapsulation is about data privacy?No, it relates to stability, coherence and ease of use No, it relates to stability, coherence and ease of use of an object of an object —— data privacy is a bydata privacy is a by--productproduct

•• How big should a class be?How big should a class be?Big enough, but not too bigBig enough, but not too big

Page 10: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 1010

Train Wrecks and VampiresTrain Wrecks and Vampires

•• Is chaining calls on function results useful?Is chaining calls on function results useful?PHP4's model was, quite frankly, surprisingPHP4's model was, quite frankly, surprising

•• What's the Law of Demeter?What's the Law of Demeter?Don't talk to strangersDon't talk to strangers

•• What's a train wreck?What's a train wreck?A violation of the Law of DemeterA violation of the Law of Demeter

•• But what about collections?But what about collections?Return iterators and offer focused methods, rather Return iterators and offer focused methods, rather than uncontrolled access to private collectionsthan uncontrolled access to private collections

Page 11: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 1111

Class HierarchiesClass Hierarchies

The history of all hitherto existing society is the history of class struggles.Karl Marx and Friedrich Engels

The history of all hitherto existing society is the history of class struggles.Karl Marx and Friedrich Engels

Page 12: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 1212

SubstitutabilitySubstitutability

•• What is LSP?What is LSP?The The LiskovLiskov Substitution PrincipleSubstitution Principle

•• What is the What is the LiskovLiskov Substitution Principle?Substitution Principle?Organise class hierarchies according to type Organise class hierarchies according to type substitutability, not according to commonality of substitutability, not according to commonality of implementationimplementation

•• What about reuse of implementation?What about reuse of implementation?Favour delegation, steer clear of ad hoc Favour delegation, steer clear of ad hoc subclassingsubclassing—— spaghetti inheritance is hard to digestspaghetti inheritance is hard to digest

Page 13: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 1313

InterfacesInterfaces

•• What is the common role of interfaces?What is the common role of interfaces?In statically typed languages, the In statically typed languages, the interfaceinterface construct, construct, or equivalent, promotes polymorphism and reduces or equivalent, promotes polymorphism and reduces dependencies dependencies —— inheritance of implementation is inheritance of implementation is the strongest form of coupling in an OO systemthe strongest form of coupling in an OO systemIn dynamically typed languages, the notion of In dynamically typed languages, the notion of usage type is normally left as an informal protocol usage type is normally left as an informal protocol or is expressed as predicate or constraint on objectsor is expressed as predicate or constraint on objects

•• What is the role of interfaces in PHP5?What is the role of interfaces in PHP5?Good questionGood question

Page 14: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 1414

PatternsPatterns

The enormous success of design patterns is a testimonial to the commonality seen by object programmers. The success of the book Design Patterns, however, has stifled any diversity in expressing these patterns.

Kent Beck

The enormous success of design patterns is a testimonial to the commonality seen by object programmers. The success of the book Design Patterns, however, has stifled any diversity in expressing these patterns.

Kent Beck

Page 15: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 1515

Shameless PlugShameless Plug

Page 16: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 1616

PitterPitter Pattern Brain DropPattern Brain Drop

•• What is a pattern?What is a pattern?A good pattern captures a proven solution practice A good pattern captures a proven solution practice along with the problem it addresses and the context along with the problem it addresses and the context in which it appliesin which it applies

•• Are patterns only about OO design?Are patterns only about OO design?NoNo

•• How do you apply patterns?How do you apply patterns?Unconsciously and tacitly or consciously and Unconsciously and tacitly or consciously and explicitly explicitly —— but watch out for but watch out for patternitispatternitis

Page 17: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 1717

The Good, the Bad and the UglyThe Good, the Bad and the Ugly

•• What are the top five patterns to know?What are the top five patterns to know?It dependsIt depends

•• If dependencies are so bad, why is Decorator If dependencies are so bad, why is Decorator recommended?recommended?

They are, and it isn't: it was documented as an OO They are, and it isn't: it was documented as an OO pattern nearly 15 years ago, but Interceptor is now pattern nearly 15 years ago, but Interceptor is now considered better for much of what Decorator doesconsidered better for much of what Decorator does

•• Do patterns make code more complicated?Do patterns make code more complicated?Only if used to make code more complicatedOnly if used to make code more complicated

Page 18: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 1818

The Epicycle ProblemThe Epicycle Problem

•• What is the trouble with Singleton?What is the trouble with Singleton?It is normally used by coincidence, it introduces a It is normally used by coincidence, it introduces a centralised point of coupling, it complicates testing centralised point of coupling, it complicates testing and it comes with various lifecycle problemsand it comes with various lifecycle problems

•• What are the alternatives?What are the alternatives?Focus on making essential object relationships Focus on making essential object relationships explicit, e.g. using patterns like Context Objectexplicit, e.g. using patterns like Context Object

•• What about the What about the MonostateMonostate pattern?pattern?This is also known as the Borg pattern, which tells This is also known as the Borg pattern, which tells you everything you need to knowyou everything you need to know

Page 19: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 1919

TestingTesting

Science is what we have learned about how to keep from fooling ourselves.Richard Feynman

Science is what we have learned about how to keep from fooling ourselves.Richard Feynman

Page 20: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 2020

Motivation for TestingMotivation for Testing

Page 21: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 2121

More MotivationMore Motivation

Page 22: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 2222

And MoreAnd More

Page 23: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 2323

Testing, Testing, One, Two, One, TwoTesting, Testing, One, Two, One, Two

•• How soon should testing be introduced?How soon should testing be introduced?Either as soon as you know what to test Either as soon as you know what to test —— why why wait? wait? —— or as soon as you don't know what to test or as soon as you don't know what to test —— use testing to help drive detailed design and use testing to help drive detailed design and clarification of requirementsclarification of requirements

•• What is the relationship between testing and What is the relationship between testing and design?design?

A test is a proposition about how something is A test is a proposition about how something is used, so it can be used as a form of specification and used, so it can be used as a form of specification and clarification of needs and knowledgeclarification of needs and knowledge

Page 24: DPC2007 Objects Of Desire (Kevlin Henney)

PHP Conference 2007PHP Conference 2007 2424

In Closing...In Closing...

The only thing to do with good advice is to pass it on. It is never any use to oneself.Oscar Wilde

The only thing to do with good advice is to pass it on. It is never any use to oneself.Oscar Wilde