Not Everything Is An Object


Published on

A presentations highlighting the weaknesses in OO programming and showing how functional languages, in this case Clojure, could be the answer.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Programming languages pre date computers themselves...Starting in 1842 and finishing in 1843 Ada Lovelace translated a memoir of an Italian mathematician on Charles Baggage’s newest proposed machine... the Analytical Engine. The article she wrote contained the entire specification of how to calculate Bernoulli numbers with the Engine. This is believed to be the first ever computer program.Then in the 1940’s along comes the first recognisable computers and we get programming languages to go with them...1943 - Plankalkül (KonradZuse) 1943 - ENIAC coding system1949 - C-10In the 1950s the first three modern programming languages whose descendants are still in widespread use today were designed:FORTRAN (1955), the "FORmulaTRANslator", invented by John Backus et al.; LISP, the "LIStProcessor", invented by John McCarthy et al.; COBOL, the COmmonBusiness Oriented Language, created by the Short Range Committee, heavily influenced by Grace Hopper. 1951 - Regional Assembly Language1952 - Autocode1954 - FORTRAN1954 - IPL (forerunner to LISP) 1955 - FLOW-MATIC (forerunner to COBOL) 1957 - COMTRAN (forerunner to COBOL) 1958 - LISP1958 - ALGOL 581959 - FACT (forerunner to COBOL) 1959 - COBOL1962 - APL1962 - Simula1964 - BASIC1964 - PL/I1970 - Pascal1970 - Forth1972 - C1972 - Smalltalk1972 - Prolog1973 - ML1975 - Scheme1978 - SQL (initially only a query language, later extended with programming constructs) 1983 - Ada1983 - C++1985 - Eiffel1986 - Erlang1987 - Perl1989 - FL (Backus) 1990 - Haskell1991 - Python1991 - Java1993 - Ruby1993 - Lua1994 - ANSI Common Lisp1995 - JavaScript1995 - PHP1997 - Rebol2000 - C#2008 - JavaFX Script
  • Through the 1990s and up until today, OO languages are in the ascendency due to: the GUI. A GUI lends itself well to the concept of discrete objects due to: having their own knowledge and behaviour messaging each other in an event driven model. Low cost of memory
  • Modelling object graph where objects are passing messagesModelling state changeModelling problem domains where the actors are hierarchal in natureEvent driven programming
  • Object graphs or sub graphs mean lots of shared dataShared data bad for concurrencyRequires lockingLocking is bad because it is either coarse grained which is expensive in time or it’s fine grained which is expensive in terms of management.Locking management is hard because there is no compiler or VM support, it’s down to you to work out how to do itExample: lock objects by alphabetical orderYou have to remember to do it that wayErrors are next to impossible to test for, hard to debug
  • Functional programming
  • First order functions means that functions can be treated like values and can be return from and passed into other functionsFunction like constructs means that flow control constructs are more like functions in that they may return the last value etcStateless because it’s functional in the sense of a mathematical function. Values are passed in, those values are operated upon and a new value is passed out. The function does not depend on any outside state. If the function is pure it has no side effects.The advantage of this is that a pure function is simple to understand, the whole essence of the function is encapsulated within the function itself. Easy to testEasy to debugConcurrency – if there is no dependency on shared state then work can be easily parallelised.
  • Pure functional language = no side effects allowedImpure functional language = side effects are not restrictedLockless concurrency does not mean there are no locks, just that the language manages them for you.
  • Nil is null and is treated as false kinda sucks.Here is a value and it is trueHere is a value and it is falseNil/null should mean there is no value.
  • Mutable collection you can modify the collection: add, remove etcImmutable collections cannot be modified but instead you get a new copy of the collection with the changes.Key idea is that copies are cheap, both in terms of time and memoryHow does this work?Copies of mutable collections take time and memory, copying 100 items needs 100+ operations but..Because data is immutable, it’s not going to change, so I don’t have to copy the data, just provide a pointer to it.If I want your data with a new member added then I get a collection which shares most of the data but has my deltas
  • Clojure favours equality but this is not a big problem as data is immutable and so objects and object graphs have a hash, since this can’t change equality simply compares the hash values and not the data in the graphThe only exception to this is string comparisons because Clojure strings are just Java stringsNot a big problem as string comparison is efficient in JavaFirst check identityThen check lengthThen check equality
  • Vars must be defined before first useThey must be def’d but not necessarily boundWhat is a macro?A macro is a function that modifies reader data
  • If is a special form as code execution is conditionalIf the condition is true then a will be returnedIf the condition is false then b will be returnedThe ? Denotes that b is optionalI said before that if were function likeFirst example returns the result of hungover if true else the result of happySecond example returns the result of drink else nil
  • Tail RecursionSpecial form of recursion whereby the last thing a function does is to call itselfFunctions need a stack frameCall a function 1,000 times recursively then you need 1,000 stackframesBut if calling the function is the last thing you are going to doThen you are not coming back to the calling functionSo there’s an optimisation where you can just overwrite the stackframe of the calling funcSo you only ever need one stack frameMost lisp dialects have this tail recursion optimisationClojure doesn’t as there is not tail recursion on the JMV
  • Not Everything Is An Object

    1. 1. Not Everything is an Object<br />By @garyshort<br />
    2. 2. Agenda<br />The road to here<br />Where we are now<br />What OOD/P is good for<br />What it’s not good for<br />The solution<br />Demo<br />Questions.<br />
    3. 3. The Road to Here<br /><br />
    4. 4. Where we are Now<br /><br />
    5. 5. What OOP/D is Good For<br /><br />
    6. 6. What is it Not Good For?<br /><br />
    7. 7. The Solution<br />
    8. 8. What Does it Mean to be Functional?<br />First order functions<br />Function like constructs<br />Stateless<br />Immutable data.<br />
    9. 9. Clojure<br />A Lisp<br />Dynamic<br />Functional<br />Impure<br />“Lockless” Concurrency<br />Macros<br /><ul><li>JVM
    10. 10. Java Interop
    11. 11. Fast
    12. 12. Persistent collections
    13. 13. Easy to learn.</li></li></ul><li>Introduction to Clojure<br />Java.Lang.Object<br />Arbitrary sized numbers<br />Ratios: 18/20<br />Nil is null and is treated as false.<br />
    14. 14. Persistent Collections<br />Immutable<br />Cheap to copy<br />Examples<br />(1 2 3)<br />List – sequential lookup time<br />[1 2 3]<br />Vector – logarithmic lookup time<br />{“key” 1, 3 7, “foo” “bar”}<br />Hashmap – unordered, key value pairs.<br />
    15. 15. Equality Vs Identity<br />Equality<br />Two objects are equal<br />Two cylinders maybe equal if their volumes are equal<br />Identity<br />Two pointers to the same object<br />Clojure favours equality :-O.<br />
    16. 16. Clojure is Impure so has Mutability<br />Var<br />Mutable pointer to immutable data<br />You can’t change the data<br />But you can change what data the var points to<br />But vars hold global data, function defs etc .<br />This data won’t change<br />So why are vars mutable?<br />So we can patch running software. <br />
    17. 17. The Reader<br />Other programming languages<br />Compiler<br />Text -&gt; lexing and parsing -&gt; AST<br />Clojure<br />The Reader<br />Text -&gt; lexing and parsing -&gt; Literals (Data)<br />As soon as it reads a complete literal it’s passed to...<br />
    18. 18. The Evaluator<br />Compile Phase<br />Traverses the data<br />Symbol evaluation<br />Symbols evaluate into Vars<br />Symbol Dog evaluates to a var in the current namespace<br />Symbol MyPets/Dog evaluates to var in namespace MyPets<br />List evaluation<br />Lists evaluate into function calls<br />(+ 1 2 3)<br />Process macros<br />Execute phase<br />Effects the special forms<br />Calls functions.<br />
    19. 19. Special Forms<br />Reserved symbol<br />Denotes special list evaluation<br />Not a function call – it’s “something else”<br />Def, if, do, let, quote, var, fn, loop, recur, throw, try, ., new, set!<br />If a list starts with any of those it’s evaluated in a special way particular to that form<br />(if condition a b?)<br />(if (been_drinking) (hungover) (happy))<br />(if (have_beers) (drink))<br />
    20. 20. Tail Recursion<br /><br />
    21. 21. I’m Bored... Show me the Code!<br /><br />
    22. 22. Questions?<br /><br /><br />@garyshort<br />