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

The Politics of JavaScript

The Politics of JavaScript

Presented at Web Directions Code, Melbourne, Australia
May 2nd 2013

NOTE for those who didn't see it live: "humble style guide" is a parody. I'm not hating on "new" and I'm certainly not advocating "always use ==="

Angus Croll

May 02, 2013
Tweet

More Decks by Angus Croll

Other Decks in Technology

Transcript

  1. The Politics
    of JavaScript
    @angustweets

    View full-size slide

  2. 1995-2004 The Anarchy Years

    View full-size slide

  3. 2004-2006 The Revolution

    View full-size slide

  4. 2007-2010 The Coming of Age

    View full-size slide

  5. 2011-2013 The Itch

    View full-size slide

  6. ;
    ==
    “I’m not sure how our community got so
    religious and fact-disoriented, but it has
    got to stop”
    “I am sorry that, instead of
    educating you, the leaders in
    this language community
    have given you lies and fear.”
    “That is insanely stupid code. Fix it
    now. Learn to use semicolons”

    View full-size slide

  7. “JavaScript is Hard”

    View full-size slide

  8. We can either learn it...

    View full-size slide

  9. ...or we can hide from it

    View full-size slide

  10. “Please don’t do this”
    Every tech forum. Ever.
    “The subset I carved out is vastly
    superior to the language as a whole”
    Douglas Crockford - The Good Parts

    View full-size slide

  11. Q: “Why avoid increment (++)
    and decrement (--) operators in
    JavaScript?”

    View full-size slide

  12. A: “Part of me wonders if it has more
    to do with a lack of experience
    (perceived or actual) with javascript
    coders. I can see how someone just
    hacking away at some sample code
    could make an innocent mistake with
    ++ and --, but I don't see why an
    experienced professional would avoid
    them.”

    View full-size slide

  13. Excessive constraint limits
    innovation

    View full-size slide

  14. If you want preditcable,
    try Java...

    View full-size slide

  15. ...or Canberra

    View full-size slide

  16. because somebody once
    redefined undefined now we are
    all stupid :)

    View full-size slide

  17. function(x) {
    if (typeof x === 'undefined') {
    return;
    }
    //...
    }

    View full-size slide

  18. function(x) {
    if (x === undefined) {
    return;
    }
    //...
    }

    View full-size slide

  19. function(x) {
    if (x == undefined) {
    return;
    }
    //...
    }

    View full-size slide

  20. function(x) {
    if (x == null) {
    return;
    }
    //...
    }

    View full-size slide

  21. “We need constraints because
    most people don't know
    JavaScript well”

    View full-size slide

  22. “The body of a for in should be
    wrapped in an if statement to filter
    unwanted properties from the
    prototype”—JS Lint
    “hasOwnProperty [is] essential
    when iterating over the properties
    of any object ”—JS Garden

    View full-size slide

  23. Don’t buy the code for the dumbest
    ideology, it negates our
    responsibility to educate ourseleves,
    and our team.
    It holds us back and holds the
    language back.

    View full-size slide

  24. The Cult of Machines

    View full-size slide

  25. The Traffic Light Paradox

    View full-size slide

  26. Drachten, Netherlands 2000

    View full-size slide

  27. Drachten, Netherlands 2001

    View full-size slide

  28. Removed all traffic lights and signs
    from the town's centre
    Two years after the system was
    introduced, average travel times
    lowered significantly and yearly
    accidents were reduced from 8 to 1.
    Drachten, Netherlands 2000-2001

    View full-size slide

  29. “Accidents, as well as congestion, are
    reduced when motorists show greater
    individual responsibility, rather than
    mentally switching off to behave like
    automata”

    View full-size slide

  30. Case in Point...

    View full-size slide

  31. function natcompare(a,b) {
    var ca, cb;
    while (true) {
    ca = a.charAt(ia++); cb = b.charAt(ib++);
    //(some zero checking goes here)
    if (ca == 0 && cb == 0) {
    //The strings compare the same.
    return 0;
    }
    if (ca < cb) {return -1;}
    if (ca > cb) {return +1;}
    }
    }

    View full-size slide

  32. function natcompare(a,b) {
    var ca, cb;
    while (true) {
    ca = a.charAt(ia++); cb = b.charAt(ib++);
    //(some zero checking goes here)
    if (ca === 0 && cb === 0) {
    //The strings compare the same.
    return 0;
    }
    if (ca < cb) {return -1;}
    if (ca > cb) {return +1;}
    }
    }

    View full-size slide

  33. function natcompare(a,b) {
    var ca, cb;
    while (true) {
    ca = a.charAt(ia++); cb = b.charAt(ib++);
    //(some zero checking goes here)
    if (ca === 0 && cb === 0) {
    //YOU ARE DOOMED!.
    return 0;
    }
    if (ca < cb) {return -1;}
    if (ca > cb) {return +1;}
    }
    }

    View full-size slide

  34. Linters are great for catching
    trailing commas

    View full-size slide

  35. But they suck at nuance

    View full-size slide

  36. “Consistency is the hobgoblin of
    little minds”
    Ralph Waldo Emerson

    View full-size slide

  37. JSPerfismo
    http://javascriptweblog.wordpress.com/2011/02/07/truth-
    equality-and-javascript/#comment-1872

    View full-size slide

  38. Browsers are wise to repetitious
    benchmarking (e.g. split)
    You’re better off profiling your real
    life app
    JSPerf is fun, but remember...

    View full-size slide

  39. The more you automate your
    JavaScript code, the more soul
    destroying it gets...

    View full-size slide

  40. GWT
    is to
    so that

    View full-size slide

  41. It’s a bit like automatic transmission

    View full-size slide

  42. You won’t crunch the gears and you
    won’t crash the car...

    View full-size slide

  43. but you sacrifice control

    View full-size slide

  44. machines and bad programmers
    lack nuance

    View full-size slide

  45. use both with caution

    View full-size slide

  46. Fearmongering

    View full-size slide

  47. Fearmongers assume the worst of everyone

    View full-size slide

  48. Fearmongers assume we are all idiots

    View full-size slide

  49. Stop worrying about hypothetical developers
    overwriting undefined, hasOwnProperty etc.

    View full-size slide

  50. These are petty
    pseudo-contingencies
    Anyone could override Object.prototype and
    we’d all be screwed
    Work with people and libraries that you
    respect

    View full-size slide

  51. ES 5
    Function.prototype.bind
    ES 5
    String.prototype.trim

    View full-size slide

  52. “Always code as if the person who ends up
    maintaining your code is a violent
    psychopath who knows where you live.”
    - Jeff Attwood

    View full-size slide

  53. This is terrible advice.

    View full-size slide

  54. Good code doesn’t walk on
    eggshells
    Good code requires knowledge of your
    language and respect for your team
    Good code is bold

    View full-size slide

  55. JavaScriptophobia

    View full-size slide

  56. How “JavaScript is sooo simple”
    became “JavaScript is sooo hard”...

    View full-size slide

  57. (weird shit)
    JAVA!!
    But Easier!!
    (Deceptively
    Familiar Syntax)
    (weird shit)
    WTF?

    View full-size slide

  58. Transpilers to the Rescue!
    github.com/jashkenas/coffee-script/wiki/
    List-of-languages-that-compile-to-JS

    View full-size slide

  59. Syntax Transpilers
    CoffeeScript
    Superset Transpilers
    TypeScript
    Caja
    ES X Transpilers
    JavaScripty Transpilers

    View full-size slide

  60. JavaScripty Transpilers convert syntax
    Little or no signal loss

    View full-size slide

  61. GWT (Java)
    Dart (Dart)
    ClojureScript (Clojure)
    UnJavaScripty Transpilers
    Opal (Ruby)
    Pyjs (Python)
    Perlito (Perl)
    Script# (C#)
    scheme2js (Scheme)
    ghcjs (Haskell)
    Amber (Smalltalk)
    Go2js (Go)
    j2js (Java)
    rb2js (Ruby)
    rb2js (Ruby)
    Skuplt (Python)
    Java2Script (Java)
    FunScript (F#)
    JScriptSuite (.NET)
    ParenScript (Lisp)
    Clue (C)
    NS Basic (Basic)
    dash sjakhd
    dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd dash sjakhd

    View full-size slide

  62. UnJavaScripty Transpilers convert
    semantics
    Significant signal loss...

    View full-size slide

  63. “The dog ate my semicolons”

    View full-size slide

  64. “The dog ate my semicolons”
    “The dog ate my commas”
    “Le chien a mangé mes virgules”

    View full-size slide

  65. Fear is the Enemy of Creativity

    View full-size slide

  66. Ideology is mostly tribalism

    View full-size slide

  67. Almost any ideological position has an
    opposite position which is equally valid

    View full-size slide

  68. “The Oxford Comma is essential”
    “She lives with her two children, a cat and
    a dog.”

    View full-size slide

  69. “The Oxford Comma is rubbish”
    “Through the window she saw George, a
    policeman, and several onlookers”

    View full-size slide

  70. “Comma first prevents trailing commas”

    View full-size slide

  71. //comma last
    var thing = {
    veni,
    vidi,
    vici
    }

    View full-size slide

  72. //comma last
    var thing = {
    veni,
    vidi,
    }

    View full-size slide

  73. //comma last
    var thing = {
    veni,
    vidi,
    }

    View full-size slide

  74. //comma first
    var thing = {
    veni
    ,vidi
    ,vici
    }

    View full-size slide

  75. //comma first
    var thing = {
    veni
    ,vidi
    }

    View full-size slide

  76. //comma first
    var thing = {
    veni
    ,vidi
    }

    View full-size slide

  77. But, “comma last prevents leading commas”

    View full-size slide

  78. //comma last
    var thing = {
    veni,
    vidi,
    vici
    }

    View full-size slide

  79. //comma last
    var thing = {
    vidi,
    vici
    }

    View full-size slide

  80. //comma last
    var thing = {
    vidi,
    vici
    }

    View full-size slide

  81. //comma first
    var thing = {
    veni
    ,vidi
    ,vici
    }

    View full-size slide

  82. //comma first
    var thing = {
    ,vidi
    ,vici
    }

    View full-size slide

  83. //comma first
    var thing = {
    ,vidi
    ,vici
    }

    View full-size slide

  84. Sayre’s Law
    “In any dispute the intensity of feeling is
    inversely proportional to the value of the
    issues at stake.”
    Charles Phillip Issawi, 1973

    View full-size slide

  85. Thus, Semicolons...
    281 comments later...
    github.com/twitter/bootstrap/issues/3057

    View full-size slide

  86. Dichotomy is attractive,
    a simple choice between right and wrong.

    View full-size slide

  87. The One True Way!
    JavaScript vs CoffeeScript
    OOP vs Functional
    Callbacks vs Promises
    comma-first vs comma-last

    View full-size slide

  88. But JavaScript is not science.
    We’re all just muddling along.

    View full-size slide

  89. My Humble Style Guide
    1. Use Brackets In Conditionals
    2. Line Break after Function Signature
    3. Space after //

    View full-size slide

  90. My Humble Style Guide
    1. Use Brackets In Conditionals
    2. Line Break after Function Signature
    3. Space after //
    4. ALWAYS use ===
    5. NEVER parseInt without a radix arg
    6. You SUCK if you use new
    Attn. Slide Viewers: this is a parody!!
    Text

    View full-size slide

  91. Guidelines mistaken for rules...

    View full-size slide

  92. “No Globals Ever”
    Globals are a feature too
    Use with caution

    View full-size slide

  93. “Always use feature detection”
    Feature detect Object.defineProperty?
    In IE 8? Really?

    View full-size slide

  94. Each time we claim an absolute, some other
    technique is discarded from the toolbelt

    View full-size slide

  95. Would you buy
    “airplane controls, the good parts”?

    View full-size slide

  96. "There are indeed many ways to do the
    same thing in JS. That's one of the beauties
    of the language" - Peter van der Zee

    View full-size slide

  97. Learn one way and fight your
    corner...
    ...or learn them all and be informed.

    View full-size slide

  98. This doesn’t make good coders

    View full-size slide

  99. This doesn’t make good coders
    You can’t legislate against bad code

    View full-size slide

  100. This makes good coders*
    (*Thanks David Nolen)

    View full-size slide

  101. We can rise above the
    fearmongers, the ideologues and
    the absolutists...
    1) Master the fundamentals: this,
    prototypes, coercion and scoping
    2) Experiment, Play, Have Fun
    and Keep an Open Mind.

    View full-size slide

  102. The End
    @angustweets

    View full-size slide

  103. code credits
    natural order comparator
    Copyright (C) 2005 by SCK-CEN (Belgian Nucleair Research Centre)
    Copyright (C) 2003 by Pierre-Luc Paour
    Copyright (C) 2000 by Martin Pool

    View full-size slide

  104. photo credits
    JUDGES: http://fitsnews.files.wordpress.com/2007/05/group-of-judges.jpg
    MODERN TIMES: http://www.doctormacro.com/Images/Chaplin,%20Charlie/
    Annex/Annex%20-%20Chaplin,%20Charlie%20(Modern%20Times)_01.jpg
    DRACHTEN BEFORE: http://assets.knowledge.allianz.com/img/
    before_laweiplein_drachten_ah_51281.jpg
    DRACHTEN AFTER: http://cameronwatson.co.nz/wp-content/uploads/
    2013/03/Drachten.png
    DICK CHENEY: http://scrapetv.com/News/News%20Pages/Politics/images-4/
    cheney-evil.jpg
    AUTO GEAR: http://www.csa.com/discoveryguides/auto/images/res3.jpg
    MANUAL GEAR: http://media-social.s-msn.com/images/blogs/
    00100065-0000-0000-0000-000000000000_00000065-06d9-0000-0000-0000
    00000000_20120607230629_HighGlossBlackGearShiftKnob_867.jpeg
    FOOD PILL: http://2.bp.blogspot.com/-411Q8hsbXjk/T0K2gN55L-I/
    AAAAAAAAEQc/M2LXaIfMCxw/s1600/pill+on+a+plate.jpg
    FRIES: http://www.bubblews.com/assets/images/news/
    1646144206_1356758521.jpg
    TOOLBELT: http://image.made-in-china.com/2f0j00nMhElzuKnBrs/Double-
    Pouch-Tool-Belt.jpg
    COCKPIT: http://etc.usf.edu/clippix/pix/airplane-controls_medium.jpg
    NELSON MANDELA: http://theelders.org/sites/default/files/imagecache/
    full_width_940x430/nelson-mandela-un-600px.jpg
    CHEETAHS: http://img.thesun.co.uk/multimedia/archive/01511/
    WILD-532_1511437a.jpg

    View full-size slide