event4 better javascript

18
Write Better Javascript Event #4 Aymeric Gaurat Apelli Senior Developer

Upload: aymeric-gaurat-apelli

Post on 05-Dec-2014

329 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Event4   better javascript

Write Better JavascriptEvent #4

Aymeric Gaurat Apelli Senior Developer

Page 2: Event4   better javascript

Who am I? Why a “Who am I?”

• Aymeric Gaurat-Apelli• .NET developer/consultant since first beta• Worked in France, Australia (Readify) and Thailand• Web entrepreneur • Facebook games (1M users/day)• http://taskarmy.com• http://weekplan.net (knockout.js SPA)

• Principal developer at Jetabroad (Thailand)

Page 3: Event4   better javascript

“Classes”Yes you can… kind of.

Want inheritance? http://ejohn.org/blog/simple-javascript-inheritance/

Page 4: Event4   better javascript

Coding principles

DRYDon’t repeat yourself

KISSSimplicity (and avoiding complexity) should always be a key goal.

YAGNIYou aren’t going to need it.

They apply to Javascript too!

Page 5: Event4   better javascript

Coding principles

LAW OF DEMETERCode components should only communicate with their direct relations

SRPA component of code (e.g. class or function) should perform a single well defined task.

DEPENDENCY INJECTIONDepend upon Abstractions. Do not depend upon Concretions.

Page 6: Event4   better javascript

Suggestion:http://jsfiddle.net/XejEY/1/

How would you rewrite this code?

Page 7: Event4   better javascript

JavascriptFrameworksMore structure for your code

Check outhttp://todomvc.com/

Page 8: Event4   better javascript

Templatingengines

Choose a template engine

http://jsfiddle.net/aPv9H/1/

Allow you to reuse snippets of HTML and to bind it to your data

Fast templating engine.

Page 9: Event4   better javascript

ScopeA bit different from C# http://madebyknight.com/javascript-scope/

http://jsfiddle.net/Ukv2R/1/

There is no block level scope.

Page 10: Event4   better javascript

ModulesThe solution to spaghetti code

Page 11: Event4   better javascript

Sample home grown module

Page 12: Event4   better javascript

AMD, the standard.

Popular AMD module loader: requirejs

Page 13: Event4   better javascript

Dependencies versus events

Page 14: Event4   better javascript

Dependencies versus eventsAfter refactoring

Router.js module

Page 15: Event4   better javascript

Unit testinghttp://qunitjs.com/intro/

http://pivotal.github.io/jasmine/

Page 16: Event4   better javascript

Random tips

1. Link to your javascript code at the end of your HTML document

2. Bundle and minify

3. Work with css classes instead of working with styling/animations

Example

Page 17: Event4   better javascript

Resourcesto learn

http://microjs.com/Fantastic micro libraries to use and learn from

http://dailyjs.comBlog about new stuff in the Javascript world

http://github.comSearch for javascript projects

http://terrariumjs.wiselabs.net

Page 18: Event4   better javascript

Thank you