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

Try a little tenderness

masylum
July 01, 2013
81

Try a little tenderness

masylum

July 01, 2013
Tweet

Transcript

  1. Coding is a fucking mess, big programs are complex and

    troublesome... follow Ottis advice and Try a little tenderness
  2. Mind your API var obj = {}; obj.pi = 3.14;

    obj.circumference = function (r) { return num * this.pi; };
  3. Mind your API var obj = {}; var pi =

    3.14; obj.circumference = function (r) { return r * a; };
  4. Be verbose function avg(xs) { var s = i =

    0; var l = xs.length; for(; i < l; i++;){ s += parseInt(xs[i]); } return s / l; }
  5. Be verbose function average(numbers) { var sum = i =

    0; var length = numbers.length; for(; i < length; i++;){ sum += parseInt(numbers[i]); } return sum / length; }
  6. Explicit over Implicit function Dog() { this.speed = 4; }

    require(‘run’); var dog = new Dog(); dog.run();
  7. Explicit over Implicit function Dog() { this.speed = 4; }

    Dog.prototype.run = require(‘run’); var dog = new Dog(); dog.run();
  8. Careful with DRY function truncateName(el) { var attr = el.type

    === ‘user’ ? ‘fullName’ : ‘name’; return el[attr].substring(0, 40); }
  9. Lines of code syndrome function isUrgent(id) { if (task =

    Task.find(id) && task.urgent) return true; return false; }
  10. Lines of code syndrome function isUrgent(id) { var task =

    Task.find(id); if (task && task.urgent) { return true; } return false; }
  11. Self explanatory code function removeUser(id) { // find user var

    user = User.find(id); ... // remove tasks return user.tasks.remove(); }
  12. Write contracts /** * Removes the user and all his

    * assigned tasks * * @param {Number} id - user id * @return {Boolean} - did work? */ function removeUser(id) ...
  13. Beware of nesting function isComplete(task) { var comment = task.comment;

    if (comment) if (comment.status === 3) return true; return task.isResolved(); }
  14. Beware of nesting function isComplete(task) { var comment = task.comment;

    if (comment) { return comment.isComplete(); } return task.isResolved(); }
  15. Beware of nesting ! function secondMethod() { console.log(‘B’); } function

    firstMethod() { setTimeout(secondMethod, 10); } setTimeout(firstMethod, 20);
  16. Use tools • Have static analyzers to detect bad code

    practices and syntax errors • Use tools that can provide metrics • Integrate your tools with your editor • Integrate your tools with your process
  17. Avoid broken windows • Before pushing: Git hooks • Before

    merging a branch: Travis • Before deploying a release: Jenkins