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

Defensive Programming vs. Let It Crash

Defensive Programming vs. Let It Crash

Shared at the second meetup of Elixir Shanghai.

A talk highly inspired by Ferd's "The Zen of Erlang" post, but from a different angle.

What does Erlang bring to the table that could enable us to be less defensive and more confident about our code?

Lou Xun

July 30, 2016
Tweet

More Decks by Lou Xun

Other Decks in Programming

Transcript

  1. Defensive(Programming
    vs.$Let$It$Crash
    [email protected]$/$Ela$Workshop

    View Slide

  2. A"cri&cal"bug"happened...
    • Resources)exhausted
    • Users)disconnect,)system)becomes)laggy
    • Cascading)failure
    • Loads)of)log,)errors)everywhere
    • "What)the)hell)is)the)root$cause?"
    [email protected]$/$Ela$Workshop

    View Slide

  3. You$started$debugging...
    • Follow&the&stacktrace
    • Looks&like&it's&some&cleanup&code
    • Something&is&wrong&in&the&try&block&(without&log!)
    • And&you&failed&to&recover&it&in&the&catch&block
    [email protected]$/$Ela$Workshop

    View Slide

  4. What%if%you%had...
    • The%exact%loca,on%where%things%start%to%fail
    • The%exact%"context"%at%there
    • local%variables,%global%variables
    • object%state
    • etc..
    • Essen,al%informa,ons%are%very%easy%to%lose
    [email protected]$/$Ela$Workshop

    View Slide

  5. You're'defensive,'because...
    • If$you$don't$catch,$a$raise$will$propagate$to$the$
    top...
    • and$crash$everything
    • You$write$code$that$you$wish$never$get$executed$in$
    produc;on
    • You're$hiding$a$broken$state$in$the$system
    [email protected]$/$Ela$Workshop

    View Slide

  6. You$program$defensively
    because'you'cannot'simply
    Let$It$Crash
    [email protected]$/$Ela$Workshop

    View Slide

  7. Erlang's)Process
    • Isolated
    • one+does+not+kill+all
    • Share+nothing
    • less+context+to+be+concerned
    • Lightweight
    • use+as+many+as+you+need
    • not+as+many+as+you+can
    [email protected]$/$Ela$Workshop

    View Slide

  8. Other&ingredients
    • A#func(onal#language
    • much#less#state
    • Message#passing
    • async#opera(on
    • Monitor#Link
    • handle#process#down,#fail#as#a#unit
    • Preemp(ve#scheduling,#Distribu(on
    [email protected]$/$Ela$Workshop

    View Slide

  9. Erlang/OTP
    • A#set#of#Erlang#libraries#and#design'principles
    • gen_server#etc..
    • maintain#state
    • unified#interface
    • supervisor
    • sole#job#is#supervising#children
    • start,#stop,#monitor,#restart
    [email protected]$/$Ela$Workshop

    View Slide

  10. State%&%Restart
    • State
    • Why%restart%works?
    • What%can%restart%fix?
    • Cannot%fix%everything%(of%course)
    • Clean%state
    • Isn't%it%the%same%as%restar?ng%my%daemon?
    [email protected]$/$Ela$Workshop

    View Slide

  11. Supervision+Tree
    [email protected]$/$Ela$Workshop

    View Slide

  12. Structured(supervision
    • Processes(close(to(root(are(robust,(solid,(rarely(
    change
    • Processes(down(to(leaves(can(be(fragile,(change(a(
    lot,(fail(regularly
    • When(a(process(fail,(supervisor(restarts(according(
    to(the(structure
    • Use(structure(to(recover(from(failure
    • Not(code
    [email protected]$/$Ela$Workshop

    View Slide

  13. Let$It$Crash
    • Don't'be'afraid'of'failures,'excep6ons'or'crashes
    • They'are'tools'we'can'use
    • No'more'defensive'programming,'only'code'as'if'
    everything'goes'right
    • Or'if'you'absolutely'know'how'to'deal'with'a'
    certain'failure
    • Otherwise'just'let$it$crash
    [email protected]$/$Ela$Workshop

    View Slide

  14. vs.$Defensive$Programming
    • Supervision,Tree,vs.,Nested,try..catch
    • Process,vs.,?
    • Self7healing,in,produc;on
    • Fail,early,,fail,fast,aids,debugging
    • Avoid,wri;ng,unused,error,handling,code
    • Produc;vity,,readability
    [email protected]$/$Ela$Workshop

    View Slide

  15. Thanks
    h"p:/
    /ferd.ca/the.zen.of.erlang.html
    [email protected]$/$Ela$Workshop

    View Slide