ASP.NET Connections – JavaScript Testing

I am honored to be presenting at ASP.NET Connections, March 26-29 in Las Vegas. One of the sessions I’m presenting is on the topic of JavaScript Testing. If you are writing web applications, JavaScript is likely as significant, if not more so, than your native C#/VB code. And yet, it  is often treated differently from a testing perspective. How does one organize JavaScript to make it testable? How do the SOLID principles apply? What tools can I use to test JavaScript? These questions and others will be addressed through practical code examples on how to use testing frameworks like QUnit and how those tools can be integrated into Visual Studio.

To more illustrate what this session will cover, consider the this hypothetical: we need to display address components in a pre-defined section of a page.

The end result might look like this:

The following code drives the address display:

$.getJSON("data/data.json", function (data) {}).success(function (data) {
		  $("#target").empty();
		  var items = [];
		  $.each(data, function (key, val) {
		    items.push('</pre>
<ul>
	<li id="' + key + '">' + val + '</li>
</ul>
<pre>
');
		  });

$('', {
		    html: items.join('')
		  }).appendTo('#target');

		})

		.error(function (error) {
		  $("#target").empty().append("

An error was encountered...

");
		})

		.complete(function (data) {
		  $("#target").removeClass('ajaxLoad');
		});

Fairly straightforward code: Data is acquired via getJSON and displayed. jQuery I used to drive the process. The code works, but there are problems. Recalling what we have learned from SOLID, DRY, etc. – we quickly learn that the code not maintainable, re-useable and it’s marginally testable. About the only thing that can be tested here is the end result. As to the target, were the proper classes assigned and is there an un ordered list with data? In other words, all we can really test are the side-effects of the JavaScript code, not the code itself. Looking at the code, there are several distinct things happening. First, there is the application of the ajaxLoad class. Second, there is the data acquisition process itself. Third, there is the application of the data. There are also operations to handle error conditions and when the ajax process has completed. Geographically, this code is embedded in an html document. Ideally, this code would be re-factored into its own js file. Going back to the distinct operations, each of these operations should be testable, and more specifically, unit testable. Ideally, we would like to verify our code with a test like this:

function testfixture() {
    module("When retrieving address data");
	//Arrange
	var html = $('</pre>
<div id="target"></div>
<pre>')
   //Act
   ApplySomeOperation(html);

    test("Given that the data has \
	  been applied to a pre-defined div",
	   function () {
	   expect(1);

	   var ul = $(html).find("ul");

       //Assert
	   equal(ul.length, 1, "The ul tag is present.");
	});
}

The code, as written, does not support a unit test like this. The code is essentially a big ball of mud. Again, the code “works”, but in our business, working is not enough. With a little re-factoring, we can get where we need to be.

This is what we end up with:

function removeStartMsg(target) {
   $(target).removeClass('ajaxLoad');
}
function displayStartMsg(target) {
   $(target)
      .empty()
      .addClass('ajaxLoad')
      .append("

One moment while your data is \
	    being retrieved...

");
}
function createAddressHtml(data) {
   var items = [];
      $.each(data, function(key, val) {
	     items
		   .push('</pre>
<ul>
	<li id="' + key + '">'
 + val + '</li>
</ul>
<pre>
');
	  });

return $('', {
		html: items.join('')
	   })
}
function displayAddress(data,target) {
   target.empty().html(createAddressHtml(data));
}
function getAddress(id,target) {
	var complete =  function(data) {
	    removeStartMsg(target);
		$(target).removeClass('ajaxLoad');
	};

	var success = function(data) {
		displayAddress(data,target)
	};

	var error = function(error) {
	   $(target).empty().append("
An error was encountered...

");
	}
	displayStartMsg(target);
	getData(id,"data/data.json",success,error,complete);
}
function getData(id,source,successCallBack,errorCallBack,completeCallBack) {
   $.getJSON(source, {id: id},
       function (data) {}).success(function (data) {
	   successCallBack(data)
   })
   .error(function (error) {
       errorCallBack(error)
   })
   .complete(function (data) {
   	   completeCallBack(data)
	});
}

The idea is that each discrete operation has its own function. Thus – we adhere to the single responsibility principle. Also note that no function reaches into the DOM. Instead, the object it is to act on is handed to it. The code in the page reduces to this:

<script type="text/javascript">
   getAddress(0,$("#target"));
</script>

For testing purposes, the json source is a static file on IIS. With little effort however, the source could easily be any http endpoint. With the refactored code, we can write more granular tests.

For example:

function testfixture() {
    module("When retrieving address data");
	//Arrange
	var html = $('<div id="target"></div>')
   //Act

   getAddress(0,html);
   stop(2); // allow async processes to run

    test("Given that the data has \
	  been applied to a pre-defined div",
	   function () {
	   expect(1);

	   var ul = html.find("ul");

       //Assert
	   equal(ul.length, 1, "The ul tag is present.");
	});

    test("Given that the data has \
	  been applied to a pre-defined div",
	   function () {
	   expect(1);
	   var expectedHtml = "<ul><li id=\"company\">Microsoft Corporation</li>
                <li id=\"Address\">One Microsoft Way</li><li id=\"City\">Redmond</li>
                <li id=\"State\">WA</li><li id=\"Zip\">98052</li></ul>"
       //Assert
	   equal(html.html(), expectedHtml, "The html markup is correct.");
	});
}

The following is the QUnit output:

Again, with the refactored code, we can test everything, at a unitary level, that is needed to support the display.

Posted in ASP.Net Connections, Javascript | Leave a comment

My appearance on Dot Net Rocks – SOPA

Recently, I was a guest on Dot Net Rocks. As you can see from the comments, this episode stirred the pot a bit. As I’ve always said, if you are not ticking off at least a few people – you are not trying hard enough! There is an update to this show that will be airing on 2/9. The original focus of the show was on SOPA – discussing what it is and perhaps more importantly, what it is not. During the show, I had added that one of the sources of intellectual property law are treaties that the United States ratifies. Seems like a non-consequential statement at the time. Turns out – it was not. For a few years, ACTA has been discussed  – but the topic has not received too much air play given that SOPA was in the news. Now that SOPA has been removed from consideration, ACTA has now entered the limelight.

I want to be clear here – I don’t agree with those that say that laws like SOPA and treaties like ACTA – subvert free speech. Stealing the intellectual property of others is not something that should be tolerated. That said, there were some troubling aspects in SOPA – mostly arising form the lack of due process. The major problem was that based on a mere allegation – a site could be shut down.  It remained to be seen how that law would have been enforced.

ACTA is another thing altogether in that it is a treaty. In my opinion, organizations like the Free Software Foundation (FSF) – completely mis-represent what ACTA is. If there is one thing about ACTA I think is worthy of criticism is the secrecy around the treaty negotiations.

In general, I’m a fan of the FSF. That said, the torch and pitchfork crowd goes a bit too far with their assertions. Here are the FSF’s points:

  1. It makes it more difficult to distribute free software: Without file sharing and P2P technologies like BitTorrent, distributing large amounts of free software becomes much harder, and more expensive. BitTorrent is a grassroots protocol that allows everyone to contribute to legally distributing free software.
  2. It will make it harder for users of free operating systems to play media: Consumers may no longer be able to buy media without DRM — and DRMed media cannot be played with free software.
  3. It increases the chances of getting your devices taken away: Portable media players that support free formats are less common than devices which support DRM, such as the iPod. Will this make them suspicious to border guards?
  4. It creates a culture of surveillance and suspicion, in which the freedom that is required to produce free software is seen as dangerous and threatening rather than creative, innovative, and exciting.

First off, folks need to read ACTA. It states pretty clearly that notions of due process and free speech must endure – that the manner of how ACTA is enforced must be consistient with those ideals. Put it this way, illegal activity occurs on cell phones and computers. Are we less likely to use those devices? Are those devices confiscated today? Of course not. File sharing technology supports legal activities. Indeed, it can support illegal activities as well. That however, does not mean those technologies will go away. This is where common sense and practicality have to kick in. For example – let’s say you in fact, have illegal copies of music on your iPod. How can you tell by looking at the device? More importantly, how could a customs tell? Do we really think this is going to be a priority? 

The fact is – pirated software, music and counterfeit goods are a problem – and it is well within the province of rights-holders to prosecute and protect their rights. That said, it cannot and should not happen at the expense of our rights and our rights to use technology that has legitimate purposes. I happen to think that the MPAA and RIAA, while they have some legitimate concerns, are also comprised of people that for the most part – are clueless on how to deal with these matters. If there is one point to take from the DNR episodes it is this – GET INVOLVED AND STAY INFORMED. If there is one suggestion I’d make to those who have to craft legislation and ratify treaties – it is this – G

 

 

Posted in ACTA, Dot Net Rocks, IP Law, SOPA, Uncategorized | Leave a comment

Use tasks you have to do around the house to improve your development skills

A somewhat unorthodox and non-traditional suggestion I admit, but as Brad Meltzer often asks: “This may seem unlikely, but go with me on this…”

How many times have you had to build that piece of furniture from Ikea, install window blinds, etc? In each case, you are solving a problem. You have to analyze and organize. It’s really not that different from development. It’s a project like anything else. It has a beginning, middle, end. Like development, if you misinterpret the requirements, you have to go back a few steps. That next thing you were going to do has to get pushed to a later time.

This past weekend, I had to install some Levolor Blinds into our spare bedroom. I also happened to be listening to the  This Developer’s Life Podcast. I was listening to Episode 1.0.6: Abstraction. A great episode (as they all are – and like really good things – you have to go through them a few times because you are likely to pick up something new and useful that you missed before.) The part that really struck me was the interview with Ward Cunningham. It was then that I decided to use my blind installation task as an analog of sorts for development. The interview with Ward was the inspiration. The end game is to then reverse the flow – to perhaps use development as an analog for the blind installation task – assuming of course the blind installation task is successful!

It begins with us…..

The one thing that really struck me about Ward was his motivation – that he builds things form himself. The same was true with John Ressig and Dan Bricklin. Then it struck me that this is really a craftsmanship issue. True craftsmen always reserve the best stuff for themselves. Indeed, much of that good stuff accrues to the benefit the things that are released for public consumption. Craftsmen work product also shares another characteristic – what they produce is pleasing to look at and use by the harshest critic of them all – the craftsmen themselves.

Turning back to my window blind example, I wanted to ensure they would function properly, be pleasing to look at – much like what we strive for in our software.  For a satisfactory end result however, there has to be some measure of planning. Since I had two blinds to install, I wanted to make sure the second one had the benefit of the first – as to process and as to a completed product. My benchmarks were simple:

Did I thoroughly understand the requirements?

Did I have a plan?

Was I organized?

As to tooling, did I have what I needed and were those tools readily available?

Was there improvement in the process?

The great thing about tangible projects – particularly those you have to do around the house or wherever you may be, those projects provide immediate feedback. You know whether they look right. You know immediately whether something was hacked together. You know whether the finished product is pleasing to look at.  You also know what is lurking beneath the surface. I’m in the middle of the Steve Jobs bio by Walter Isaacson. Jobs was obsessive about quality (perhaps to an unhealthy extreme!!). His [Jobs] obsession was centered around the fact that quality of thing not seen was at least as important as what is seen.   That consideration absolutely applies to the software world. When we see properly working software, we can take pride that the things people don’t see are well constructed. With home projects – we know that something is not going to break or fall apart because the things people don’t see (like screws into the wall being well anchored) are well done.

Food for thought as you contemplate ways to continuously improve.

Posted in Continuous Improvement, Software Engineering | Leave a comment

WebAPI Developer Preview 6: Self Hosted Mode Example

In case you missed it, the WebAPI Developer Preview is up to version 6. The CodePlex site provides all of the information you need to get started. To illustrate how to build a simple WebAPI, this example uses ASP.NET MVC as the host. While the example makes reference to the fact that you can create this example in self-hosted mode (not using ASP.NET/ASP.NET MVC) – there is no code that illustrates how to do this.

To get started, create a Console Application and follow the steps in the ASP.NET MVC example to use Nuget to download the WebAPI references and to create the Contact Manager classes. Once you have those items in place, all you need to do is outfit the application to fire up the web host. To do that, use this code:

using System;
using ContactManagerSelfHost.APIs;
using Microsoft.ApplicationServer.Http;

namespace ContactManagerSelfHost
{
    class Program
    {
        static void Main(string[] args)
        {

            using (var host = new HttpServiceHost(typeof(ContactsApi), "http://localhost:9000/api/contacts"))
            {
                host.Open();
                Console.WriteLine("Press any key to stop host...");
                Console.ReadKey();
            }
        }
    }
}

Simply run the app and navigate to the URL. That’s it!

Posted in .NET, WebAPI | Tagged | Leave a comment

Great post on why the patent system in the US is broken

You can read the post at SFGate. It’s a great interview with a Google attorney who lays out in pretty simple terms why the patent system in the US is broken. The comments are also worth a read. Some of them are quite insightful.

In a nutshell, to be patentable, the given subject matter must cross two thresholds:

  1. The subject matter must be novel
  2. It must be non-obvious to those who are skilled in the art.

Ideas and discoveries are not patentable. Inventions are patentable. i.e. things that do something. Great examples are the microprocessor, the internal combustion engine, the  machine that produces Slurpees, etc. The big question is whether software is patentable. It’s settled that software is copyrightable in that it can be expressed in document form. But as we know as professionals in this business, software, like it’s machine and hardware counterparts, actually does something – whether it is controlling hardware, carrying out a business process, etc. A software process can be described in a patent claim just as the processes for how a microprocessor works can be documented (in reality, multiple patents will cover complex things like a microprocessor, combustion engine, etc.)

A good example of a software patent was FoxPro’s Rushmore Technology which, simply stated, was a data access method that used indexes to optimize data retrieval. A bad example of a software patent was 1-Click Buying. Setting aside the obviousness problem here, let’s consider whether something like 1 Click Buying was novel. By novel, does something build on prior art? I think it is safe to say that 1 Click Buying most certainly built on prior art that Apple didn’t own. When you consider how big Apple has become, don’t just think of the iPod, iPhone, IPad, etc. In 2000, Amazon licensed 1-Click from Apple. How much in licensing and royalty fees do you reckon Apple has cashed in on?

I agree with the Google attorney, the system is broken and has been broken for quite some time.  That’s not to say that software cannot and should not be patentable. I contend that software is just as patentable as anything else that has the potential for being patentable subject matter. The notions of novelty and non-obviousness seem to have been discarded by the USPTO. Some claim that it is very difficult to discern whether something is actually building on prior art. Peraps if claims where drawn up in a more standardized way, it would be easier. But you see, this is where the lawyers come in. The goal is always to draw up claims that are broad and vague. The broder the claims, the more your patent covers – something that patent trolls strive for. The gate keeper to protect the system is supposed to be the USPTO. They have been totally asleep at the wheel. It’s also been the court system that has been asleep at the wheel. Often, the USPTO will actually get it right – only to be reversed by the federal courts.

Now…before you think this is something that is just limited to technology, think again. Do you remember those orange trashbags that you could fill with leaves? They had a picture of a pumpkin stamped on them. Was that novel? I don’t believe so as it most definitely built on prior art. Was it non-obvious?  I thnk it is safe to say that in reality, the bags were obvious. The USPTO rejected the patents. The Circuit Court however, in 1999, reversed the USPTO. The Dembiczak and Zinbarg case is a good one to read because it is instructive on the gap that existed between the law and common sense. The USPTO was reversed on technical grounds based on evidence that was not in the record – but nevertheless could have been. This was a case where there the narrow teaching/suggestion/motivation standard for obviousness under 35 U.S.C. §103 was applied. Fortunately, in 2007, in the KSR v. Teleflex decision, the SCOTUS finally brought in notions of common sense. IMO, had the KSR decision been around when 1 Click was applied for, the patent would have been rejected.

Things should get better, but it would appear they are not because of business reality. The fact is, companies with a lot of money have the threat of litigation on their side – and that causes many more other companies to cave in. The Google attorney interviewed commented, as I have in the past that the only way you get to a definitive word is to litigate and get a court decision as to what is novel and non-obvious. The difficulity is that these are mixed questions of law and fact.

Bottom line, it’s going to be a while before things actually get better in the patent landscape.

 

Posted in IP Law, Patents, Uncategorized | Leave a comment