SlideShare a Scribd company logo
1 of 55
JavaScript
    Proven Practices
20 June 2012
Agenda
Cover proven practices about how to write better
JavaScript

Lots of slides (50+) but a lot are code we will talk about
and see what is right & what is wrong

Assumption: You can read JavaScript (or fake it)
JavaScript is a sloppy
language, but inside it
there is an elegant,
better language.
    Douglas Crockford -
Situation 1
code
   1.    var i=0;
   2.    function demo(){
   3.      for (i=0; i<10; i++){
   4.        // do stuff
   5.      }
   6.    }

   7.    demo();
   8.    // what is i?


output
         10
Situation 1 cont.
code
   1.    function demo(){
   2.      i = 50;
   3.    }

   4.    demo();
   5.    // what is i?




output
         50
Always use var
Variables have scope
  Global
  Function
Using var helps keep the scope straight
Always use var - fixes
1.   var i=0;

2.   function demo(){
3.     var i = 0;
4.     for (i=0; i<10; i++){
5.       print("demo");
6.     }
7.   }

8.   demo();
9.   // what is i?
Always use var - fixes
1.   var i =0;

2.   function demo(){
3.     for (var i=0; i<10; i++){
4.       print("demo");
5.     }
6.   }

7.   demo();
8.   // what is i?
Situation 2
code
   1.    function setspan(num, text)
   2.    {
   3.        eval("myspan" + num + ".innerText = '" + text + "'");
   4.    }


data
   1.    myspan1.innerText = 'it ain't what you do, it's the way thacha do it';



output
         //syntax error
Situation 2 cont.
code
   1.    eval("var a = 'awesome';");
   2.    print(a);




output
         'awesome'
Avoid eval
eval is hard to get right and there are many many
situations out of your control you need solve

eval runs code – so you are saying you trust the user
to write code

Modern browsers pre-compile JavaScript, eval doesn’t
benefit from this!
Situation 3
code
   1.   function doSomething(someValue){
   2.       //...
   3.   }
   4.
   5.   setTimeout("doSomething('3 seconds elapsed. Time is up.');", 3000);


code
   1.   var sum = new Function('op1', 'op2', 'return op1 + op2;');
   2.   var result = sum(10, 20); // ==> 30
Avoid eval part 2
eval can appear in sneaky locations in JavaScript
  Timers – SetTimeout & SetInterval
  Function contstructors
Avoid Eval Part 2 Fixed
1.   setTimeout(function(){
2.     doSomething('3 seconds elapsed. Time is up.');
3.     }, 3000);




1.   function sum(op1, op2){
2.     return op1 + op2;
3.   }
Situation 4
code
   1.    var total = variable1 + variable2;
   2.    print(total);


data
   1.    var variable1 = "1";
   2.    var variable2 = "2";


output
   1.    12


I’d never be so stupid to add strings?
   1.    var variable1 = document.elements["inputA"].value;
Situation 4
code
   1.    var total = parseInt(variable1) + parseInt(variable2);
   2.    print(total);

data
   1.    var variable1 = "1";
   2.    var variable2 = "2";

output
   3
data
   1.    var variable1 = "064";   64 base 8 = 52 base 10
   2.    var variable2 = "032";   32 base 8 = 26 base 10
output                            52 + 26 = 78
   78
Unary Operator
JavaScript has one type for numbers

Other primitives: Object, string & boolean, RegExp,
Math, Array, Date, Error, NativeError (mostly)

parseInt & parseFloat require you to set the numeric
base the number is in – else it will try and guess
Unary Operator
(+<variable>) is the right way to get an number

Even though braces are optional - you should always
have them, else it is hard to work out what is addition
and what is unary
Unary Operator Fixed
code
   1.    var total = (+variable1) + (+variable2);
   2.    print(total);

data
   1.    var variable1 = "1";
   2.    var variable2 = "2";

output
   3

data
   1.    var variable1 = "064";
   2.    var variable2 = "032";

output
         96
Unary Operator vs. parseInt
code
   1.    parseInt("567Gb");


output
   567


code
   1.    +"567Gb";


output
   NaN
Situation 5
1.   function validate (idNumber) {
2.     var year = getYear(idNumber);
3.     // omitted
4.   }
5.
6.   function getYear(idNumber) {
7.     // omitted
8.   }
Namespaces
Group things into namespaces, so that they are
properly scoped.

Expose only what you need to expose.

There are at least 5 different ways to do namespaces –
pick one as a team. I’m going to show you my favourite
next – the Self-Executing Anonymous Function Public
& Private.
Namespaces Fixed
1.    (function (idNumbers, undefined) {
2.
3.    idNumbers.validate = function (idNumber) {
4.       var year = getYear(idNumber);
5.       // omitted
6.     }
7.
8.      function getYear(idNumber) {
9.        // omitted
10.     }

11.   }(window.idNumbers = window.idNumbers || {}));
Namespaces Fixed
1.   idNumbers.validate(""); // works

2.   idNumbers.getYear(""); // undefined
Namespaces Fixed

1.   (function (idNumbers, $, undefined) {
2.
3.   // omitted

4.   }(window.idNumbers = window.idNumbers || {}, jQuery));
Separation of concerns
 Taking namespaces further – practise separation of
 concerns
       Put JavaScript into separate files – do not inline in the HTML file.
       Build as much of your JavaScript do it is not tied to HTML or CSS
       Let HTML do what it is good at (organisation of content), CSS do what it
       is good at (presentation style) & JavaScript do what it is good at
bad
  1.   itemX.color = "#fff";
  2.   itemX.font-family = "Consolas";

good
  1.   itemX.style = "codeBlock";
Situation 6
code
   1.   with (document.forms["mainForm"].elements) {
   2.      input1.value = "junk";
   3.      input2.value = "junk";
   4.   }

data
   1.   <html>
   2.    <body>
   3.     <form id="mainForm">
   4.       <input id="input1" type="text">
   5.       <input id="input2" type="text">
   6.     </form>
   7.    </body>
   8.   </html>
Situation 6 cont.
code
   1.   with (document.forms["mainForm"].elements) {
   2.      input1.value = "junk";
   3.      input2.value = "junk";
   4.   }

data
   1.   <html>
   2.    <body>
   3.     <form id="mainForm">
   4.       <input id="input1" type="text">

   5.     </form>
   6.    </body>
   7.   </html>
Situation 6 cont.
code
   1.   var input2;
   2.   with (document.forms["mainForm"].elements) {
   3.      input1.value = "junk";
   4.      input2.value = "junk";
   5.   }

data
   1.   <html>
   2.    <body>
   3.     <form id="mainForm">
   4.       <input id="input1" type="text">
   5.     </form>
   6.    </body>
   7.   </html>
Avoid With
No way to really know who/what will be changed
Avoid with - Fixed

1.   var o = document.forms["mainForm"].elements;
2.   o.input1.value = "junk";
3.   o.input2.value = "junk";
Situation 7
code
   1.   var o = new Object();
   2.     o.name = 'Jeffrey';
   3.     o.lastName = 'Way';
   4.     o.someFunction = function() {
   5.        console.log(this.name);
   6.     }
Prefer object literals
PREFER

Allows for more flexibility, cleaner code (no
constructors) & you don’t need to worry about
constructor parameter ordering.
Prefer object literals - fixed
1.   var o = {
2.      name : 'Jeffrey',
3.      lastName : 'Way',
4.      someFunction : function() {
5.         console.log(this.name);
6.      }
7.   };



1.   var o = {};
Situation 8

code
   1.   var a = new Array();
   2.   a[0] = "Robert";
   3.   a[1] = "MacLean";
Prefer object literals
Applies to arrays just as well
Prefer object literals - fixed
1.   var o = ["Robert", "MacLean"];




      A common error in JavaScript programs is to use an
      object when an array is required or an array when an
      object is required. The rule is simple: when the property
      names are small sequential integers, you should use an
      array. Otherwise, use an object.
                                                Douglas Crockford
Situation 9
code
   1.    var x = "123";
   2.    var y = 123;

   3.    x == y;

output
   1.    true
Situation 9 cont.
1.   '' == '0'            false
2.   0 == ''              true

3.   false == 'false'     false
4.   false == '0'         true

5.   false == undefined   false
6.   false == null        false
7.   null == undefined    true

8.   ' trn' == 0       true
Use === rather than ==
== is equality
=== is identity
Equality checks type, if type differs will convert and
then check values
Identity checks type, if type differs will return false
Use === rather than == - fixed
1.   "123" === 123; // false

2.   '' === '0' // false
3.   0 === '' // false

4.   false === 'false' // false
5.   false === '0' // false

6.   false === undefined // false
7.   false === null // false
8.   null === undefined // false

9.   ' trn' === 0 // false
Situation 10
code
   1.    function x(){
   2.        return
   3.        6
   4.    }

   5.    x();


output
ALWAYS end with semicolon
Semicolon is a statement separator in JavaScript

Putting semicolon’s in is optional – but then JavaScript
will guess where the statements will be separated

Be consistent
ALWAYS end with semicolon - fixed
code
   1.    function x(){
   2.        return 6;
   3.    }

   4.    x();




output
   6
Situation 11
code
   1.    var a = 1,2;
   2.    var b = 1,2,3;
   3.    var c = 99+1,99




output
   2
   3
   99
Avoid the comma operator
Comma operator – not the comma separator in arrays
– can lead to very tricky to understand code.

Unless you are 100% sure about it – don’t use it

Even when you are 100% sure – add comments for the
rest of us
Comma Operator - Fixed
1.   var r = [], n = 0, a = 0, b = 1, next ;

2.   function nextFibonacci() {
3.     next = a + b;
4.     return b = (a = b, next);
5.   }

6.   while(n++ < 10) {
7.     r.push(nextFibonacci());
8.   }

9.   r; //[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Situation 12
1.   foo = "awesome";


1.   var foo = "awesome";
2.   delete foo;


1.   var o = { foo: 123, foo: 456 };


1.   function eval() { }


1.   eval("var a = false;");
2.   print(a);

1.   with
Turn Strict mode ON
All code runs in the strict context which makes your
code more stable, finds more coding problems and
raises more exceptions.
Can define strict in a function
Turn Strict mode ON
What does it do?
  ECMAScript 3 deprecated methods raise exceptions
  Unassigned variables throw exceptions
  Delete throws exceptions
  Duplicate properties throw exceptions
  Hijacking eval throws exceptions
  Eval code is sandboxed
  With is gone
  MORE!!!
Full info: http://dmitrysoshnikov.com/ecmascript/es5-
chapter-2-strict-mode/
Turn Strict mode ON
For this talk it helps with:
   Situation 1 – always use var. Undeclared variables will syntax error
   Situation 4 – parseInt defaults to decimal in strict mode
       Your mileage may vary here: Chrome 19 does it wrong  IE 10 does
       it right 
   Situation 6 – with. Always gives syntax error
Turn Strict mode ON - Fixed
1.   "use strict";

1.   function demo(){
2.     "use strict";
3.     // more
4.   }

1.   foo = "awesome"; // exception
2.   var bar = "awesome";
3.   delete bar; // exception
4.   var o = { foo: 123, foo: 456 }; // exception
5.   function eval() { } // exception
6.   eval("var a = false;");
7.   print(a); // undefined
8.   with //syntax error
Questions?
Next week: Tools
included will be tools that solve all these issues
Sources
http://www.javascripttoolbox.com/bestpractices

http://net.tutsplus.com/tutorials/javascript-ajax/24-javascript-best-practices-
for-beginners/

http://stackoverflow.com/

http://www.crockford.com/

http://www.jslint.com/lint.html#options

http://enterprisejquery.com/2010/10/how-good-c-habits-can-encourage-bad-
javascript-habits-part-1/

More Related Content

What's hot

Practical JavaScript Programming - Session 8/8
Practical JavaScript Programming - Session 8/8Practical JavaScript Programming - Session 8/8
Practical JavaScript Programming - Session 8/8Wilson Su
 
Unit testing with Easymock
Unit testing with EasymockUnit testing with Easymock
Unit testing with EasymockÜrgo Ringo
 
Practical JavaScript Programming - Session 4/8
Practical JavaScript Programming - Session 4/8Practical JavaScript Programming - Session 4/8
Practical JavaScript Programming - Session 4/8Wilson Su
 
clean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionclean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionsaber tabatabaee
 
Ensure code quality with vs2012
Ensure code quality with vs2012Ensure code quality with vs2012
Ensure code quality with vs2012Sandeep Joshi
 
The art of reverse engineering flash exploits
The art of reverse engineering flash exploitsThe art of reverse engineering flash exploits
The art of reverse engineering flash exploitsPriyanka Aash
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScriptJohannes Hoppe
 
Beautiful java script
Beautiful java scriptBeautiful java script
Beautiful java scriptÜrgo Ringo
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScriptJohannes Hoppe
 
201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programing201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programingwahyuseptiansyah
 
Rechecking SharpDevelop: Any New Bugs?
Rechecking SharpDevelop: Any New Bugs?Rechecking SharpDevelop: Any New Bugs?
Rechecking SharpDevelop: Any New Bugs?PVS-Studio
 
Stop Making Excuses and Start Testing Your JavaScript
Stop Making Excuses and Start Testing Your JavaScriptStop Making Excuses and Start Testing Your JavaScript
Stop Making Excuses and Start Testing Your JavaScriptRyan Anklam
 

What's hot (20)

Practical JavaScript Programming - Session 8/8
Practical JavaScript Programming - Session 8/8Practical JavaScript Programming - Session 8/8
Practical JavaScript Programming - Session 8/8
 
Writing clean code
Writing clean codeWriting clean code
Writing clean code
 
Unit testing with Easymock
Unit testing with EasymockUnit testing with Easymock
Unit testing with Easymock
 
Practical JavaScript Programming - Session 4/8
Practical JavaScript Programming - Session 4/8Practical JavaScript Programming - Session 4/8
Practical JavaScript Programming - Session 4/8
 
Java
JavaJava
Java
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Java Script Best Practices
Java Script Best PracticesJava Script Best Practices
Java Script Best Practices
 
clean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionclean code book summary - uncle bob - English version
clean code book summary - uncle bob - English version
 
Ensure code quality with vs2012
Ensure code quality with vs2012Ensure code quality with vs2012
Ensure code quality with vs2012
 
The art of reverse engineering flash exploits
The art of reverse engineering flash exploitsThe art of reverse engineering flash exploits
The art of reverse engineering flash exploits
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript
 
Beautiful java script
Beautiful java scriptBeautiful java script
Beautiful java script
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript
 
GMock framework
GMock frameworkGMock framework
GMock framework
 
Clean code
Clean codeClean code
Clean code
 
201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programing201913046 wahyu septiansyah network programing
201913046 wahyu septiansyah network programing
 
Library system project file
Library system project fileLibrary system project file
Library system project file
 
Exceptional exceptions
Exceptional exceptionsExceptional exceptions
Exceptional exceptions
 
Rechecking SharpDevelop: Any New Bugs?
Rechecking SharpDevelop: Any New Bugs?Rechecking SharpDevelop: Any New Bugs?
Rechecking SharpDevelop: Any New Bugs?
 
Stop Making Excuses and Start Testing Your JavaScript
Stop Making Excuses and Start Testing Your JavaScriptStop Making Excuses and Start Testing Your JavaScript
Stop Making Excuses and Start Testing Your JavaScript
 

Viewers also liked

Viewers also liked (7)

Webfonts in the wild
Webfonts in the wildWebfonts in the wild
Webfonts in the wild
 
11designer
11designer11designer
11designer
 
perini 2005_Second_Century
perini  2005_Second_Centuryperini  2005_Second_Century
perini 2005_Second_Century
 
Give Us Clean Hands
Give Us Clean HandsGive Us Clean Hands
Give Us Clean Hands
 
Mediengerechtes Webdesign
Mediengerechtes WebdesignMediengerechtes Webdesign
Mediengerechtes Webdesign
 
Westminster Wireframes
Westminster WireframesWestminster Wireframes
Westminster Wireframes
 
Julio
JulioJulio
Julio
 

Similar to JavaScript Proven Practises

Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesAnkit Rastogi
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of JavascriptTarek Yehia
 
Workshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScriptWorkshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScriptVisual Engineering
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and youmarkstory
 
To Err Is Human
To Err Is HumanTo Err Is Human
To Err Is HumanAlex Liu
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My HeartBui Kiet
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesDragos Ionita
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenGraham Royce
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the mastersAra Pehlivanian
 

Similar to JavaScript Proven Practises (20)

Ten useful JavaScript tips & best practices
Ten useful JavaScript tips & best practicesTen useful JavaScript tips & best practices
Ten useful JavaScript tips & best practices
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of Javascript
 
Workshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScriptWorkshop 1: Good practices in JavaScript
Workshop 1: Good practices in JavaScript
 
Typescript barcelona
Typescript barcelonaTypescript barcelona
Typescript barcelona
 
JS
JSJS
JS
 
ES2015 workflows
ES2015 workflowsES2015 workflows
ES2015 workflows
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and you
 
To Err Is Human
To Err Is HumanTo Err Is Human
To Err Is Human
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 
Powerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best PracticesPowerful JavaScript Tips and Best Practices
Powerful JavaScript Tips and Best Practices
 
Es6 hackathon
Es6 hackathonEs6 hackathon
Es6 hackathon
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
Javatut1
Javatut1 Javatut1
Javatut1
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the masters
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 

More from Robert MacLean

14 things you need to be a successful software developer (v3)
14 things you need to be a successful software developer (v3)14 things you need to be a successful software developer (v3)
14 things you need to be a successful software developer (v3)Robert MacLean
 
Building a µservice with Kotlin, Micronaut & GCP
Building a µservice with Kotlin, Micronaut & GCPBuilding a µservice with Kotlin, Micronaut & GCP
Building a µservice with Kotlin, Micronaut & GCPRobert MacLean
 
Features of Kotlin I find exciting
Features of Kotlin I find excitingFeatures of Kotlin I find exciting
Features of Kotlin I find excitingRobert MacLean
 
DevConf Survival Guide
DevConf Survival GuideDevConf Survival Guide
DevConf Survival GuideRobert MacLean
 
The state of testing @ Microsoft
The state of testing @ MicrosoftThe state of testing @ Microsoft
The state of testing @ MicrosoftRobert MacLean
 
Visual Studio ❤ JavaScript
Visual Studio ❤ JavaScriptVisual Studio ❤ JavaScript
Visual Studio ❤ JavaScriptRobert MacLean
 
Putting the DOT in .NET - Dev/Ops/Test
Putting the DOT in .NET - Dev/Ops/TestPutting the DOT in .NET - Dev/Ops/Test
Putting the DOT in .NET - Dev/Ops/TestRobert MacLean
 
A Developer Day 2014 - Durban
A Developer Day 2014 - Durban A Developer Day 2014 - Durban
A Developer Day 2014 - Durban Robert MacLean
 
Agile lessons learned in the Microsoft ALM Rangers
Agile lessons learned in the Microsoft ALM RangersAgile lessons learned in the Microsoft ALM Rangers
Agile lessons learned in the Microsoft ALM RangersRobert MacLean
 
Hour of code - Train the trainer
Hour of code - Train the trainerHour of code - Train the trainer
Hour of code - Train the trainerRobert MacLean
 
Building services for apps on a shoestring budget
Building services for apps on a shoestring budgetBuilding services for apps on a shoestring budget
Building services for apps on a shoestring budgetRobert MacLean
 
3 things your app API is doing WRONG
3 things your app API is doing WRONG3 things your app API is doing WRONG
3 things your app API is doing WRONGRobert MacLean
 

More from Robert MacLean (20)

14 things you need to be a successful software developer (v3)
14 things you need to be a successful software developer (v3)14 things you need to be a successful software developer (v3)
14 things you need to be a successful software developer (v3)
 
Git
GitGit
Git
 
OWASP TOP 10
OWASP TOP 10OWASP TOP 10
OWASP TOP 10
 
Building a µservice with Kotlin, Micronaut & GCP
Building a µservice with Kotlin, Micronaut & GCPBuilding a µservice with Kotlin, Micronaut & GCP
Building a µservice with Kotlin, Micronaut & GCP
 
Looking at the Vue
Looking at the VueLooking at the Vue
Looking at the Vue
 
Kotlin 101
Kotlin 101Kotlin 101
Kotlin 101
 
Features of Kotlin I find exciting
Features of Kotlin I find excitingFeatures of Kotlin I find exciting
Features of Kotlin I find exciting
 
JavaScript Gotchas
JavaScript GotchasJavaScript Gotchas
JavaScript Gotchas
 
DevConf Survival Guide
DevConf Survival GuideDevConf Survival Guide
DevConf Survival Guide
 
The state of testing @ Microsoft
The state of testing @ MicrosoftThe state of testing @ Microsoft
The state of testing @ Microsoft
 
Visual Studio ❤ JavaScript
Visual Studio ❤ JavaScriptVisual Studio ❤ JavaScript
Visual Studio ❤ JavaScript
 
What is new in C# 6?
What is new in C# 6?What is new in C# 6?
What is new in C# 6?
 
Putting the DOT in .NET - Dev/Ops/Test
Putting the DOT in .NET - Dev/Ops/TestPutting the DOT in .NET - Dev/Ops/Test
Putting the DOT in .NET - Dev/Ops/Test
 
A Developer Day 2014 - Durban
A Developer Day 2014 - Durban A Developer Day 2014 - Durban
A Developer Day 2014 - Durban
 
Agile lessons learned in the Microsoft ALM Rangers
Agile lessons learned in the Microsoft ALM RangersAgile lessons learned in the Microsoft ALM Rangers
Agile lessons learned in the Microsoft ALM Rangers
 
Hour of code - Train the trainer
Hour of code - Train the trainerHour of code - Train the trainer
Hour of code - Train the trainer
 
Building services for apps on a shoestring budget
Building services for apps on a shoestring budgetBuilding services for apps on a shoestring budget
Building services for apps on a shoestring budget
 
3 things your app API is doing WRONG
3 things your app API is doing WRONG3 things your app API is doing WRONG
3 things your app API is doing WRONG
 
ASP.NET
ASP.NETASP.NET
ASP.NET
 
LightSwitch
LightSwitchLightSwitch
LightSwitch
 

Recently uploaded

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 

Recently uploaded (20)

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 

JavaScript Proven Practises

  • 1. JavaScript Proven Practices 20 June 2012
  • 2. Agenda Cover proven practices about how to write better JavaScript Lots of slides (50+) but a lot are code we will talk about and see what is right & what is wrong Assumption: You can read JavaScript (or fake it)
  • 3. JavaScript is a sloppy language, but inside it there is an elegant, better language. Douglas Crockford -
  • 4. Situation 1 code 1. var i=0; 2. function demo(){ 3. for (i=0; i<10; i++){ 4. // do stuff 5. } 6. } 7. demo(); 8. // what is i? output 10
  • 5. Situation 1 cont. code 1. function demo(){ 2. i = 50; 3. } 4. demo(); 5. // what is i? output 50
  • 6. Always use var Variables have scope Global Function Using var helps keep the scope straight
  • 7. Always use var - fixes 1. var i=0; 2. function demo(){ 3. var i = 0; 4. for (i=0; i<10; i++){ 5. print("demo"); 6. } 7. } 8. demo(); 9. // what is i?
  • 8. Always use var - fixes 1. var i =0; 2. function demo(){ 3. for (var i=0; i<10; i++){ 4. print("demo"); 5. } 6. } 7. demo(); 8. // what is i?
  • 9. Situation 2 code 1. function setspan(num, text) 2. { 3. eval("myspan" + num + ".innerText = '" + text + "'"); 4. } data 1. myspan1.innerText = 'it ain't what you do, it's the way thacha do it'; output //syntax error
  • 10. Situation 2 cont. code 1. eval("var a = 'awesome';"); 2. print(a); output 'awesome'
  • 11. Avoid eval eval is hard to get right and there are many many situations out of your control you need solve eval runs code – so you are saying you trust the user to write code Modern browsers pre-compile JavaScript, eval doesn’t benefit from this!
  • 12. Situation 3 code 1. function doSomething(someValue){ 2. //... 3. } 4. 5. setTimeout("doSomething('3 seconds elapsed. Time is up.');", 3000); code 1. var sum = new Function('op1', 'op2', 'return op1 + op2;'); 2. var result = sum(10, 20); // ==> 30
  • 13. Avoid eval part 2 eval can appear in sneaky locations in JavaScript Timers – SetTimeout & SetInterval Function contstructors
  • 14. Avoid Eval Part 2 Fixed 1. setTimeout(function(){ 2. doSomething('3 seconds elapsed. Time is up.'); 3. }, 3000); 1. function sum(op1, op2){ 2. return op1 + op2; 3. }
  • 15. Situation 4 code 1. var total = variable1 + variable2; 2. print(total); data 1. var variable1 = "1"; 2. var variable2 = "2"; output 1. 12 I’d never be so stupid to add strings? 1. var variable1 = document.elements["inputA"].value;
  • 16. Situation 4 code 1. var total = parseInt(variable1) + parseInt(variable2); 2. print(total); data 1. var variable1 = "1"; 2. var variable2 = "2"; output 3 data 1. var variable1 = "064"; 64 base 8 = 52 base 10 2. var variable2 = "032"; 32 base 8 = 26 base 10 output 52 + 26 = 78 78
  • 17. Unary Operator JavaScript has one type for numbers Other primitives: Object, string & boolean, RegExp, Math, Array, Date, Error, NativeError (mostly) parseInt & parseFloat require you to set the numeric base the number is in – else it will try and guess
  • 18. Unary Operator (+<variable>) is the right way to get an number Even though braces are optional - you should always have them, else it is hard to work out what is addition and what is unary
  • 19. Unary Operator Fixed code 1. var total = (+variable1) + (+variable2); 2. print(total); data 1. var variable1 = "1"; 2. var variable2 = "2"; output 3 data 1. var variable1 = "064"; 2. var variable2 = "032"; output 96
  • 20. Unary Operator vs. parseInt code 1. parseInt("567Gb"); output 567 code 1. +"567Gb"; output NaN
  • 21. Situation 5 1. function validate (idNumber) { 2. var year = getYear(idNumber); 3. // omitted 4. } 5. 6. function getYear(idNumber) { 7. // omitted 8. }
  • 22. Namespaces Group things into namespaces, so that they are properly scoped. Expose only what you need to expose. There are at least 5 different ways to do namespaces – pick one as a team. I’m going to show you my favourite next – the Self-Executing Anonymous Function Public & Private.
  • 23. Namespaces Fixed 1. (function (idNumbers, undefined) { 2. 3. idNumbers.validate = function (idNumber) { 4. var year = getYear(idNumber); 5. // omitted 6. } 7. 8. function getYear(idNumber) { 9. // omitted 10. } 11. }(window.idNumbers = window.idNumbers || {}));
  • 24. Namespaces Fixed 1. idNumbers.validate(""); // works 2. idNumbers.getYear(""); // undefined
  • 25. Namespaces Fixed 1. (function (idNumbers, $, undefined) { 2. 3. // omitted 4. }(window.idNumbers = window.idNumbers || {}, jQuery));
  • 26. Separation of concerns Taking namespaces further – practise separation of concerns Put JavaScript into separate files – do not inline in the HTML file. Build as much of your JavaScript do it is not tied to HTML or CSS Let HTML do what it is good at (organisation of content), CSS do what it is good at (presentation style) & JavaScript do what it is good at bad 1. itemX.color = "#fff"; 2. itemX.font-family = "Consolas"; good 1. itemX.style = "codeBlock";
  • 27. Situation 6 code 1. with (document.forms["mainForm"].elements) { 2. input1.value = "junk"; 3. input2.value = "junk"; 4. } data 1. <html> 2. <body> 3. <form id="mainForm"> 4. <input id="input1" type="text"> 5. <input id="input2" type="text"> 6. </form> 7. </body> 8. </html>
  • 28. Situation 6 cont. code 1. with (document.forms["mainForm"].elements) { 2. input1.value = "junk"; 3. input2.value = "junk"; 4. } data 1. <html> 2. <body> 3. <form id="mainForm"> 4. <input id="input1" type="text"> 5. </form> 6. </body> 7. </html>
  • 29. Situation 6 cont. code 1. var input2; 2. with (document.forms["mainForm"].elements) { 3. input1.value = "junk"; 4. input2.value = "junk"; 5. } data 1. <html> 2. <body> 3. <form id="mainForm"> 4. <input id="input1" type="text"> 5. </form> 6. </body> 7. </html>
  • 30. Avoid With No way to really know who/what will be changed
  • 31. Avoid with - Fixed 1. var o = document.forms["mainForm"].elements; 2. o.input1.value = "junk"; 3. o.input2.value = "junk";
  • 32. Situation 7 code 1. var o = new Object(); 2. o.name = 'Jeffrey'; 3. o.lastName = 'Way'; 4. o.someFunction = function() { 5. console.log(this.name); 6. }
  • 33. Prefer object literals PREFER Allows for more flexibility, cleaner code (no constructors) & you don’t need to worry about constructor parameter ordering.
  • 34. Prefer object literals - fixed 1. var o = { 2. name : 'Jeffrey', 3. lastName : 'Way', 4. someFunction : function() { 5. console.log(this.name); 6. } 7. }; 1. var o = {};
  • 35. Situation 8 code 1. var a = new Array(); 2. a[0] = "Robert"; 3. a[1] = "MacLean";
  • 36. Prefer object literals Applies to arrays just as well
  • 37. Prefer object literals - fixed 1. var o = ["Robert", "MacLean"]; A common error in JavaScript programs is to use an object when an array is required or an array when an object is required. The rule is simple: when the property names are small sequential integers, you should use an array. Otherwise, use an object. Douglas Crockford
  • 38. Situation 9 code 1. var x = "123"; 2. var y = 123; 3. x == y; output 1. true
  • 39. Situation 9 cont. 1. '' == '0' false 2. 0 == '' true 3. false == 'false' false 4. false == '0' true 5. false == undefined false 6. false == null false 7. null == undefined true 8. ' trn' == 0 true
  • 40. Use === rather than == == is equality === is identity Equality checks type, if type differs will convert and then check values Identity checks type, if type differs will return false
  • 41. Use === rather than == - fixed 1. "123" === 123; // false 2. '' === '0' // false 3. 0 === '' // false 4. false === 'false' // false 5. false === '0' // false 6. false === undefined // false 7. false === null // false 8. null === undefined // false 9. ' trn' === 0 // false
  • 42. Situation 10 code 1. function x(){ 2. return 3. 6 4. } 5. x(); output
  • 43. ALWAYS end with semicolon Semicolon is a statement separator in JavaScript Putting semicolon’s in is optional – but then JavaScript will guess where the statements will be separated Be consistent
  • 44. ALWAYS end with semicolon - fixed code 1. function x(){ 2. return 6; 3. } 4. x(); output 6
  • 45. Situation 11 code 1. var a = 1,2; 2. var b = 1,2,3; 3. var c = 99+1,99 output 2 3 99
  • 46. Avoid the comma operator Comma operator – not the comma separator in arrays – can lead to very tricky to understand code. Unless you are 100% sure about it – don’t use it Even when you are 100% sure – add comments for the rest of us
  • 47. Comma Operator - Fixed 1. var r = [], n = 0, a = 0, b = 1, next ; 2. function nextFibonacci() { 3. next = a + b; 4. return b = (a = b, next); 5. } 6. while(n++ < 10) { 7. r.push(nextFibonacci()); 8. } 9. r; //[1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  • 48. Situation 12 1. foo = "awesome"; 1. var foo = "awesome"; 2. delete foo; 1. var o = { foo: 123, foo: 456 }; 1. function eval() { } 1. eval("var a = false;"); 2. print(a); 1. with
  • 49. Turn Strict mode ON All code runs in the strict context which makes your code more stable, finds more coding problems and raises more exceptions. Can define strict in a function
  • 50. Turn Strict mode ON What does it do? ECMAScript 3 deprecated methods raise exceptions Unassigned variables throw exceptions Delete throws exceptions Duplicate properties throw exceptions Hijacking eval throws exceptions Eval code is sandboxed With is gone MORE!!! Full info: http://dmitrysoshnikov.com/ecmascript/es5- chapter-2-strict-mode/
  • 51. Turn Strict mode ON For this talk it helps with: Situation 1 – always use var. Undeclared variables will syntax error Situation 4 – parseInt defaults to decimal in strict mode Your mileage may vary here: Chrome 19 does it wrong  IE 10 does it right  Situation 6 – with. Always gives syntax error
  • 52. Turn Strict mode ON - Fixed 1. "use strict"; 1. function demo(){ 2. "use strict"; 3. // more 4. } 1. foo = "awesome"; // exception 2. var bar = "awesome"; 3. delete bar; // exception 4. var o = { foo: 123, foo: 456 }; // exception 5. function eval() { } // exception 6. eval("var a = false;"); 7. print(a); // undefined 8. with //syntax error
  • 54. Next week: Tools included will be tools that solve all these issues

Editor's Notes

  1. What happens if someone else writes a method with the same name in the global scope?Does the person using my code need to see all that?Stolen “keywords”