Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Javascript Ilegível

Javascript Ilegível

Uma abordagem sobre padrões de desenvolvimento javascript: estilo, organização, legibilidade de código.

Rafael Specht da Silva

November 08, 2014
Tweet

More Decks by Rafael Specht da Silva

Other Decks in Programming

Transcript

  1. “É melhor não ter comentários e/ou documentação do que tê-los

    de forma ruim/errônea/desatualizada” http://blog.millermedeiros.com/always-comment-weird-things/
  2. var anonymBtn = document.querySelector('#anonym'); var declaredBtn = document.querySelector('#declared'); var myHandler

    = function () { console.log('declared'); var a = 1 + b; } anonymBtn.addEventListener('click', function () { console.log('anonym'); var a = 1 + b; }); declaredBtn.addEventListener('click', myHandler, false);
  3. // Globais Everywhere var User = function () {} var

    Company = function () {} // Tudo certo para dar errado...
  4. // Uma global para a todos governar var MYGLOBAL =

    MYGLOBAL || {}; MYGLOBAL.User = {}; MYGLOBAL.User.Admin = function () {}; MYGLOBAL.Company = function () {};
  5. define([], function () { var Robot = function () {

    this.init = function (battery) { this.battery = battery; } this.charge = function () { this.battery.level++; console.log(this.battery.level) } } return Robot; });
  6. define([], function () { var Battery = function () {

    this.init = function (level) { this.level = level; } } return Battery; });
  7. require(['battery.js', 'robot.js'], function (Source, Robot) { var source = new

    Source(); source.init(80); var robot = new Robot(); robot.init(source); robot.charge(); // 81 robot.charge(); // 82 });
  8. var Battery = function () { this.init = function (level)

    { this.level = level; } } module.exports = Battery;
  9. var Robot = function () { this.init = function (battery)

    { this.battery = battery; } this.charge = function () { this.battery.level++; console.log(this.battery.level); } } module.exports = Robot;
  10. var Battery = require('./battery.js'); var Robot = require('./robot.js'); var source

    = new Battery(); source.init(80); var robot = new Robot(); robot.init(source); robot.charge(); // 81 robot.charge(); // 82
  11. var Battery = function () { this.init = function (level)

    { this.level = level; } } export { Battery }
  12. var Robot = function () { this.init = function (battery)

    { this.battery = battery; } this.charge = function () { this.battery.level++; console.log(this.battery.level); } } export { Robot }
  13. import { Battery } from 'battery.js'; import { Robot }

    from 'robot.js'; var source = new Battery(); source.init(80); var robot = new Robot(); robot.init(source); robot.charge(); robot.charge();
  14. Single Responsability Principle “Uma classe/módulo deve ter somente uma razão

    para mudar” http://freshbrewedcode.com/derekgreer/2011/12/08/solid-javascript-single- responsibility-principle/
  15. var SearchWidget = function () { return { init: function

    (el) { this.el = el; this.input = this.el.find('.search-input'); }, onData: function (data) { this.renderList(data); }, renderList: function (users) { var str = ''; users.forEach(function (user) { str += '<li>' + user.name + '</li>'; }); $('.userList').html(str); },
  16. get: function (query) { $.ajax({ url: 'users.json', data: { query:

    query } }).done(this.onData.bind(this)); }, bindClick: function () { $('.go-button').on('click', function () { var query = this.input.val(); this.get(query); }.bind(this)); } } }
  17. var SearchWidget = function () { return { init: function

    (el) { this.el = el; this.input = this.el.find('.search-input'); }, bindClick: function (callback) { this.el.find('.go-button').on('click', function () { var query = this.input.val(); callback(query); }.bind(this)); } } }
  18. Sempre pense na pessoa que vai dar manutenção no seu

    código! @rafael_sps github.com/rssilva