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 Slide

  2. View Slide

  3. 1995-2004 The Anarchy Years

    View Slide

  4. View Slide

  5. 2004-2006 The Revolution

    View Slide

  6. View Slide

  7. 2007-2010 The Coming of Age

    View Slide

  8. View Slide

  9. 2011-2013 The Itch

    View Slide

  10. ;
    ==
    “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 Slide

  11. Paternalism

    View Slide

  12. “JavaScript is Hard”

    View Slide

  13. We can either learn it...

    View Slide

  14. ...or we can hide from it

    View Slide

  15. “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 Slide

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

    View Slide

  17. 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 Slide

  18. Excessive constraint limits
    innovation

    View Slide

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

    View Slide

  20. ...or Canberra

    View Slide

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

    View Slide

  22. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  28. “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 Slide

  29. View Slide

  30. View Slide

  31. 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 Slide

  32. The Cult of Machines

    View Slide

  33. The Traffic Light Paradox

    View Slide

  34. Drachten, Netherlands 2000

    View Slide

  35. Drachten, Netherlands 2001

    View Slide

  36. 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 Slide

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

    View Slide

  38. Case in Point...

    View Slide

  39. 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 Slide

  40. 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 Slide

  41. 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 Slide

  42. Linters are great for catching
    trailing commas

    View Slide

  43. But they suck at nuance

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  48. GWT
    is to
    so that

    View Slide

  49. is to
    as

    View Slide

  50. It’s a bit like automatic transmission

    View Slide

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

    View Slide

  52. but you sacrifice control

    View Slide

  53. machines and bad programmers
    lack nuance

    View Slide

  54. use both with caution

    View Slide

  55. Fearmongering

    View Slide

  56. View Slide

  57. Fearmongers assume the worst of everyone

    View Slide

  58. Fearmongers assume we are all idiots

    View Slide

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

    View Slide

  60. 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 Slide

  61. View Slide

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

    View Slide

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

    View Slide

  64. This is terrible advice.

    View Slide

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

    View Slide

  66. JavaScriptophobia

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  71. JavaScripty Transpilers convert syntax
    Little or no signal loss

    View Slide

  72. 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 Slide

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

    View Slide

  74. “The dog ate my semicolons”

    View Slide

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

    View Slide

  76. View Slide

  77. Fear is the Enemy of Creativity

    View Slide

  78. Ideology

    View Slide

  79. Ideology is mostly tribalism

    View Slide

  80. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  84. “Comma first prevents trailing commas”

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  91. But, “comma last prevents leading commas”

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  98. 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 Slide

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

    View Slide

  100. Absolutism

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  105. 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 Slide

  106. Guidelines mistaken for rules...

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  110. View Slide

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

    View Slide

  112. Hope

    View Slide

  113. "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 Slide

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

    View Slide

  115. This doesn’t make good coders

    View Slide

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

    View Slide

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

    View Slide

  118. 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 Slide

  119. The End
    @angustweets

    View Slide

  120. 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 Slide

  121. 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 Slide