Download - Best practices for JavaScript RIAs
![Page 1: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/1.jpg)
Best practices for JavaScript RIAs
Carlos Ble | www.carlosble.com | @carlosble
Thanks to MadridJS.org
January 2013
![Page 2: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/2.jpg)
Know the history
![Page 3: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/3.jpg)
Table of contents● Is it an application or is it a website?
● Is it a library, a framework, or application code?
● Am I test-driving the code or not?
● Testing tools
● Testing techniques
● Design patterns and architecture
● Client-Sever?
![Page 4: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/4.jpg)
What kind of JavaScript are you writing?
● For frameworks and libraries:
if (someVariable === undefined)...
● For applications, without automated tests:
● Test-first JavaScript: if (someVariable)... - Duck typing is OK. - No need for lots of JavaScript patterns. - Write code for humans.
● Functional or Object Oriented? Just make it SOLID
![Page 5: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/5.jpg)
TDD with JavaScript
● Tddjs.com
● DirigidoPorTests.com/el-libro
![Page 6: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/6.jpg)
Testing tools
There are new tools every day!
Some tools I use, thanks to @pasku1 & @eamodeorubio (follow these guys):
Jasmine/Mocha Jasmine-node Chai – chaijs.com CasperJS / PhantomJS JsTestDriver
Believe me, tools are there to support concepts, they are not important themselves!
![Page 7: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/7.jpg)
Testing Rules &
Test-First Rules
● Test-first is absolutely different from testing.
● Do not mock artifacts you don't own.
● Use stubs for queries and mocks/spies for actions.
● Exploratory testing is always necessary.
![Page 8: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/8.jpg)
Widgets are objects, not pictures
![Page 9: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/9.jpg)
Events: DOM level 0 – Traditional model(one2one: a nice kind of dependency injection)
Events: Observer (one2many) & Pub/Sub (many2many)
![Page 10: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/10.jpg)
Passive View
http://martinfowler.com/eaaDev/PassiveScreen.html
![Page 11: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/11.jpg)
Factory
Singleton
More patterns: http://addyosmani.com/resources/essentialjsdesignpatterns/book
Singleton
![Page 12: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/12.jpg)
Optimal code for machinesis hard to read for humans
● Don't write code for machines, write it for humans
● Do you really have performance metrics?
● Google Closure Compiler
● CoffeeScript
● Do performance testing often
![Page 13: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/13.jpg)
Smart client, dumb server● Let the client side application contain all the business logic you can.
● Keep the server just as an event bus for clients to interact with each other.
● Examples: - TeamMonitor in LiveTeamApp.com - Skype and TeamViewer clients can connect directly between them (OK, this is not JavaScript).
● Advantages: - Ease of testing. - Ease of maintenance. - Scalability.
![Page 14: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/14.jpg)
Sample app: LiveTeamApp
![Page 15: Best practices for JavaScript RIAs](https://reader035.vdocuments.site/reader035/viewer/2022062511/54b6b11f4a795988158b457d/html5/thumbnails/15.jpg)
Conclusions● JavaScript is too big. Consider the context to make decisions.
● Retrieve best practices from the desktop development age, those days back in the 90's.
● Read books, the good ones don't get old.
● Try to understand the concepts, not just the tools.
● You usually don't need frameworks you need libraries.
● Care about your code and your tests . Test-drive your code.