Looking at the principle of abstraction and implementation hiding as it pertains to JavaScript apps, in the context of 2014's framework soup. Originally given as a +rehabstudio tech talk.
- Ill-conceived library design - Testing considerations - Deprecation All of these can force a change in the base libraries used by the app, mid-development = PAIN Ass-biters of core library dependency
parts of our application logic Problem Solution Hide the base libraries behind an intermediate layer, and write our application logic against this layer instead
- Ninjitsu training - Look at pictures of bats - Pretend to be a playboy idiot - Mourn parents - Fall in love with bad girls I’M THE GODDAMN BATMAN = Abstraction hides the specifics of what and how things are done behind a simplified facade presented to the outside world.
all the toys I got from Bruce - Restore the Bat-Signal - Gargle with Jack Daniels and thumbtacks every night - Put lifts in my boots I’M THE GODDAMN BATMAN = It doesn’t matter to the outside world what goes on behind the facade, provided the facade itself stays the same.
a security and privacy risk If people knew he was Batman, he couldn’t easily switch the role to someone else If people knew his underlying methods, it would be much easier to compromise or even break him altogether Why does Batman hide behind a facade?
- Prevents unwanted meddling with the core - Promotes careful architecture design - Forces us to think about the libs and tools we use* - Can make testing easier * ‘Why don’t we just use document.getElementById()?’ - CG Why abstract?
- make XHR GET requests - hide and show elements - do Canvas work - whatever - Choose the right tools for the job - e.g We don’t need jQuery *just* for XHR - Identify where things are most likely to change - Make your method names agnostic where possible - e.g. ‘makeCircle’ vs ‘gsapCircle’ Designing the facade
- Potential for scope change / creep is high - Using new or experimental libraries and tech When it makes sense When it (probably) doesn’t - The core architectural framework (e.g. Backbone) - Performance-critical work - There is The One Library (e.g. Google Maps) - The cost of implementation outweighs the benefits
a specification for potential replacements - Common libraries can be grouped via a facade module into a single require() call - Just thinking about how you would abstract an in-use library will help you see how little of it you’re (probably) using Quick wins