torsdag den 13. oktober 11 torsdag den 13. oktober 11 torsdag den - - PowerPoint PPT Presentation

torsdag den 13 oktober 11 torsdag den 13 oktober 11
SMART_READER_LITE
LIVE PREVIEW

torsdag den 13. oktober 11 torsdag den 13. oktober 11 torsdag den - - PowerPoint PPT Presentation

torsdag den 13. oktober 11 torsdag den 13. oktober 11 torsdag den 13. oktober 11 torsdag den 13. oktober 11 torsdag den 13. oktober 11 http://www.doxdesk.com/updates/2009.html torsdag den 13. oktober 11 Design, techniques and tools for


slide-1
SLIDE 1

torsdag den 13. oktober 11

slide-2
SLIDE 2

torsdag den 13. oktober 11

slide-3
SLIDE 3

torsdag den 13. oktober 11

slide-4
SLIDE 4

torsdag den 13. oktober 11

slide-5
SLIDE 5

torsdag den 13. oktober 11

slide-6
SLIDE 6

http://www.doxdesk.com/updates/2009.html

torsdag den 13. oktober 11

slide-7
SLIDE 7

Design, techniques and tools

for larger JavaScript applications

Karl Krukow (kkr@trifork.com), Goto Amsterdam, Oct. 13th, 2011

torsdag den 13. oktober 11

slide-8
SLIDE 8

About me

PhD, University of Aarhus, Theory-stuff :) Working at Trifork for about 5 years on Web, JavaScript, Java/JEE, Ruby/Rails, Clojure, Mobile, Conferences and Training. Last two years on iOS on Trifork in the financial sector. Recently part of a start-up doing mobile automated testing: LessPainful ApS (http:/

/www.lesspainful.com)

torsdag den 13. oktober 11

slide-9
SLIDE 9

torsdag den 13. oktober 11

slide-10
SLIDE 10

Have you ever found yourself in a project like this...

torsdag den 13. oktober 11

slide-11
SLIDE 11

Development Time

torsdag den 13. oktober 11

slide-12
SLIDE 12

torsdag den 13. oktober 11

slide-13
SLIDE 13

torsdag den 13. oktober 11

slide-14
SLIDE 14

torsdag den 13. oktober 11

slide-15
SLIDE 15

Development Time

Unstructured No clear architecture No consistency

torsdag den 13. oktober 11

slide-16
SLIDE 16

Runtime

torsdag den 13. oktober 11

slide-17
SLIDE 17

torsdag den 13. oktober 11

slide-18
SLIDE 18

torsdag den 13. oktober 11

slide-19
SLIDE 19

Unless we do something: The way we organize our source code at development time... Significantly affect its behavior at runtime!

With JavaScript

torsdag den 13. oktober 11

slide-20
SLIDE 20

Server side

torsdag den 13. oktober 11

slide-21
SLIDE 21

Server side

We often have non-functional requirements

Maintainability, extensibility, understandability, quality, Productivity, Performance, ...

torsdag den 13. oktober 11

slide-22
SLIDE 22

Server side

We often have non-functional requirements

Maintainability, extensibility, understandability, quality, Productivity, Performance, ...

We have techniques and tools to help

Architecture, modularity, reuse, separation of concerns automated testing & continuous integration Tool support (static analysis, compilers, IDEs, profilers)

torsdag den 13. oktober 11

slide-23
SLIDE 23

Why so different?

torsdag den 13. oktober 11

slide-24
SLIDE 24

Some key properties of JavaScript as a language

Linkage of different scripts/modules via global variables. Delivered as source code, as opposed to executable binaries.

Compilation is done by browser: compiles scripts as it receives them

Dynamically typed Dynamic Objects (general containers) Prototypal inheritance

Objects inherit from objects (no classes)

torsdag den 13. oktober 11

slide-25
SLIDE 25

JavaScript in the large

torsdag den 13. oktober 11

slide-26
SLIDE 26

JavaScript in the large

JavaScript(ECMA-262 3rd edition)is focused on flexibility and ease of use:

poorly suited for writing large & complex applications.

torsdag den 13. oktober 11

slide-27
SLIDE 27

JavaScript in the large

JavaScript(ECMA-262 3rd edition)is focused on flexibility and ease of use:

poorly suited for writing large & complex applications.

Here are some of the problems for large-scale use

language flaws (e.g., type conversion, scope rules, numbers...) global namespace / missing module system (packages) missing encapsulation everything is mutable - even methods on objects program information hard to use by tools (e.g., static typing)

torsdag den 13. oktober 11

slide-28
SLIDE 28

“Larger” projects

(whatever that means)

torsdag den 13. oktober 11

slide-29
SLIDE 29

“Larger” projects

(whatever that means)

This may or may not be a problem for you, except when your app scales in one or more of:

torsdag den 13. oktober 11

slide-30
SLIDE 30

“Larger” projects

(whatever that means)

This may or may not be a problem for you, except when your app scales in one or more of: size and complexity

torsdag den 13. oktober 11

slide-31
SLIDE 31

“Larger” projects

(whatever that means)

This may or may not be a problem for you, except when your app scales in one or more of: size and complexity development team: size and composition (new people, different skills)

torsdag den 13. oktober 11

slide-32
SLIDE 32

“Larger” projects

(whatever that means)

This may or may not be a problem for you, except when your app scales in one or more of: size and complexity development team: size and composition (new people, different skills) time: stretches over years of development and maintenance.

torsdag den 13. oktober 11

slide-33
SLIDE 33

Theme of this talk: What can we do?

torsdag den 13. oktober 11

slide-34
SLIDE 34

Theme of this talk: What can we do?

Rationale part (“the theory”)

intro: helpful technologies and techniques

Practical part (“the code”):

  • pen source sample application with a strict architecture

and a JavaScript tool-chain.

a modular MVC architecture (using custom events) advanced tooling (compilation, type-check, lazy-loading) automated testing (unit, integration and functional)

torsdag den 13. oktober 11

slide-35
SLIDE 35

Compiler+VM Technology

torsdag den 13. oktober 11

slide-36
SLIDE 36

Compiler+VM Technology

“JavaScript is assembly language for the web”

  • Eric Meijer

torsdag den 13. oktober 11

slide-37
SLIDE 37

Compiler+VM Technology

Google Dart ClojureScript (Clojure -> JS) Cappuccino (Objective-J -> JS) CoffeeScript (lightweight, “local” JS compilation) Google Closure Compiler (JS -> JS), Traceur (JS.Next->JS) Google Web Toolkit (Java -> JS) JSIL (.NET/CIL -> JS) ...

“JavaScript is assembly language for the web”

  • Eric Meijer

torsdag den 13. oktober 11

slide-38
SLIDE 38

Differences in compilers

torsdag den 13. oktober 11

slide-39
SLIDE 39

Differences in compilers

“whitespace” pretty print whole-program optimizing

  • ptional typing, multiple targets

local optimization

  • aka. “minification”

torsdag den 13. oktober 11

slide-40
SLIDE 40

Differences in compilers

“whitespace” pretty print whole-program optimizing

  • ptional typing, multiple targets

jsmin

local optimization

  • aka. “minification”

YUICompres, uglifyjs closure simple mode

closure compiler advanced mode, dartc Clojure- Script

torsdag den 13. oktober 11

slide-41
SLIDE 41

This talk assumes you are staying in JavaScript.

torsdag den 13. oktober 11

slide-42
SLIDE 42

Google Closure Compiler

torsdag den 13. oktober 11

slide-43
SLIDE 43

Google Closure Compiler

Extensible optimizing JavaScript-to-JavaScript compiler

torsdag den 13. oktober 11

slide-44
SLIDE 44

Google Closure Compiler

Extensible optimizing JavaScript-to-JavaScript compiler Several modes - the interesting one is “advanced mode”.

whole-program analysis (all js files + extern must be supplied).

  • ptimizations (e.g., dead-code elimination, constant folding/

propagation)

  • ptional type-checking, @private access, ...

lazy-loading of modules

torsdag den 13. oktober 11

slide-45
SLIDE 45

Google Closure Compiler

Extensible optimizing JavaScript-to-JavaScript compiler Several modes - the interesting one is “advanced mode”.

whole-program analysis (all js files + extern must be supplied).

  • ptimizations (e.g., dead-code elimination, constant folding/

propagation)

  • ptional type-checking, @private access, ...

lazy-loading of modules

Requires writing JavaScript code in a particular way.

torsdag den 13. oktober 11

slide-46
SLIDE 46

Google Closure Compiler

Extensible optimizing JavaScript-to-JavaScript compiler Several modes - the interesting one is “advanced mode”.

whole-program analysis (all js files + extern must be supplied).

  • ptimizations (e.g., dead-code elimination, constant folding/

propagation)

  • ptional type-checking, @private access, ...

lazy-loading of modules

Requires writing JavaScript code in a particular way. Very under-appreciated! Not just yet another minifier!

torsdag den 13. oktober 11

slide-47
SLIDE 47

Typical JS Application composition

Code actually used at runtime Total amount of code

torsdag den 13. oktober 11

slide-48
SLIDE 48

Typical JS Application composition

Code actually used at runtime Total amount of code Your code

torsdag den 13. oktober 11

slide-49
SLIDE 49

Typical JS Application composition

Code actually used at runtime Total amount of code Your code Libraries

torsdag den 13. oktober 11

slide-50
SLIDE 50

ç ç

Typical JS Application composition

Code actually used at runtime Total amount of code Your code Libraries

torsdag den 13. oktober 11

slide-51
SLIDE 51

Local optimization

ç ç

torsdag den 13. oktober 11

slide-52
SLIDE 52

Local optimization

ç ç ç ç

torsdag den 13. oktober 11

slide-53
SLIDE 53

Whole program analysis & Closure Tools

ç ç

torsdag den 13. oktober 11

slide-54
SLIDE 54

Whole program analysis & Closure Tools

ç ç ç

  • App. base modules

torsdag den 13. oktober 11

slide-55
SLIDE 55

Whole program analysis & Closure Tools

ç ç ç ç

  • App. base modules

Other Modules loaded by need

torsdag den 13. oktober 11

slide-56
SLIDE 56

Closure Library

torsdag den 13. oktober 11

slide-57
SLIDE 57

Closure Library

HUGE JavaScript library used by Google.

Symbiotic with closure compiler advanced mode, which makes it SMALL when used with compiler.

torsdag den 13. oktober 11

slide-58
SLIDE 58

Closure Library

HUGE JavaScript library used by Google.

Symbiotic with closure compiler advanced mode, which makes it SMALL when used with compiler.

Highlights (apart from expected of a JS library)

Dependency/module system goog.provide, goog.require. “Interfaces”, “Enums”, “Class”-based inheritance system. (custom) event system supporting bubbling, capture, preventDefault and cancel. extensible UI components with a well-defined lifecycle, and which support custom events.

torsdag den 13. oktober 11

slide-59
SLIDE 59

JSDoc annotations and tooling

torsdag den 13. oktober 11

slide-60
SLIDE 60

JSDoc annotations and tooling

Several advantages to using JSDoc: Standardized => tool support Google uses JSDoc annotations for “talking” to the compiler (e.g., denoting types and access/visibility). Many IDEs understand them, and can help with warnings, code-completion, inline documentation. Makes you document your code and think about what your public API is.

torsdag den 13. oktober 11

slide-61
SLIDE 61

JSDoc annotations and tooling

Several advantages to using JSDoc: Standardized => tool support Google uses JSDoc annotations for “talking” to the compiler (e.g., denoting types and access/visibility). Many IDEs understand them, and can help with warnings, code-completion, inline documentation. Makes you document your code and think about what your public API is. Example IDEs: WebStorm, RubyMine, Spket, Aptana

torsdag den 13. oktober 11

slide-62
SLIDE 62

JSDoc annotations and tooling

Several advantages to using JSDoc: Standardized => tool support Google uses JSDoc annotations for “talking” to the compiler (e.g., denoting types and access/visibility). Many IDEs understand them, and can help with warnings, code-completion, inline documentation. Makes you document your code and think about what your public API is. Example IDEs: WebStorm, RubyMine, Spket, Aptana

torsdag den 13. oktober 11

slide-63
SLIDE 63

Automated Testing

js-test-driver (http:/

/code.google.com/p/js-test-driver/) unit tests executing in real browsers ide and commandline SinonJS for spies,stubs,mocks

Selenium Webdriver (“selenium 2.0”)

De-facto standard for automated functional/acceptance testing.

torsdag den 13. oktober 11

slide-64
SLIDE 64

JavaScript Application Architecture

In the past few years there have been focus

  • n JavaScript application architecture.

People like: Nicholas Zakas (ex YAHOO, JS app arch) Rebecca Murphey (jQuery vs “enterprise”) Peter Michaux (MVC Architecture) Ray Ryan (Google, GWT app arch.)

torsdag den 13. oktober 11

slide-65
SLIDE 65

Common theme

torsdag den 13. oktober 11

slide-66
SLIDE 66

Common theme

Modularity - reusable modules, loose coupling. clearly def. public interface and private state+functions

torsdag den 13. oktober 11

slide-67
SLIDE 67

Common theme

Modularity - reusable modules, loose coupling. clearly def. public interface and private state+functions Strict file organization - matches modules

Many small files, each file one concern (like good class design)

torsdag den 13. oktober 11

slide-68
SLIDE 68

Common theme

Modularity - reusable modules, loose coupling. clearly def. public interface and private state+functions Strict file organization - matches modules

Many small files, each file one concern (like good class design)

Separation of concerns (e.g., MVC, MVP)

view management, communication/network access, domain model,...

torsdag den 13. oktober 11

slide-69
SLIDE 69

Common theme

Modularity - reusable modules, loose coupling. clearly def. public interface and private state+functions Strict file organization - matches modules

Many small files, each file one concern (like good class design)

Separation of concerns (e.g., MVC, MVP)

view management, communication/network access, domain model,...

Custom events

extend the DOM Level 2 event model to app-specific events

torsdag den 13. oktober 11

slide-70
SLIDE 70

A note on custom events

torsdag den 13. oktober 11

slide-71
SLIDE 71

A note on custom events

Custom events: like DOM-Level 2 except App.-specific and - generated Logical rather than physical

torsdag den 13. oktober 11

slide-72
SLIDE 72

A note on custom events

Custom events: like DOM-Level 2 except App.-specific and - generated Logical rather than physical Bubbles and cancels just like DOM events.

torsdag den 13. oktober 11

slide-73
SLIDE 73

A note on custom events

Custom events: like DOM-Level 2 except App.-specific and - generated Logical rather than physical Bubbles and cancels just like DOM events. Helps with loose coupling

torsdag den 13. oktober 11

slide-74
SLIDE 74

A note on custom events

Custom events: like DOM-Level 2 except App.-specific and - generated Logical rather than physical Bubbles and cancels just like DOM events. Helps with loose coupling

browser component application

Concrete Abstract

torsdag den 13. oktober 11

slide-75
SLIDE 75

A note on custom events

Custom events: like DOM-Level 2 except App.-specific and - generated Logical rather than physical Bubbles and cancels just like DOM events. Helps with loose coupling

browser component application

mouse (x,y,btn) autocomplete select item (item) user selected (u)

Concrete Abstract

torsdag den 13. oktober 11

slide-76
SLIDE 76

MVC

(Yes, again)

Model Singleton View Singleton Controller Singleton Dependency

torsdag den 13. oktober 11

slide-77
SLIDE 77

MVC

(Yes, again)

Model Singleton View Singleton Controller Singleton Dependency Custom Events

torsdag den 13. oktober 11

slide-78
SLIDE 78

MVC

(Yes, again)

Model Singleton View Singleton Controller Singleton Component domain objects controllers Dependency Custom Events

torsdag den 13. oktober 11

slide-79
SLIDE 79

MVC

(Yes, again)

Model Singleton View Singleton Controller Singleton Component domain objects controllers Dependency Custom Events

torsdag den 13. oktober 11

slide-80
SLIDE 80

How to do it in practice?

torsdag den 13. oktober 11

slide-81
SLIDE 81

Example: chatroom++

torsdag den 13. oktober 11

slide-82
SLIDE 82

Example: chatroom++

Communication uses WebSockets+Stomp on Torquebox

Messages are ‘instant’ & can be transactional with reliable delivery (JMS via STOMP on HornetQ).

torsdag den 13. oktober 11

slide-83
SLIDE 83

Example: chatroom++

Communication uses WebSockets+Stomp on Torquebox

Messages are ‘instant’ & can be transactional with reliable delivery (JMS via STOMP on HornetQ).

JavaScript Tool-chain Google closure compiler and linter/checker. Jetbrains’ RubyMine as IDE (JSDoc comments, code “intelligence”) functional tests: selenium-webdriver for automating browsers unit tests: js-test-driver + SinonJS

torsdag den 13. oktober 11

slide-84
SLIDE 84

Example: chatroom++

Communication uses WebSockets+Stomp on Torquebox

Messages are ‘instant’ & can be transactional with reliable delivery (JMS via STOMP on HornetQ).

JavaScript Tool-chain Google closure compiler and linter/checker. Jetbrains’ RubyMine as IDE (JSDoc comments, code “intelligence”) functional tests: selenium-webdriver for automating browsers unit tests: js-test-driver + SinonJS JavaScript Client: Google Closure Library for compatibility with Closure compiler. Stomple JavaScript library (STOMP over websockets). Model-View-Controller with Custom Events. Lazy-module loading, strict file organization, optional types.

torsdag den 13. oktober 11

slide-85
SLIDE 85

DEMO

torsdag den 13. oktober 11

slide-86
SLIDE 86

Two Solutions Compared

One: jQuery, jQuery-UI, and stilts-stomp.js.

Minified using UglifyJS. No strict architecture, but does separate view and “everything else”, uses custom events.

Two: Google Closure Library and Stomple-0.99.

Both libraries are written to be compatible with Closure Compiler Advanced mode. Modular Model-View-Controller arch. using custom events.

torsdag den 13. oktober 11

slide-87
SLIDE 87

Development time

jQuery (233kB) jQuery-UI-min (367kB) stilts-stomp.js (7kB) app.js (two files) (6.3kB)

Total: 603,3 kB

Closure Library (approx. 1.2MB) Stomple-0.99.js (39,6kB) “App” (several files...) (35kB)

Total: ~ 1.3 MB

torsdag den 13. oktober 11

slide-88
SLIDE 88

Production

jQuery-min (90kB) jQuery-UI-min (197kB) app+stilts-stomp (3kB)

Total: 290 kB

torsdag den 13. oktober 11

slide-89
SLIDE 89

Production

jQuery-min (90kB) jQuery-UI-min (197kB) app+stilts-stomp (3kB) Closure compiled: 71kB

Total: 290 kB Total: 71 kB

torsdag den 13. oktober 11

slide-90
SLIDE 90

Production

jQuery-min (90kB) jQuery-UI-min (197kB) app+stilts-stomp (3kB) Closure compiled: 71kB

Total: 290 kB

Webkit Closure compiled: 64kB

Total: 71 kB

  • r optionally

torsdag den 13. oktober 11

slide-91
SLIDE 91

Summary

torsdag den 13. oktober 11

slide-92
SLIDE 92

Summary

JavaScript is not well-suited for large-scale application

  • development. We must add “something”

Requires much discipline, structure, convention. Poor tradition and literature about client-side architecture.

torsdag den 13. oktober 11

slide-93
SLIDE 93

Summary

JavaScript is not well-suited for large-scale application

  • development. We must add “something”

Requires much discipline, structure, convention. Poor tradition and literature about client-side architecture.

Tooling is not great, but there are tools that can help:

Compiler technologies are superior tools. There are significant differences in compiler techs. Closure is not just another minifier. Although not perfect, IDEs and testing tools are getting better.

torsdag den 13. oktober 11

slide-94
SLIDE 94

Summary

JavaScript is not well-suited for large-scale application

  • development. We must add “something”

Requires much discipline, structure, convention. Poor tradition and literature about client-side architecture.

Tooling is not great, but there are tools that can help:

Compiler technologies are superior tools. There are significant differences in compiler techs. Closure is not just another minifier. Although not perfect, IDEs and testing tools are getting better.

Custom events, MVC-like patterns, file-organization help with structuring your application.

torsdag den 13. oktober 11

slide-95
SLIDE 95

Summary

JavaScript is not well-suited for large-scale application

  • development. We must add “something”

Requires much discipline, structure, convention. Poor tradition and literature about client-side architecture.

Tooling is not great, but there are tools that can help:

Compiler technologies are superior tools. There are significant differences in compiler techs. Closure is not just another minifier. Although not perfect, IDEs and testing tools are getting better.

Custom events, MVC-like patterns, file-organization help with structuring your application. Example: https:/

/github.com/krukow/advanced_javascript_tooling

torsdag den 13. oktober 11

slide-96
SLIDE 96

torsdag den 13. oktober 11