Enterprise Strength
 Mobile JavaScript
Unit Testing and Debugging in the Real World
      LA C#.NET - 4 September 2012
       SoCal.NET - 5 September 2012
jssaturday.com
Nov. 10th, Long Beach Convention
             Center
 Discount code: RiaConsultingLLC
The Agenda
• The Rise of JavaScript
• Best Practices
• Unit Testing
• Debugging
The Rise of JavaScript
• Why JavaScript is so important?
• Why JavaScript Sucks?
• Why is Good JavaScript So Hard To Do?
• The Browser Wars I, II, and III
Why JavaScript is so
   important?
2002
2012
Why JavaScript Sucks?
• The DOM
• The Internet’s Long Tail
• Java is to JavaScript...
The DOM
• The Document Object Model is a cross-
  platform and language-independent
  convention for representing and interacting
  with objects in HTML
• The DOM is not JavaScript
The Internet’s Long Tail
• It easy to change websites, hard to change
  browsers
• Standards are hard to change
• Ecma International
 • Move slow
 • Fearful of breaking code in production
Java is to JavaScript as
    Car is to Carpet
• First version built in 10 days by Brendan
  Eich and named LiveScript
• Influenced greatly by Scheme
• Marketing won over Engineering
 • Changed into a curly brace language
 • Renamed to JavaScript for marketing
    appeal
Scheme
;The first program
(begin
  (display "Hello, World!")
  (newline))
Why is Good JavaScript
 So Hard to Write?
It’s Your Fault!
Why, It’s Your Fault
• JavaScript is a language that engineers don’t
  bother to learn
• It is changed not embraced
• For years the page load has protected you
The Browser Wars
Browser War I
• Microsoft vs. Netscape
• Internet Explorer vs. Navigator
• Control of the Internet
• Proprietary Additions
• Netscape no longer exists
Browser War II
• Microsoft vs. Mozilla vs. Google vs. Apple
• IE vs FireFox vs Chrome vs Safari
• Search and Referrals
• Microsoft is losing massive market share
Browser War III
• Microsoft vs. Apple vs. Google
• Mobile IE vs Mobile Safari vs The Browser
  named Browser
• Mobile Search and Referrals
• Implementation of HTML5/CSS3
• Two way tie between Apple and Google
Use Best Practices
Use Best Practices
• Avoid sloppy JavaScript
• Avoid the Global Space
• Encapsulate Code into Objects
• Use Design Patterns
Avoid Sloppy JavaScript
• JavaScript is a Harsh Mistress
• Always use ‘===’ & ‘!==’
• Code in JavaScript not C#, Java, Ruby, etc.
• Use JSLint or JSHint or Google Closure
Avoid the Global Space
• Minimize Use of Global Variables
• Use Namespacing
• Use Anonymous/Immediate Functions
  When Appropriate
Use Design Patterns
• Singleton
• Chaining
• Revealing Module Structure
• + many more
jQuery Chaining

$('#mypanel')
  .find('TABLE .firstCol')
  .removeClass('.firstCol')
  .css('background' : 'red')
  .append('<span>This cell is now red</span>');
C# LINQ Chaining
using System;
using System.Linq;

namespace StringMethodChaining
{
    class Program
    {
        static void Main(string[] args)
        {
            "aardvarks AND antelopes AND hippopotami"
                .Replace("antelopes", "centipedes")
                .Substring("aardvarks".Length)
                .ToLower()
                .Trim()
                .Split(new[] {"and"}, StringSplitOptions.None)
                .ToList()
                .ForEach(item => Console.WriteLine(item.Trim()));
            Console.ReadLine();
        }
    }
}
Unit Testing
• Introduction to Unit Testing
• Headless Unit Testing
• JS Unit Testers
A unit test is a piece of
code that tests a piece of
production code.
Unit Test
• Sets up one or a few more objects in a
  known state
• Exercises them
• Inspects the results
• Must be fast and easy to run
Beware of headless unit
        tests
Headless Unit Testing
• Ruby, Rhino, and JSUnit
• Visual Studio (using plug-ins)
JS Unit Testers
• Jasmine (JSUnit obsolete)
• QUnit
• YUI 3 Test
• jsTestDriver
• + lots more
JSTestDriver &
         Visual Studio

http://slmoloch.blogspot.com/2009/08/how-to-
run-jstestdriver-with-visual_02.html
Some Tips
• Involve your team in Unit Tester choice
• 100% coverage is tough and not practical
• Test should be public within your team, the
  more eyes the better
Debugging
• What’s the Problem?
• Analogues
• WEINRE & Adobe Shadow
• iWebInspector
• Opera Mobile Emulator
What’s the Problem?
• Every device has a different browser and
  none have development tools
• The alert box - freezes the browser
• Console.log - better but not easy to
• Breakpoints
Analogues

In biochemistry, an analog is a substance that
is similar, but not identical, to another.
For us, it is a browser that is similar, but not
identical, to the one we wish to test.
Analogues
• iPhone - Safari
• Android - Chrome
• Windows Phone 7 - IE
• Blackberry - Safari/Chrome
Analogs


Don’t assume that if it works on the analog,
it will work on the device. Always test on a
device.
WEINRE
• Created by Patrick Mueller in JavaScript
• WEb INspector REmote (pronounced like
  winery)
• WebKit Only! (It uses built in hooks)
• Allows for inspection of HTML/CSS
• Remote console.log
• NOT A DEBUGGER
Adobe Shadow
• Chrome Extension + iOS app & Android
  app
• WEINRE with a better UI
• Inspection of HTML/CSS
• Remote console.log
• NOT A DEBUGGER
iWebInspector

• Runs only on the iOS Simulator
• Inspection of HTML/CSS
• Remote console.log
• True Remote Debugging
Opera Mobile Emulator
• Runs using desktop Opera & Opera Mobile
  or Opera Mobile Emulator
• Inspection of HTML/CSS
• Remote console.log
• True Remote Debugger
Summary
• JavaScript is tough, but overly so
• Use the Best Tools
• Be Pragmatic, not Dogmatic
Useful URLs
• http://www.JSLint.com
• http://www.JSHint.com
• https://developers.google.com/closure/
  utilities/
• http://code.google.com/p/js-test-driver/
• http://people.apache.org/~pmuellr/weinre/
  docs/1.x/1.5.0/
Useful URLs
• http://yuilibrary.com/yui/docs/test/
• http://qunitjs.com/
• http://pivotallabs.com/what/mobile/
  overview
• http://siliconforks.com/jscoverage/

Enterprise Strength Mobile JavaScript

  • 1.
    Enterprise Strength MobileJavaScript Unit Testing and Debugging in the Real World LA C#.NET - 4 September 2012 SoCal.NET - 5 September 2012
  • 2.
    jssaturday.com Nov. 10th, LongBeach Convention Center Discount code: RiaConsultingLLC
  • 3.
    The Agenda • TheRise of JavaScript • Best Practices • Unit Testing • Debugging
  • 4.
    The Rise ofJavaScript • Why JavaScript is so important? • Why JavaScript Sucks? • Why is Good JavaScript So Hard To Do? • The Browser Wars I, II, and III
  • 5.
    Why JavaScript isso important?
  • 6.
  • 7.
  • 8.
    Why JavaScript Sucks? •The DOM • The Internet’s Long Tail • Java is to JavaScript...
  • 9.
    The DOM • TheDocument Object Model is a cross- platform and language-independent convention for representing and interacting with objects in HTML • The DOM is not JavaScript
  • 10.
    The Internet’s LongTail • It easy to change websites, hard to change browsers • Standards are hard to change • Ecma International • Move slow • Fearful of breaking code in production
  • 11.
    Java is toJavaScript as Car is to Carpet • First version built in 10 days by Brendan Eich and named LiveScript • Influenced greatly by Scheme • Marketing won over Engineering • Changed into a curly brace language • Renamed to JavaScript for marketing appeal
  • 12.
    Scheme ;The first program (begin (display "Hello, World!") (newline))
  • 13.
    Why is GoodJavaScript So Hard to Write?
  • 14.
  • 15.
    Why, It’s YourFault • JavaScript is a language that engineers don’t bother to learn • It is changed not embraced • For years the page load has protected you
  • 16.
  • 17.
    Browser War I •Microsoft vs. Netscape • Internet Explorer vs. Navigator • Control of the Internet • Proprietary Additions • Netscape no longer exists
  • 18.
    Browser War II •Microsoft vs. Mozilla vs. Google vs. Apple • IE vs FireFox vs Chrome vs Safari • Search and Referrals • Microsoft is losing massive market share
  • 19.
    Browser War III •Microsoft vs. Apple vs. Google • Mobile IE vs Mobile Safari vs The Browser named Browser • Mobile Search and Referrals • Implementation of HTML5/CSS3 • Two way tie between Apple and Google
  • 20.
  • 21.
    Use Best Practices •Avoid sloppy JavaScript • Avoid the Global Space • Encapsulate Code into Objects • Use Design Patterns
  • 22.
    Avoid Sloppy JavaScript •JavaScript is a Harsh Mistress • Always use ‘===’ & ‘!==’ • Code in JavaScript not C#, Java, Ruby, etc. • Use JSLint or JSHint or Google Closure
  • 23.
    Avoid the GlobalSpace • Minimize Use of Global Variables • Use Namespacing • Use Anonymous/Immediate Functions When Appropriate
  • 24.
    Use Design Patterns •Singleton • Chaining • Revealing Module Structure • + many more
  • 25.
    jQuery Chaining $('#mypanel') .find('TABLE .firstCol') .removeClass('.firstCol') .css('background' : 'red') .append('<span>This cell is now red</span>');
  • 26.
    C# LINQ Chaining usingSystem; using System.Linq; namespace StringMethodChaining { class Program { static void Main(string[] args) { "aardvarks AND antelopes AND hippopotami" .Replace("antelopes", "centipedes") .Substring("aardvarks".Length) .ToLower() .Trim() .Split(new[] {"and"}, StringSplitOptions.None) .ToList() .ForEach(item => Console.WriteLine(item.Trim())); Console.ReadLine(); } } }
  • 27.
    Unit Testing • Introductionto Unit Testing • Headless Unit Testing • JS Unit Testers
  • 28.
    A unit testis a piece of code that tests a piece of production code.
  • 29.
    Unit Test • Setsup one or a few more objects in a known state • Exercises them • Inspects the results • Must be fast and easy to run
  • 30.
  • 31.
    Headless Unit Testing •Ruby, Rhino, and JSUnit • Visual Studio (using plug-ins)
  • 32.
    JS Unit Testers •Jasmine (JSUnit obsolete) • QUnit • YUI 3 Test • jsTestDriver • + lots more
  • 33.
    JSTestDriver & Visual Studio http://slmoloch.blogspot.com/2009/08/how-to- run-jstestdriver-with-visual_02.html
  • 34.
    Some Tips • Involveyour team in Unit Tester choice • 100% coverage is tough and not practical • Test should be public within your team, the more eyes the better
  • 35.
    Debugging • What’s theProblem? • Analogues • WEINRE & Adobe Shadow • iWebInspector • Opera Mobile Emulator
  • 36.
    What’s the Problem? •Every device has a different browser and none have development tools • The alert box - freezes the browser • Console.log - better but not easy to • Breakpoints
  • 37.
    Analogues In biochemistry, ananalog is a substance that is similar, but not identical, to another. For us, it is a browser that is similar, but not identical, to the one we wish to test.
  • 38.
    Analogues • iPhone -Safari • Android - Chrome • Windows Phone 7 - IE • Blackberry - Safari/Chrome
  • 39.
    Analogs Don’t assume thatif it works on the analog, it will work on the device. Always test on a device.
  • 40.
    WEINRE • Created byPatrick Mueller in JavaScript • WEb INspector REmote (pronounced like winery) • WebKit Only! (It uses built in hooks) • Allows for inspection of HTML/CSS • Remote console.log • NOT A DEBUGGER
  • 41.
    Adobe Shadow • ChromeExtension + iOS app & Android app • WEINRE with a better UI • Inspection of HTML/CSS • Remote console.log • NOT A DEBUGGER
  • 42.
    iWebInspector • Runs onlyon the iOS Simulator • Inspection of HTML/CSS • Remote console.log • True Remote Debugging
  • 43.
    Opera Mobile Emulator •Runs using desktop Opera & Opera Mobile or Opera Mobile Emulator • Inspection of HTML/CSS • Remote console.log • True Remote Debugger
  • 44.
    Summary • JavaScript istough, but overly so • Use the Best Tools • Be Pragmatic, not Dogmatic
  • 45.
    Useful URLs • http://www.JSLint.com •http://www.JSHint.com • https://developers.google.com/closure/ utilities/ • http://code.google.com/p/js-test-driver/ • http://people.apache.org/~pmuellr/weinre/ docs/1.x/1.5.0/
  • 46.
    Useful URLs • http://yuilibrary.com/yui/docs/test/ •http://qunitjs.com/ • http://pivotallabs.com/what/mobile/ overview • http://siliconforks.com/jscoverage/

Editor's Notes

  • #2 \n
  • #3 \n
  • #4 \n
  • #5 \n
  • #6 \n
  • #7 Here is a state of the web page from 10 years ago, yes only 10 years. It was built mostly on the server with some backing JavaScript.\n
  • #8 Then these web apps happened. While these sites also have heavy server components, they also fully embrace and use JavaScript on the client\n Google - September 1998\n GMail - April 2004\n Google Maps - February 2005\n Facebook - February 2004\n Twitter - July 2006\n LinkedIn - May 2003\n Netflix - October 2008 (streaming)\n\n
  • #9 So if all of the big name companies are making so many billions off JavaScript heavy web apps, why does it suck so badly?\n\n
  • #10 \n
  • #11 \n
  • #12 \n
  • #13 \n
  • #14 \n
  • #15 \n
  • #16 Hence the rise of all of these compiles to JavaScript languages: CoffeeScript, Coco, Caffeine, Dart, Narcissus, and dozens more.\nThe page load aka document.location - flushes the browser and quietly buries your dead bodies with cement shoes into the ocean.\n
  • #17 \n
  • #18 People sometimes \n
  • #19 Still ongoing Google is currently on top, but by the slimmest of margins.\n
  • #20 \n
  • #21 \n
  • #22 \n
  • #23 \n
  • #24 \n
  • #25 \n
  • #26 \n
  • #27 \n
  • #28 \n
  • #29 \n
  • #30 \n
  • #31 \n
  • #32 \n
  • #33 \n
  • #34 \n
  • #35 \n
  • #36 \n
  • #37 The alert box freezes the browser - timers stop ticking\nconsole.log is better but can only see on iPhone (and it can screw up your UI)\nbreakpoints don&amp;#x2019;t normally exist in mobile browsers\n
  • #38 \n
  • #39 Yes - blackberry use webkit too. Microsoft is the lone hold out\nImportant to note that webkit is not an open source browser, \nbut an open source browser engine kit. \nSafari and Chrome are not identical.\n
  • #40 Test, test, test.\nAnalogs are a convenience tool but not a substitute for device testing. \n(turn phone side ways)\n
  • #41 JavaScript is important to note because it explains a lot of its shortcomings.\nAll of these tools are free!\n
  • #42 Very similar to WEINRE in function. I thought it was WEINRE version 2.0 or something at first\n
  • #43 This is a must have if you are supporting the iPhone!\n\n
  • #44 \n
  • #45 \n
  • #46 \n
  • #47 \n