Transcript
Page 1: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Ustream vs Legacy

@matenadasdi

Page 2: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Ustream basics

80.000.000 visitors / month

5.000.000+ viewer hours on tough days

Page 3: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Legacy is not bad code, it’s just old or over-iterated

Page 4: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Everyone starts in the dark…

but there is always light in the end of the tunnel

Page 5: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Our goal was to achieve

more stability and scalability

in our frontend codebase

Page 6: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Our situation was (like every frontend codebase some years ago)

no tests

no modules

all-in-one feature based class system

poor abstraction

JSLint as “code quality tools”

Page 7: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

It’s a new ERAit’s a heaven of tools and new solutions

for frontend developers nowadays

Page 8: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

we had 5 stages in our long journey

it may help or inspire you too

Page 9: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Structure

Page 10: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

separate business logic from its representation

easy isolation

dependency injection

we had to create / use a new structure

There are some must-have things for testing…

Page 11: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

not the framework, the way of thinking matters

Page 12: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

always think in layers with separated responsibilities

Page 13: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Sync Socket (full-duplex)Async

get data set data

DOM

AJAX WebStorage Cookies Socket Ustream Flash APILongpoll Embeds

Views

manipulates events

Controllers

control notify

Logics Models

get data set data

Page 14: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

our new small framework is under 10kb

but we had millions of lines written in an old style

Page 15: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

don’t be afraid to start the hardcore continuous integration

Page 16: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Testing

Page 17: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Mocha + Chaiframework:

Node and Browser support

Separated assert libraries

Tons of reporters

mocking:

SinonJS

Spies, Stubs, Mocks

Assertions for invocations

wide framework support

Faking AJAX, server

module dependency mocking:

SquireJS

Dependency injector for

RequireJS

mock / store

Unit testing

Page 18: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Unit testing is a must in every architecture

but it’s not enough for client side code!

Page 19: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Testing real browser functionality with mocking

and simulating the DOM can be a pain in the …

Page 20: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Node.js based navigation scripting

PhantomJS / SlimerJS support

screenshot capture

you can skip or use your own testing framework

Solution: CasperJS

Page 21: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Right now we have:

parallel execution thanks to an own grunt task solution

Tools based on Casper: Screenshot comparison tool, regression testing (PhantomCSS)

own testing wrapper layer with different presets and transparent modules. User.login(), etc.

It’s really flexible and easy to customize!

Page 22: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Automation

Page 23: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Manual processes simply won’t work…

but there is an easy cure!

Page 24: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

GRUNT ||  GULP

we use Grunt!

Why not gulp?

Bad question, both are awesome, move on, and pick one!:)

Page 25: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

we could migrate our old PHP / Ruby / etc. based frontend jobs to Node

there is a transparent layer for every frontend related task

thanks to our dynamic GruntFile.js solution, adding new tasks is fast

Thanks to Grunt

Page 26: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

CI integration is important!

with an automation layer, it’s easy to do

Page 27: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Rules & standards insurance for the future

Page 28: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Follow your rules, because if you break them,

they are not rules anymore…

Page 29: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Code style!

Page 30: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

JSHint !

!pros:

.jshintrc

huge community

wide IDE / Text editor integration

grunt / gulp plugins

!

cons:

still regexp based (JSLint fork)

not pluginable

nearly impossible to write semantic rules

Page 31: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

ESLint

!

pros:

pluginable

tons of new rules

rapidly growing community

semantics

ESPRIMA

growing IDE / Text editor integration

!

cons:

you can tell maybe!

Page 32: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

We’ve created our own rules

Page 33: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Complexity?

Lines of code (LOC)

Halstead indexes

Maintainability index

Cyclomatic complexitylinearly independent paths in the method

Page 34: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

JSComplexity & Plato

We run complexity report in Jenkins nightly build for our whole JS codebase

https://www.npmjs.org/package/complexity-report

Plato is a great tool for manual examinations

https://github.com/es-analysis/plato

Page 35: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Use your CI or Git hooks to force your rules & standards

Page 36: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Modules, modules!

Page 37: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

async module loading

dependency injection

project based SOA workflow, we have to avoid code duplication in

several repos/projects

Why we’ve started our modularisation marathon

Page 38: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Do not worry! Code modifications can be automated! http://esprima.org/

{ "type": "Program", "body": [ { "type": "VariableDeclaration", "declarations": [ { "type": "VariableDeclarator", "id": { "type": "Identifier", "name": "city" }, "init": { "type": "Literal", "value": "istanbul", "raw": "'istanbul'" } } ], "kind": "var" }, { "type": "IfStatement", "test": { "type": "BinaryExpression", "operator": "===", "left": { "type": "Identifier", "name": "city" }, "right": { "type": "Literal", "value": "istanbul", "raw": "'istanbul'" } }, "consequent": { "type": "BlockStatement",

var city = ‘istanbul’; !if (city === ‘istanbul’) { conf = 'jsist'; }

Page 39: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

NPM modules? Maybe private ones?

more!

Page 40: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

private repo server: Sinopia

internal GitLab repos for each package

grunt release task for NPM module release

NPM in private

https://github.com/geddski/grunt-release https://github.com/boennemann/grunt-semantic-release

Page 41: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Why?

we can manage our dependencies in different projects / services

separated tests & documentation for each module

we also use NPM for non-node package management

Yeoman for automated project configuration

Page 42: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

That’s not dark anymore!:)

This is where we are right now!

Page 43: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

What we’ve achieved so far

600+ modules created

hundreds of unit tests and Casper tests, and growing rapidly

new and important core features are moved to the new structure

we started to create our private NPM modules like hell!

ready for Async module loading

Page 44: Ustream vs Legacy, It's never too late to start your fight! #Jsist 2014

Remember: It’s never too late!

@matenadasdi


Top Related