Pro Yearly is on sale from $80 to $50! »

DevInSantos 2015 - There is life beyond OOP

DevInSantos 2015 - There is life beyond OOP

alternate title: why you should care about functional programming

4569aec00cb223b3fbf484f9e7ba1256?s=128

Renan Ranelli

November 28, 2015
Tweet

Transcript

  1. THERE'S LIFE BEYOND OOP Renan Ranelli

  2. (OR …) WHY YOU SHOULD CARE ABOUT FUNCTIONAL PROGRAMMING Renan

    Ranelli
  3. Renan Ranelli (Milhouse)

  4. Software Engineer @ Renan Ranelli (Milhouse)

  5. Software Engineer @ Renan Ranelli (Milhouse)

  6. Renan Ranelli (Milhouse)

  7. 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 (!!)
  8. WHY {WAS,IS} OOP SO SUCCESSFUL?

  9. 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 (?)
  10. WHY {WAS,IS} OOP SO SUCCESSFUL?

  11. WHY {WAS,IS} OOP SO SUCCESSFUL?

  12. WHY {WAS,IS} OOP SO SUCCESSFUL? • The main reason IMO:

    the existence of Sun & Microsoft that pushed their `Java` and `.Net` platforms.
  13. MEANWHILE, WHAT IS HAPPENING IN THE HARDWARE INDUSTRY?

  14. WHAT IS HAPPENING IN THE HARDWARE INDUSTRY? • You should

    google “no more free lunch” and read the first article. Seriously.
  15. None
  16. 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
  17. None
  18. > 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
  19. This was written in March 2005

  20. 2005

  21. 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.
  22. This was written in March 2005

  23. 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...
  24. WHY ARE PEOPLE TALKING SO MUCH ABOUT FUNCTIONAL PROGRAMMING?

  25. 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
  26. 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
  27. Process Context Process Context HEAP (SHARED MEMORY) Stack2 Thread 2

    Stack3 Thread 3 Processor Core Stack1 Thread 1
  28. Process Context Process Context Processor Core HEAP (SHARED MEMORY) Stack1

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

    Thread 1 Stack2 Thread 2 Stack3 Thread 3
  30. Process Context Process Context Processor Core Processor Core HEAP (SHARED

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

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

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

  34. WHY ARE PEOPLE TALKING SO MUCH ABOUT FUNCTIONAL PROGRAMMING? •

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

  36. • If data is immutable, this problem is impossible, by

    construction
  37. WHY ARE PEOPLE TALKING SO MUCH ABOUT FUNCTIONAL PROGRAMMING? •

    Covariance of generic collection types:
  38. WHY ARE PEOPLE TALKING SO MUCH ABOUT FUNCTIONAL PROGRAMMING? •

    (code demo)
  39. WHY ARE PEOPLE TALKING SO MUCH ABOUT FUNCTIONAL PROGRAMMING? •

    (code demo)
  40. • If data is immutable, this problem is impossible, by

    construction
  41. 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.
  42. • IMPORTANT: IMMUTABILITY DOES NOT IMPLY LACK OF STATE (!)

    – (as I will show in a simple demo)
  43. THERE IS NO OOP VS FP DICHOTOMY

  44. 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
  45. 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
  46. THERE IS NO OOP VS FP DICHOTOMY • (what is

    a closure ?)
  47. 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)
  48. 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.
  49. FP IS *NOT* ABOUT CONCURRENCY AND MULTICORE (!!)

  50. (OR …) WHERE TO GO FROM NOW

  51. 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”)
  52. FP IS *NOT* ABOUT CONCURRENCY AND MULTICORE (!!) • Google

    for “peter norvig design patterns dynamic”
  53. 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.
  54. 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
  55. 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
  56. OBRIGADO !

  57. @renanranelli /rranelli Renan Ranelli (Milhouse) milhouseonsofware.com