Slide 1

Slide 1 text

Spaguetti com amêndoas Desenrolando seu código Javascript @ArthurGouveia

Slide 2

Slide 2 text

- O projeto é simples… …na sua cabeça preguiçosa

Slide 3

Slide 3 text

- É só botar uns “ífi”… …naquele(s) cenário(s) que você não previu

Slide 4

Slide 4 text

Só o Ctrl + F para achar… …aquele trecho de código no arquivo de 3000 linhas

Slide 5

Slide 5 text

- Reutilizar? Ctrl + C e Ctrl + V …pra colocar mais massa nesse Spaghetti Code

Slide 6

Slide 6 text

Os antigos sábios já diziam:

Slide 7

Slide 7 text

Namespaces
 Module Pattern
 Almond Os ingredientes que faltavam
 para desenrolar esse seu
 Spaghetti Code

Slide 8

Slide 8 text

Arthur Gouveia Software Engineer @ Reliant Solutions

Slide 9

Slide 9 text

Credits: https://commons.wikimedia.org/wiki/File:Pasta.jpg Namespaces ‘Global scope’ === ‘Danger’ Variable/Function name overwriting Third-party scripts, etc

Slide 10

Slide 10 text

Show me the code var chef = { name: 'Cuca', title: 'Mestre' }; whoIsTheChef(); changeChef(); whoIsTheChef(); function whoIsTheChef() { console.log(chef.title + ' ' + chef.name); } /* Random function from third-party script */ function changeChef() { chef = { name: 'Benta', title: 'Dona' }; }

Slide 11

Slide 11 text

Show me the code > Mestre Cuca > Dona Benta var chef = { name: 'Cuca', title: 'Mestre' }; whoIsTheChef(); changeChef(); whoIsTheChef(); function whoIsTheChef() { console.log(chef.title + ' ' + chef.name); } /* Random function from third-party script */ function changeChef() { chef = { name: 'Benta', title: 'Dona' }; }

Slide 12

Slide 12 text

Show me the code /* Single global object solution */ var KitchenApp = KitchenApp || {}; KitchenApp.chef = { name: 'Cuca', title: 'Mestre' }; whoIsTheChef(); changeChef(); whoIsTheChef(); function whoIsTheChef() { console.log(KitchenApp.chef.title + ' ' + KitchenApp.chef.name); } /* Random function from third-party script */ function changeChef() { chef = { name: 'Benta', title: 'Dona' }; }

Slide 13

Slide 13 text

Show me the code > Mestre Cuca > Mestre Cuca /* Single global object solution */ var KitchenApp = KitchenApp || {}; KitchenApp.chef = { name: 'Cuca', title: 'Mestre' }; whoIsTheChef(); changeChef(); whoIsTheChef(); function whoIsTheChef() { console.log(KitchenApp.chef.title + ' ' + KitchenApp.chef.name); } /* Random function from third-party script */ function changeChef() { chef = { name: 'Benta', title: 'Dona' }; } Unique ‘use strict’ won’t allow this

Slide 14

Slide 14 text

Show me the code /* Single global object solution + Object Literal Notation */ var KitchenApp = KitchenApp || {}; KitchenApp.Chef = { info: { name: 'Cuca', title: 'Mestre' }, whoIsTheChef: function() { console.log(this.info.title + ' ' + this.info.name); }, changeChef: function(chef_info) { this.info = chef_info; } }; KitchenApp.Chef.whoIsTheChef(); changeChef({ name: 'Ana Maria Braga', title: 'Dona' }); KitchenApp.Chef.whoIsTheChef(); KitchenApp.Chef.changeChef({ name: 'Ana Maria Braga', title: 'Dona' }); KitchenApp.Chef.whoIsTheChef(); /* Random function from third-party script */ function changeChef(chef_info) { chef = chef_info; }

Slide 15

Slide 15 text

Show me the code > Mestre Cuca > Mestre Cuca > Ana Maria Braga /* Single global object solution + Object Literal Notation */ var KitchenApp = KitchenApp || {}; KitchenApp.Chef = { info: { name: 'Cuca', title: 'Mestre' }, whoIsTheChef: function() { console.log(this.info.title + ' ' + this.info.name); }, changeChef: function(chef_info) { this.info = chef_info; } }; KitchenApp.Chef.whoIsTheChef(); changeChef({ name: 'Ana Maria Braga', title: 'Dona' }); KitchenApp.Chef.whoIsTheChef(); KitchenApp.Chef.changeChef({ name: 'Ana Maria Braga', title: 'Dona' }); KitchenApp.Chef.whoIsTheChef(); /* Random function from third-party script */ function changeChef(chef_info) { chef = chef_info; }

Slide 16

Slide 16 text

Source: http://www.freeimages.com/photo/kitchen-tools-1237100 Module Pattern Single Responsibility Principle Privacy (clean global scope) with IIFEs Independency & Reusability Locked state

Slide 17

Slide 17 text

Module 1. a separable component, frequently one that is interchangeable with others, for assembly into units of differing size, complexity, or function. Source: http://dictionary.reference.com/browse/module?s=t

Slide 18

Slide 18 text

Single Responsibility Principle

Slide 19

Slide 19 text

Show me the code var ingredientsForPasta = ['Pasta', 'Water', 'Salt']; // IIFE - Immediately Invoked Function Expression var KitchenApp = (function() { 'use strict'; // Isolated scope with access to Global console.log(ingredientsForPasta); var ingredientsForSauce = ['Tomato', 'Olive Oil', 'Basil']; console.log(ingredientsForSauce); }()); console.log(ingredientsForPasta); // Error: ingredientsForSauce is not defined // No access to variables defined inside de IIFE console.log(ingredientsForSauce);

Slide 20

Slide 20 text

Show me the code var ingredientsForPasta = ['Pasta', 'Water', 'Salt']; // IIFE - Immediately Invoked Function Expression var KitchenApp = (function() { 'use strict'; // Isolated scope with access to Global console.log(ingredientsForPasta); var ingredientsForSauce = ['Tomato', 'Olive Oil', 'Basil']; console.log(ingredientsForSauce); }()); console.log(ingredientsForPasta); // Error: ingredientsForSauce is not defined // No access to variables defined inside de IIFE console.log(ingredientsForSauce); > [ 'Pasta', 'Water', 'Salt' ] > [ 'Tomato', 'Olive Oil', 'Basil' ] > [ 'Pasta', 'Water', 'Salt' ]
 > ಠ_ಠ

Slide 21

Slide 21 text

var KitchenApp = KitchenApp || {}; KitchenApp = (function(){ function init() { KitchenApp.ChefModule.init(); KitchenApp.ChefModule.getNames(); } return { init: init } }()); KitchenApp.ChefModule = (function(){ 'use strict'; var names; function init() { names = ['Dona Benta', 'Palmirinha', 'Ana Maria Braga']; };// public function getNames(){ console.log(sortChefs()); }; // public function setNames(chefsNames) { names = chefsNames; }; // public function sortChefs() { return names.sort(); } // private return { init: init, getNames: getNames, setNames: setNames } }()); KitchenApp.init(); Show me the code

Slide 22

Slide 22 text

Show me the code > [ 'Ana Maria Braga', 'Dona Benta', 'Palmirinha' ] var KitchenApp = KitchenApp || {}; KitchenApp = (function(){ function init() { KitchenApp.ChefModule.init(); KitchenApp.ChefModule.getNames(); } return { init: init } }()); KitchenApp.ChefModule = (function(){ 'use strict'; var names; function init() { names = ['Dona Benta', 'Palmirinha', 'Ana Maria Braga']; };// public function getNames(){ console.log(sortChefs()); }; // public function setNames(chefsNames) { names = chefsNames; }; // public function sortChefs() { return names.sort(); } // private return { init: init, getNames: getNames, setNames: setNames } }()); KitchenApp.init();

Slide 23

Slide 23 text

Almond AMD Implementation Define and Require modules
 
 ~3kb of cool stuff Source: http://www.freeimages.com/photo/almonds-1317800

Slide 24

Slide 24 text

Asynchronous Module Definition

Slide 25

Slide 25 text

Show me the code define('Module.Chef', [], function() { var name = 'Palmirinha', salutation = 'Hello, AMD World.'; function intro() { return 'I am ' + name + '. ' + salutation; } return { intro: intro } }); define('Kitchen', ['Module.Chef'], function(chef) { function init() { console.log(chef.intro()); } return { init: init } }); var kitchenApp = require('Kitchen'); kitchenApp.init();

Slide 26

Slide 26 text

Show me the code > I am Palmirinha. Hello, AMD World. define('Module.Chef', [], function() { var name = 'Palmirinha', salutation = 'Hello, AMD World.'; function intro() { return 'I am ' + name + '. ' + salutation; } return { intro: intro } }); define('Kitchen', ['Module.Chef'], function(chef) { function init() { console.log(chef.intro()); } return { init: init } }); var kitchenApp = require('Kitchen'); kitchenApp.init();

Slide 27

Slide 27 text

Show me the code (function(){ var spaghetti = document.body.dataset.spaghetti; if (spaghetti !== undefined) { require('Recipe.' + spaghetti).cook(); } else { document.write('Y u no select a recipe?'); } }()); kitchenApp.js index.html

Slide 28

Slide 28 text

Show me the code (function(){ var spaghetti = document.body.dataset.spaghetti; if (spaghetti !== undefined) { require('Recipe.' + spaghetti).cook(); } else { document.write('Y u no select a recipe?'); } }()); kitchenApp.js index.html

Slide 29

Slide 29 text

Show me the code bolognese.js define('Recipe.Bolognese', [ 'Ingredients.Water', 'Ingredients.Salt', 'Ingredients.Pasta', 'Ingredients.Tomato', 'Ingredients.GroundBeef', 'Ingredients.OliveOil' ], function(water, salt, pasta, tomato, groundBeef, oliveOil) { var cook = function() { addIngredients([water, salt, pasta, oliveOil]); makeSauce([tomato, salt, oliveOil]); addIngredients([groundBeef]); } var addIngredients = function(ingredients) {} var makeSauce = function(ingredients) {} return { cook: cook }; });

Slide 30

Slide 30 text

Use sua preguiça a favor de eficiência

Slide 31

Slide 31 text

Design Patterns

Slide 32

Slide 32 text

Modularize seu código

Slide 33

Slide 33 text

Obrigado! @ArthurGouveia