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

Improvisational JavaScript

Garann Means
October 07, 2012

Improvisational JavaScript

In the book Bossypants, Tina Fey gives three rules of improv: 1. Always say yes, 2. Say yes, and, 3. Make statements. These rules are a damn good approach to dealing with the unexpected in comedy or anything else. Error handling in JavaScript, for example. The way JS deals with exceptions is already a little unpredictable - instead of fighting that, let’s take advantage of it to create better user experiences, more powerful services, and room for hacks and happy accidents.

Garann Means

October 07, 2012
Tweet

More Decks by Garann Means

Other Decks in Technology

Transcript

  1. howdy Garann Means, @garannm engineer at Etsy Austin, Texas Node

    for Front-End Developers Austin All-Girl Hack Night, Girl Develop It Austin
  2. “bossypants” by Tina Fey 30 Rock, Saturday Night Live, Mean

    Girls hilarious best-seller success without infallibility
  3. fallibility e.g. Liz Lemon empathetic character a caricature of Ms.

    Fey conflict inherent in doing something without knowing if it’s the right thing == lulz!
  4. javascript infallible! fallibility comes from the developer failure to account

    for edge cases ignorance of user “works for me” mindset
  5. key difference fallibility in Tina Fey characters: hilarious relatable fallibility

    in JavaScript: not funny users/developers run away screaming
  6. comedy cope with uncertainty, even tragedy guide to dealing with

    humans adjusts expectations of how the world works computers rely on us to communicate this, using our observations
  7. rules of improv “Will Change Your Life and Reduce Belly

    Fat *” 1. AGREE 2. SAY YES, AND 3. MAKE STATEMENTS 4. THERE ARE NO MISTAKES
  8. improv for machines machines like Yes and No all things

    must be true or false grey areas need algorithms grey areas approximated by developers instead of programming right answers, we program ways to find right answers
  9. AGREE all answers are saved even incomplete gibberish even illegal

    strings nothing executed, just safely cached
  10. MAKE STATEMENTS work with what you know e.g. show a

    preview user profile highlight incomplete data separately allow continued, sandboxed progress
  11. NO MISTAKES suggest a stronger password make it a step

    to more privileges suggest a unique username show user ID until then
  12. hopefully not news ideas already exist in UI philosophy JavaScript

    can do more. dynamic typing schema-less data stores flexibility unique to our language and sphere of implementation
  13. improv.js your JavaScript is an improv partner a framework a

    transpiler machine learning JavaScript don’t give a
  14. AGREE JS has no method overloading i.e., JS has infinite

    method overloading! function foo can do several things use arguments to decide which
  15. agreeing in the real world jQuery’s $() $( `#myThing` );

    $( document.getElementById( myThing ) ); $( this ); $( `<div id=”myThing”></div>` ); same result for all
  16. SAY YES, AND is there a callback? execute that callback

    can’t tell? go back to AGREE-ment at minimum, return chainable context object
  17. affirmative return values design functions to alter a context always

    be next()ing JavaScript would like to talk your ear off
  18. MAKE STATEMENTS create helpful instances include the most information reasonable

    don’t hide information once set clarify what is application state and what is object state
  19. imperative states return instances automatically set smart defaults allow access

    to the best picture of state possible from any object JS doesn’t know what it’s doing, but it’s doin it!
  20. real world imperatives the good ol’ DOM all properties exist

    add more properties easily each element has its own state browser’s DOM API unaffected
  21. NO MISTAKES try..catch should be a last resort if private

    property is accessible, it isn’t private type casting should be the norm, not an error AGREE or return this and GTFO
  22. no-error error handling can’t run foo() with input bar? check

    for type, properties errors are the result of a developer failing to realize a use case JS just wants to make you happy
  23. real world error handling try to execute an XHR timeouts

    wait, try again n times 500 error try other methods
  24. when things just fail JS has a thing for that

    error-first callbacks (or arguments) if an error callback isn’t supplied, gets swallowed developer opts in to error handling
  25. the golden rule code it like you have to use

    it frameworks transpilers UIs any tool you create
  26. agreeable functions function’s goal is reuse good functions are decoupled

    functions usable in multiple ways will be used more people like useful functions
  27. affirmative return values return true; === return “derp derp derp”;

    worst case? goes unused fewer modern functions are pure utilities async? return promises or subscribers functions that leave you hanging suck
  28. imperative states clear and controllable picture of app expose carefully

    chosen, useful information protect abstractions for convenience, not out of distrust “that’s a good-lookin object!”
  29. no-error error handling exceptions are useless nowhere to go from

    a dead stop multiple ways to provide opt-in error information developers know what they want, and JS wants to give it to them
  30. errors become useless once you deploy to prod encourage narrower

    definitions of acceptable instead of broader ones every error thrown costs empathy points
  31. the golden rule if your code is not a good

    improv partner, code and developer both fail code is meant to be built upon someone will use your code in a way you don’t expect
  32. language features loosely defined function signatures ad-hoc object states, visibility

    no need to declare exceptions blurred line between users and developers
  33. the future ES6, TypeScript, Dart? you can write JS in

    any language now JavaScript creates unique opportunities JS is comedy in a world of drama deals well with uncertainty
  34. uncertainty is hard encouraging good coding practices chaining without object

    type guarantees potentially limitless state hierarchies continuing after errors in a useful state
  35. JavaScript wants to be hilarious predictability is boring “legality” is

    boring forcing others to conform to your expectations is boring the way JS works is the way it works best