Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DevInSantos 2015 - There is life beyond OOP

DevInSantos 2015 - There is life beyond OOP

alternate title: why you should care about functional programming

Renan Ranelli

November 28, 2015
Tweet

More Decks by Renan Ranelli

Other Decks in Programming

Transcript

  1. AGENDA • Why OOP {was,is} so successful? • What is

    happening in the hardware industry? • Why are people talk so much about functional programming (FP)? • There is no FP vs OOP dichotomy • FP is not about concurrency & multicore (!!)
  2. WHY {WAS,IS} OOP SO SUCCESSFUL? • Object Oriented Programming gives

    you a “natural” way to talk about and structure programs, and offers many ways to achieve polymorphism and composition • A big chunk of “software engineering” disciplines being tailored to work with this paradigm (requirements, analysis, UML, design, etc.) • History: being at the right place at the right time with the right people (?)
  3. WHY {WAS,IS} OOP SO SUCCESSFUL? • The main reason IMO:

    the existence of Sun & Microsoft that pushed their `Java` and `.Net` platforms.
  4. WHAT IS HAPPENING IN THE HARDWARE INDUSTRY? • You should

    google “no more free lunch” and read the first article. Seriously.
  5. WHAT IS HAPPENING IN THE HARDWARE INDUSTRY? • You should

    google “no more free lunch” and read the first article. Seriously. • Since I'm already here, I'll give you a primer: – CPU clock is not getting exponentially faster – Transistor number is still growing rapidly – No performance gain for single threaded apps – Concurrency is the next major revolution in how we write software
  6. > Concurrency is the next major revolution in how we

    write software > Applications will increasingly need to be concurrent if they want to fully exploit continuing exponential CPU throughput gains > Efficiency and performance optimization will get more, not less, important
  7. In 2005: – Java 5 was hot news. – Windows

    XP. Vista only in 2007. – No AWS, Twitter, Netflix. – Ruby on Rails launched in December 13th. – Youtube was just founded. – We never have heard of Justin Bieber – I was playing Gunbound & Tibia. No money for WoW.
  8. WHAT IS HAPPENING IN THE HARDWARE INDUSTRY? • Every programming

    language created in the last few years has a strong focus in making concurrent programming easier: – Scala, Clojure – Elixir – Go, Rust – The recent popularity boom of Erlang & Haskell – Kotlin, Nim, Pony, Crystal, etc...
  9. WHY ARE PEOPLE TALKING SO MUCH ABOUT FUNCTIONAL PROGRAMMING? •

    First of all, functional programming is not a new thing • Here is a list of “functional languages” and the year they were introduced: – 1960 → Lisp – 1973 → ML – 1980s → Lazy langs: Miranda, KRC, SASL, Orwell... – 1986 → Erlang – 1990 → Haskell 1.0
  10. WHY ARE PEOPLE TALKING SO MUCH ABOUT FUNCTIONAL PROGRAMMING? •

    There is no consensus on what actually means for a language to be “functional”. We prefer talking about “functional style”, which rely heavily on: – Functions as first class citizens – Immutability of data • These are fundamental to avoid types of race conditions
  11. Process Context Process Context HEAP (SHARED MEMORY) Stack2 Thread 2

    Stack3 Thread 3 Processor Core Stack1 Thread 1
  12. Process Context Process Context Processor Core Processor Core HEAP (SHARED

    MEMORY) Stack1 Thread 1 Stack2 Thread 2 Stack3 Thread 3
  13. Process Context Process Context HEAP (SHARED MEMORY) Stack1 Thread 1

    Stack2 Thread 2 Stack3 Thread 3 Processor Core Processor Core
  14. Process Context Process Context HEAP (SHARED MEMORY) Stack1 Thread 1

    Stack2 Thread 2 Stack3 Thread 3 Processor Core Processor Core
  15. WHY ARE PEOPLE TALKING SO MUCH ABOUT FUNCTIONAL PROGRAMMING? •

    The “functional style” relies heavily on: – Functions as first class citizens – Immutability of data
  16. WHY ARE PEOPLE TALKING SO MUCH ABOUT FUNCTIONAL PROGRAMMING? •

    This is one of the cases where immutability makes your life easier. It will also enable the compiler/runtime to perform a lot of optimizations that are just downright impossible in languages like Java, Ruby, Python, C#, etc.
  17. • IMPORTANT: IMMUTABILITY DOES NOT IMPLY LACK OF STATE (!)

    – (as I will show in a simple demo)
  18. THERE IS NO OOP VS FP DICHOTOMY • You probably

    has heard the following: – Good OO design dictates that an object has a single responsibility – Good OO design dictates that an object should have few methods in its public interface – Good OO design dictates that an object limit the number of other objects it interacts with
  19. THERE IS NO OOP VS FP DICHOTOMY • In the

    limit … – Objects should interact with few other objects – Objects should contain only a single public method, which does one single thing • This “object” is indistinguishable from a closure
  20. THERE IS NO OOP VS FP DICHOTOMY • Closures are

    behavior with attached data • Objects are data with attached behavior • The concepts are dual (which means you can describe one in terms of the other)
  21. THERE IS NO OOP VS FP DICHOTOMY • You may

    have heard that functional programming is hard • I strongly disagree. Object systems are hard, and you don't see people whining about them • What is hard is changing your paradigm, and getting out of your comfort zone.
  22. WHERE TO GO FROM NOW • Although FP helps a

    lot in writing concurrent software, it is by no means limited to this. • Many design patterns common to object oriented languages are downright irrelevant with functional languages • Many problems are much better described in functional idioms (google for “the expression problem”)
  23. FP IS *NOT* ABOUT CONCURRENCY AND MULTICORE (!!) • Google

    for “peter norvig design patterns dynamic”
  24. WHERE TO GO FROM NOW • Functional Reactive Programming is

    a great to organize and structure programs that must react to user input. Check out Elm for a conundrum of amazing ideas. • You don't need to abandon OO to use functional programming: Scala, C#, F# and many others let you mix and match according to your tastes.
  25. WHERE TO GO FROM NOW • Many great ideas like

    Actor systems, software transactional memory, persistent data structures, parser combinators, etc where popularized and “discovered” in the functional community • There is a lot of nice things happening out there. • Get out of your box
  26. IF YOU WANT EXPAND YOUR MIND & LEARN MORE •

    Take Prof. Dan Grossman's “Programming Languages” course on Coursera • Take Martin Odersky's “Functional programming principles in Scala” course on Coursera • Take Erik Meijer's “Functional programming” course on Edx. • … learn Haskell • … write your own Lisp interpreter