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

A Programmer’s Red Pill: Pure, Functional, and Typed

A Programmer’s Red Pill: Pure, Functional, and Typed

An introduction to 3 red pills that will make you not look at your programming language the same way again

Chew Choon Keat

February 07, 2021
Tweet

More Decks by Chew Choon Keat

Other Decks in Technology

Transcript

  1. Warning “Actually, I hate you with all my soul because

    after going the route of pure functional language with union types.. there is no way back” “my whole life is a lie”
  2. About • C, Perl, Java • Javascript & Ruby on

    Rails for 10 years • Go and Elm in the past few years • @choonkeat
  3. “Software engineering is what happens to programming when you add

    time and other programmers.” — Russ Cox https://research.swtch.com/vgo-eng
  4. Hindley–Milner Type Inference Est. Since 1982 COVID-19 2000 1960 Java

    Ruby Javascript C Go 1980 Haskell Elm Python Standard ML
  5. Where error happened Where mistake was made Runtime errors Error

    when I save my fi le https://elm-lang.org/news/compilers-as-assistants
  6. Compiler Driven Development “ Change parts of the code and

    then let the compiler errors guide you in the rest of the task. Then rinse and repeat.
 ” https://dev.to/lucamug/elm-6m8
  7. Compiler Driven Development “ Change parts of the code and

    then let the compiler errors guide you in the rest of the task. Then rinse and repeat.
 ” https://dev.to/lucamug/elm-6m8
  8. GTD “I really like this approach of testing, it just

    walks you through: what’s the next step I have to do to get this working. ” http://railscasts.com/episodes/275-how-i-test
  9. TDD • This is what I have • This is

    what I need • Make it work
  10. TDD • This is what I have • This is

    what I need • Make it work
  11. TDD • This is what I have • This is

    what I need • Make it work Given Expect
  12. Given Expect TDD • This is what I have •

    This is what I need • Make it work Code
  13. Given Expect TDD • This is what I have •

    This is what I need • Make it work Code Write test harness
  14. • This is what I have • This is what

    I need • Make it work Code But wouldn’t it be better…
  15. TDD • This is what I have • This is

    what I need • Make it work Code Function Signature
  16. 🌈 Function Signature? 🌈 • Is the given URL fetched

    and JSON data parsed as User? • Are we connecting to some DB to get User? • What if `User` and `error` are both nil? Are both NOT nil?
  17. Pure Function • Its return value is the same for

    the same arguments • No side e ff ects; doesn’t change anything else anywhere
  18. GTD: Solve Smaller Problems • Custom Types to narrow problem

    surface • Solve small parts of bigger problems — regardless of whether your codebase is 50 or 500,000 LOC, focus only in the current function body • Easily assemble without baggage because functions are pure • Refactoring: establish new ideals then solve errors
  19. Constant Vigilance Functional architecture - The pits of success -

    Mark Seemann https://youtu.be/US8QG9I1XW0
  20. “I’m more interested in functional techniques than functional languages
 Adapters

    Ports Design Pattern + Best Practices Core • avoid shadowing variables • avoid cyclic dependency • avoid inheritance • avoid mutation • prefer explicit dependency • prefer stateless functions • pure functions are testable • …
  21. “I’m more interested in functional techniques than functional languages
 Adapters

    Ports Design Pattern + Best Practices Core • avoid shadowing variables • avoid cyclic dependency • avoid inheritance • avoid mutation • prefer explicit dependency • prefer stateless functions • pure functions are testable • …
  22. “Software engineering is what happens to programming when you add

    time and other programmers.” — Russ Cox https://research.swtch.com/vgo-eng
  23. “I’m more interested in functional techniques than functional languages
 Adapters

    Ports Soft Social Contract Core • avoid shadowing variables • avoid cyclic dependency • avoid inheritance • avoid mutation • prefer explicit dependency • prefer stateless functions • pure functions are testable • …
  24. Adapters Ports Soft Social Contract Core other people’s code urgent

    fi x • avoid shadowing variables • avoid cyclic dependency • avoid inheritance • avoid mutation • prefer explicit dependency • prefer stateless functions • pure functions are testable • … “I’m more interested in functional techniques than functional languages

  25. Life without Footguns Functional architecture - The pits of success

    - Mark Seemann https://youtu.be/US8QG9I1XW0
  26. Life without Footguns Functional architecture - The pits of success

    - Mark Seemann https://youtu.be/US8QG9I1XW0 How?
  27. Pure Functional Program Runtime Your Program Http request Render DOM

    Call Javascript Http response Input change Mouse move
  28. Pure Functional Program Runtime SAME callback function hooked up to

    ALL events Events in Return values Http request Render DOM Call Javascript Http response Input change Mouse move
  29. Pure Functional Program Runtime Events in Return values Http request

    Render DOM Call Javascript Http response Input change Mouse move Pure Function Pure Function Pure Function Pure Function Pure Function Pure Function Pure Function Pure Function Pure Function
  30. Pure Functional Program Runtime Your Callback Function Events in Return

    values Http request Render DOM Call Javascript Http response Input change Mouse move
  31. Life without Footguns Functional architecture - The pits of success

    - Mark Seemann https://youtu.be/US8QG9I1XW0
  32. Pure Functional Program Runtime Your Code Msg, Model Model, Cmd

    Http request Render DOM Call Javascript store `Model` value Http response Input change Mouse move
  33. Links • Elm at Rakuten https://dev.to/lucamug/elm-6m8 • How Elm Slays

    a UI Antipattern http://blog.jenkster.com/2016/06/how- elm-slays-a-ui-antipattern.html • Making Impossible States Impossible https://youtu.be/IcgmSRJHu_8 • Business logic in Elm that runs anywhere https://morphir. fi nos.org/ why_functional_programming • Exploring “Pure Core Imperative Shell” in Ruby https:// www.destroyallsoftware.com/talks/boundaries
  34. Summary • Custom Types — describing domain models really well,

    unambiguously 
 see https://youtu.be/Up7LcbGZFuo • Pure and Functional — can actually be done in any language, but you get to keep your footguns. I prefer no footguns.
 see https://youtu.be/US8QG9I1XW0