<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss version="2.0">
  <channel>
    <generator>PHP Feeder 1.2 http://proger.i-forge.net/PHP_Feeder/7sg</generator>
    <docs>http://cyber.law.harvard.edu/rss/rss.html</docs>

    <title>phawk.co.uk</title>
    <description>a software engineer specialising in javascript, ruby, php and ios development</description>
    <language>en</language>
    <image>
      <url>http://phawk.co.uk/logo.png</url>
    </image>
    <managingEditor>pete@phawk.co.uk (Pete Hawkins)</managingEditor>
    <webMaster>pete@phawk.co.uk (Pete Hawkins)</webMaster>
    <rating>SFW</rating>
    <lastBuildDate>Tue, 08 Jan 2013 23:03:07 +0000</lastBuildDate>
    <pubDate>Sat, 26 Jan 2013 00:39:32 +0000</pubDate>
    <ttl>60</ttl>
    <category>JavaScript,</category>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/peteyhawkins" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="peteyhawkins" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
      <title>Automating your tests</title>
      <description>&lt;p&gt;Having a test suite is awesome, doing TDD is awesome too, but what if you make that really quick change and forget to run those tests? Its likely to blow up in your face.&lt;/p&gt;

&lt;h3&gt;Don't fret automating tests is simple&lt;/h3&gt;

&lt;p&gt;I'm going to show you two super easy ways to automate your node.js mocha tests from the &lt;a href="http://phawk.co.uk/articles/testing-node-apps-with-mocha"&gt;last post&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Git pre-commit hook&lt;/h2&gt;

&lt;p&gt;You've got to commit your code before you can deploy it, so a pre-commit hook will ensure your tests are always run, there is no forgetting.&lt;/p&gt;

&lt;p&gt;Setting up a git pre-commit hook is super easy, first create a pre-commit file in your &lt;code&gt;my-project/.git/hooks/&lt;/code&gt; directory and make it executable, then add &lt;code&gt;make test&lt;/code&gt; the command to be run on pre-commit. (Run these commands at the root of your git repository)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;touch .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
echo "make test" &amp;gt; .git/hooks/pre-commit
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once you have this file, git will run the commands inside it and listen to the exit codes of the programs to see if it will proceed with the commit or not. Our &lt;code&gt;make test&lt;/code&gt; from the last post will output an exit code &lt;strong&gt;1&lt;/strong&gt; (which is an error) if the tests fail and a &lt;strong&gt;0&lt;/strong&gt; (which is a pass) if the tests pass.&lt;/p&gt;

&lt;h3&gt;Failing tests&lt;/h3&gt;

&lt;p&gt;&lt;img src="http://phawk.co.uk/img/articles/automating-tests/commit-failing.png" alt="Failing commit"&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If the tests fail git will not even commit the code, it will leave the changes in the staging area.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;Passing tests&lt;/h3&gt;

&lt;p&gt;&lt;img src="http://phawk.co.uk/img/articles/automating-tests/commit-passing.png" alt="Passing commit"&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If the tests pass your commit will go through like normal&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Travis CI&lt;/h2&gt;

&lt;p&gt;You are now covered with the pre-commit hook above, git won't let you check in code that doesn't pass the tests, this next step will also allow you to see contributors or team members code is passing before accepting pull requests.&lt;/p&gt;

&lt;h3&gt;Failing travis pull request&lt;/h3&gt;

&lt;p&gt;&lt;img src="http://phawk.co.uk/img/articles/automating-tests/travis-fail.png" alt="Failing travis-ci pull request on github"&gt;&lt;/p&gt;

&lt;h3&gt;Passing travis pull request&lt;/h3&gt;

&lt;p&gt;&lt;img src="http://phawk.co.uk/img/articles/automating-tests/travis-pass.png" alt="Passing travis-ci pull request on github"&gt;&lt;/p&gt;

&lt;p&gt;Integrating with &lt;a href="https://travis-ci.org/"&gt;travis-ci&lt;/a&gt; is free for open source projects and is a perfect first pass for your pull requests.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a &lt;code&gt;.travis.yml&lt;/code&gt; file in the root of your repository and set its content to the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;language: node_js
node_js: 0.8
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="http://about.travis-ci.org/docs/"&gt;A lot more configuration options&lt;/a&gt; can be set in this file, this is all we need to get our node tests running though since they follow a lot of the node conventions (like running with npm test and installing dependencies with npm install).&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Signin to &lt;a href="https://travis-ci.org/"&gt;travis-ci&lt;/a&gt; with your github account and turn the tests on for your repository.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;(optional) It is nice to show the consumers of your project the build status to give a little peace of mind, you can put a status image into your readme &lt;img src="https://travis-ci.org/phawk/tdd-node-mocha.png?branch=master" alt="Build status"&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Image url:
https://travis-ci.org/[Organisation]/[Repository].png?branch=master
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Wrapping up&lt;/h2&gt;

&lt;p&gt;With git pre-commit hooks and continuous integration systems like travis-ci it is extremely straightforward to ensure you, your team or collaborators never commit or accept failing code.&lt;/p&gt;

&lt;p&gt;It is a little more complex to get travis to run your web browser based tests for mocha in a headless browser, I will leave that for another post.&lt;/p&gt;
</description>
      <link>http://phawk.co.uk/post/automating-your-tests</link>
      <author>pete@phawk.co.uk (Pete Hawkins)</author>
      <guid>http://phawk.co.uk/post/automating-your-tests</guid>
      <pubDate>Tue, 08 Jan 2013 22:58:31 +0000</pubDate>
    </item>

    <item>
      <title>Testing node.js apps with mocha</title>
      <description>&lt;p&gt;A couple of days ago I wrote an article on &lt;a href="http://phawk.co.uk/articles/testing-backbone-with-mocha"&gt;Testing Backbone.js with mocha&lt;/a&gt;, this was a primer on doing TDD in one of the easiest environments possible, all you needed to get started was a web browser. If you have never done TDD before, I'd recommend checking out &lt;a href="http://phawk.co.uk/articles/testing-backbone-with-mocha"&gt;that post&lt;/a&gt; first.&lt;/p&gt;

&lt;p&gt;For those of us who aren't building client side and are working with multi-page websites this won't be as useful, but we can reuse the same language and testing framework, &lt;a href="http://visionmedia.github.com/mocha/"&gt;mocha&lt;/a&gt;, on the server side with &lt;a href="http://nodejs.org/"&gt;node.js&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Setup&lt;/h2&gt;

&lt;p&gt;This isn't quite as simple as downloading a zip file in my last article...&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make sure you have node.js and npm installed. Hit up &lt;a href="http://nodejs.org/"&gt;nodejs.org&lt;/a&gt; and click the big install button.&lt;/li&gt;
&lt;li&gt;Create a new directory for your project&lt;/li&gt;
&lt;li&gt;Make a package.json file and fill it with the following content&lt;/li&gt;
&lt;/ol&gt;

&lt;script src="https://gist.github.com/e61056dfa773e2005f12.js"&gt;&lt;/script&gt;

&lt;ol&gt;
&lt;li&gt;Install your dependencies with &lt;code&gt;npm install&lt;/code&gt;. &lt;em&gt;I haven't used the devDependencies section for these as there are still a few problems with that and circular references when installing chai or expect.js alongside mocha&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Create a Makefile so you can run &lt;code&gt;make test&lt;/code&gt; to run all of the command line arguments passed to mocha in an easy manner, I found this helpful from &lt;a href="http://brianstoner.com/blog/testing-in-nodejs-with-mocha"&gt;Brian Stoners article on testing with node&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;script src="https://gist.github.com/3aa93e359cd731e7f5fc.js"&gt;&lt;/script&gt;

&lt;p&gt;&lt;strong&gt;Warning&lt;/strong&gt; if you get the following error when running &lt;code&gt;make test&lt;/code&gt;: &lt;code&gt;Makefile:13: *** missing separator (did you mean TAB instead of 8 spaces?).  Stop.&lt;/code&gt;, you need to use tabs as the separator in a Makefile, spaces won't work.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;test/&lt;/code&gt; directory in the app root — mocha will automatically look into this folder for tests to run.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;What we are going to do&lt;/h2&gt;

&lt;p&gt;We need to write a Bank Account Object for our application, we're going to test drive the design of it.&lt;/p&gt;

&lt;h2&gt;The most simple test&lt;/h2&gt;

&lt;p&gt;Let's test for the existence of our bank account, that is the simplest test we could write.&lt;/p&gt;

&lt;p&gt;Create a new file named &lt;code&gt;bank-account.test.js&lt;/code&gt; inside your &lt;code&gt;test/&lt;/code&gt; directory:&lt;/p&gt;

&lt;script src="https://gist.github.com/b77d41c6cc69de6b230d.js"&gt;&lt;/script&gt;

&lt;p&gt;You'll notice this test is pending and won't actually assert anything yet, but this is a good time to run &lt;code&gt;make test&lt;/code&gt; and check that mocha is running as it should, if all is working you'll see the following output:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://phawk.co.uk/img/articles/testing-node-with-mocha/output-pending.png" alt="Mocha - Output pending"&gt;&lt;/p&gt;

&lt;p&gt;Hopefully you're all setup and good to go, if you have any problems at this stage go back over the steps, or post in the comments with any problems.&lt;/p&gt;

&lt;h3&gt;Writing the test&lt;/h3&gt;

&lt;p&gt;Now let's fill in that pending test to make it fail and give us some work to do:&lt;/p&gt;

&lt;script src="https://gist.github.com/aa7ba588b24266e2c995.js"&gt;&lt;/script&gt;

&lt;p&gt;Run the tests with &lt;code&gt;make test&lt;/code&gt; and you should get a big scary error: &lt;code&gt;Error: Cannot find module '../lib/bank-account'&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Let's fix this one by making our Account module.&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;lib/&lt;/code&gt; folder in the root directory of our application.&lt;/li&gt;
&lt;li&gt;Create a &lt;code&gt;bank-account.js&lt;/code&gt; file inside the lib folder you just created.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Re-run &lt;code&gt;make test&lt;/code&gt;, all should fail, but it will look a lot prettier and also be red.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://phawk.co.uk/img/articles/testing-node-with-mocha/first-red-test.png" alt="Mocha - First red test"&gt;&lt;/p&gt;

&lt;h3&gt;Making it pass&lt;/h3&gt;

&lt;p&gt;Our test uses an instantiated version of our object, therefore our bank account module will need to return a constructor function that will return an object, let's create this barebones object.&lt;/p&gt;

&lt;script src="https://gist.github.com/550785932ff863dbce8a.js"&gt;&lt;/script&gt;

&lt;p&gt;The output of your &lt;code&gt;make test&lt;/code&gt; should now be &lt;code&gt;1 test complete&lt;/code&gt; and all green.&lt;/p&gt;

&lt;h2&gt;The balanace&lt;/h2&gt;

&lt;p&gt;What good is a bank account without a balance, we want to have private variables (encapsulation) via closures on this bank account object to prevent people tampering with it, so we want a method to fetch the balance, we'll also start the balance off at 0 so we know what to assert.&lt;/p&gt;

&lt;h3&gt;Writing the test&lt;/h3&gt;

&lt;script src="https://gist.github.com/773625a29f147ab49fbf.js"&gt;&lt;/script&gt;

&lt;p&gt;Watch it fail, you should get a TypeError: &lt;code&gt;TypeError: Object [object Object] has no method 'getBalance'&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Making it pass&lt;/h3&gt;

&lt;script src="https://gist.github.com/1106ebfc99d7c2651961.js"&gt;&lt;/script&gt;

&lt;p&gt;All should now be green and you should have two passing tests&lt;/p&gt;

&lt;h2&gt;Logding money&lt;/h2&gt;

&lt;p&gt;What good is a bank account with no money in it, lets make a lodge method on the account to input some money.&lt;/p&gt;

&lt;h3&gt;Red&lt;/h3&gt;

&lt;script src="https://gist.github.com/a20359b04d7ae10a397c.js"&gt;&lt;/script&gt;

&lt;p&gt;We add a new suite just below the second test, suites can be infinitely nested and help to provide context in your test ouput.&lt;/p&gt;

&lt;h3&gt;Green&lt;/h3&gt;

&lt;p&gt;You are on your own here, make this test pass. If you do run into trouble, you can view the complete source code of &lt;a href="https://github.com/phawk/tdd-node-mocha"&gt;my solution on GitHub&lt;/a&gt;, try not to use it though.&lt;/p&gt;

&lt;h2&gt;Multiple bank accounts&lt;/h2&gt;

&lt;p&gt;Is it just me or do you have a sneaky suspicion the variable for balance will be shared across all accounts? Let's write a new test to make sure they work.&lt;/p&gt;

&lt;script src="https://gist.github.com/6092989ba80bd5f61722.js"&gt;&lt;/script&gt;

&lt;p&gt;Run your test and see what happens.&lt;/p&gt;

&lt;p&gt;Oh dear, now our balance isn't coming out as 110 for account 1, it is coming out as 305, why could this be? Well if we add up all of the money we have lodged it adds up to 305, so it would seem as anticipated our &lt;code&gt;balance&lt;/code&gt; variable is actually shared between all of our accounts. This has been a common mistake for me in the past with node and I'm sure might affect other people starting out as well.&lt;/p&gt;

&lt;p&gt;What has gone wrong? Well we have reference to one Account variable, which shares one instance of the closure to &lt;code&gt;var balance&lt;/code&gt;. Let's change our Account around a little and make its creation happen with a function. Update our test suite first.&lt;/p&gt;

&lt;p&gt;Change all instances of &lt;code&gt;new Account()&lt;/code&gt; in our test suite to be &lt;code&gt;Account.create()&lt;/code&gt;. Run the tests again and see all of them fail.&lt;/p&gt;

&lt;h3&gt;Making it pass&lt;/h3&gt;

&lt;p&gt;We need our module to have export create method that will have it's own internal closure state and create our Account object for us and return it.&lt;/p&gt;

&lt;script src="https://gist.github.com/57a21ba20bef4483a23b.js"&gt;&lt;/script&gt;

&lt;h2&gt;Withdrawing money&lt;/h2&gt;

&lt;p&gt;Now we want to be able to take out some of our well deserved cash at an ATM. Let's write a failing test.&lt;/p&gt;

&lt;script src="https://gist.github.com/9ac234952802491bde04.js"&gt;&lt;/script&gt;

&lt;p&gt;You should get an error: &lt;code&gt;TypeError: Object [object Object] has no method 'withdraw'&lt;/code&gt;. Let's add a withdraw method to the &lt;code&gt;prototype&lt;/code&gt; of our Account object that let's you take out money and passes this test.&lt;/p&gt;

&lt;script src="https://gist.github.com/5f70f87728b5dc277bdf.js"&gt;&lt;/script&gt;

&lt;h2&gt;Starting balance of an account&lt;/h2&gt;

&lt;p&gt;One last thing we are going to write a test for: Allow passing of a starting account balance when opening the account with &lt;code&gt;Account.create(150);&lt;/code&gt;.&lt;/p&gt;

&lt;script src="https://gist.github.com/2af1ddb205e48968a42f.js"&gt;&lt;/script&gt;

&lt;p&gt;Passing the test is pretty easy too:&lt;/p&gt;

&lt;script src="https://gist.github.com/cf92247ede8cda55bea8.js"&gt;&lt;/script&gt;

&lt;h2&gt;Homework&lt;/h2&gt;

&lt;p&gt;If you want to continue on with TDD in node, there are a few things you can do to improve your bank account:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Type checking on all inputs into methods.&lt;/li&gt;
&lt;li&gt;Make sure you can't lodge negative amounts of money or 0.&lt;/li&gt;
&lt;li&gt;Make sure you can't withdraw negative amounts of money or 0.&lt;/li&gt;
&lt;li&gt;Support for an overdraft facility.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;Source&lt;/h2&gt;

&lt;p&gt;If you want to view the finished source of my example, it is &lt;a href="https://github.com/phawk/tdd-node-mocha"&gt;available on GitHub&lt;/a&gt;.&lt;/p&gt;
</description>
      <link>http://phawk.co.uk/post/testing-node-apps-with-mocha</link>
      <author>pete@phawk.co.uk (Pete Hawkins)</author>
      <guid>http://phawk.co.uk/post/testing-node-apps-with-mocha</guid>
      <pubDate>Sun, 06 Jan 2013 17:16:52 +0000</pubDate>
    </item>

    <item>
      <title>Testing Backbone.js with mocha</title>
      <description>&lt;p&gt;This is a zero setup quick starter to using TDD, it should take about 20minutes of your time max!&lt;/p&gt;

&lt;p&gt;Client side is one of the best places to get started with TDD as it will teach you the fundamentals without you having to worry about mocking out databases and file i/o. JavaScripts awesome flexibility helps a lot too, no matter the state of your application you will probably still find it relatively easy to get legacy code into a test harness.&lt;/p&gt;

&lt;h2&gt;Setup&lt;/h2&gt;

&lt;p&gt;I'm going to skip past the setup phase here and give you a &lt;a href="http://cl.ly/LwDa"&gt;zip file&lt;/a&gt; thats good to go. For the purpose of this tutorial I want to teach some TDD by example, not module loading with AMD or CommonJS, so we're just going to use namespacing to organise the application.&lt;/p&gt;

&lt;h3&gt;Structure&lt;/h3&gt;

&lt;p&gt;All source files go into the &lt;code&gt;src&lt;/code&gt; directory, you can see an example in &lt;code&gt;src/models/user.js&lt;/code&gt; and it's related test is mirrored in &lt;code&gt;tests/models/user.test.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;src/app.js&lt;/code&gt; file sets up an extremely basic namespace of app.models, app.views and app.collections to organise our source files.&lt;/p&gt;

&lt;p&gt;All source files are wrapped in immediately executing anonymous functions to prevent hammering the global namespace with variables.&lt;/p&gt;

&lt;h2&gt;The first task&lt;/h2&gt;

&lt;p&gt;We want our User model to use the first and last name of the user but we want a convenience method to call to get the full name of a user.&lt;/p&gt;

&lt;h3&gt;Red (the first failing test)&lt;/h3&gt;

&lt;p&gt;In TDD we always start with a failing test, how else could we be confident that our code passes the test without first making sure that it fails.&lt;/p&gt;

&lt;p&gt;Let's write a test in &lt;code&gt;tests/models/user.test.js&lt;/code&gt; (this file has already been setup for you as a reference). Place the test below the pre-existing test inside the suite block.&lt;/p&gt;

&lt;p&gt;I like to start off with the most simple test that I could possibly assert, in this case it's probably to test for the presence of a 'getFullName' method.&lt;/p&gt;

&lt;script src="https://gist.github.com/4452249.js"&gt;&lt;/script&gt;

&lt;p&gt;Open up the &lt;code&gt;tests/test-runner.html&lt;/code&gt; file in your browser, you should see this output:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://phawk.co.uk/img/articles/testing-backbone-with-mocha/failing-test.png" alt="First red test"&gt;&lt;/p&gt;

&lt;p&gt;The expect code is part of the &lt;a href="http://chaijs.com/"&gt;chaijs assertion library&lt;/a&gt;, they have a few different styles of assertions, we'll continue to use the expect style throughout this tutorial.&lt;/p&gt;

&lt;h3&gt;Making the test green&lt;/h3&gt;

&lt;p&gt;The next step is to take this failing test and write the minimum amount of code to make it pass, don't write any extra code, you should have more failing tests for that.&lt;/p&gt;

&lt;p&gt;So to pass this test we simply need to add a 'getFullName' method to the User model, go ahead and open up &lt;code&gt;src/models/user.js&lt;/code&gt; and add in the new method, it should look like this:&lt;/p&gt;

&lt;script src="https://gist.github.com/5ff66a7ffcbae48a1972.js"&gt;&lt;/script&gt;

&lt;p&gt;Refresh your test-runner.html file and watch the test pass. The TDD three step process is &lt;strong&gt;red&lt;/strong&gt;, &lt;strong&gt;green&lt;/strong&gt;, &lt;strong&gt;refactor&lt;/strong&gt;, given the simplicity of what we have just done and the rest of this tutorial I doubt we'll use the refactoring step, but this is a critical step in TDD.&lt;/p&gt;

&lt;h3&gt;Testing this method actually works&lt;/h3&gt;

&lt;p&gt;As you can see in the &lt;code&gt;setup()&lt;/code&gt; method inside &lt;code&gt;tests/models/user.test.js&lt;/code&gt; we have setup a user model with a first_name and last_name property, we want to test that those come out as 'Jimmy Wilson'. Write our next test like so and watch it fail:&lt;/p&gt;

&lt;script src="https://gist.github.com/07562e14ee75fd4ba61c.js"&gt;&lt;/script&gt;

&lt;p&gt;You might think of writing some code here to make sure to trim the full name, but remember what I said above &lt;strong&gt;write the minimum code to pass the test&lt;/strong&gt;, if you think you should add in a trim() function around the returned name, write a test for it and then add the code. For now the simplest way of passing this test legitimately would be to take the first_name and last_name properties and concatenate them with a space in between, so that's what we shall do.&lt;/p&gt;

&lt;script src="https://gist.github.com/dfd1a8f333301fa1a7c7.js"&gt;&lt;/script&gt;

&lt;p&gt;Again, watch it pass and we can move on. You want to move from failing to passing tests as quickly as possible, some experts on the topic would say around 30 seconds to 1 minute maximum, I would be a little bit more liberal than that, but I would feel uncomfortable if I didn't have my test passing within 5minutes of watching it fail.&lt;/p&gt;

&lt;p&gt;Overall in Backbone, models are quite straightforward to test until you come to mocking out AJAX requests, I'll write another post on that soon.&lt;/p&gt;

&lt;h2&gt;Testing Backbone views&lt;/h2&gt;

&lt;p&gt;We need a profile view that will display the users details.&lt;/p&gt;

&lt;p&gt;Your test suite should run very fast and we should always mock out things like databases, file system calls or even the DOM. For Backbone this is quite esay, we are still going to use the jQuery elements, but detatched ones in memory, this keeps things very fast.&lt;/p&gt;

&lt;h3&gt;The first failing test&lt;/h3&gt;

&lt;p&gt;Create the most simple test possible, that the user profile view exists. We also need to make a new test file &lt;code&gt;tests/views/profile.test.js&lt;/code&gt; and add two script tags to our test-runner.html file:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add &lt;code&gt;&amp;lt;script src="../src/views/profile.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt; to the source files section&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;&amp;lt;script src="views/profile.test.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt; to the tests section&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And then write our test:&lt;/p&gt;

&lt;script src="https://gist.github.com/ca5b83ff8cb5dca478ee.js"&gt;&lt;/script&gt;

&lt;p&gt;Pay attention to the comments in the above test file as they explain a little more about writing testable code. Watch the test fail, you should see an error like this:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://phawk.co.uk/img/articles/testing-backbone-with-mocha/failing-test2.png" alt="profile view fail"&gt;&lt;/p&gt;

&lt;p&gt;We are getting this error because we haven't defined our view yet, create the file &lt;code&gt;src/views/profile.js&lt;/code&gt; and add the following Backbone view code:&lt;/p&gt;

&lt;script src="https://gist.github.com/e692dcf2fb6ce1eba418.js"&gt;&lt;/script&gt;

&lt;p&gt;Now run your test again and it will pass.&lt;/p&gt;

&lt;h3&gt;Test View rendering&lt;/h3&gt;

&lt;p&gt;Our view template will look like this:&lt;/p&gt;

&lt;script src="https://gist.github.com/407b305a45c04149fc82.js"&gt;&lt;/script&gt;

&lt;p&gt;Before we write out failing view tests, a word of warning. &lt;strong&gt;Coupling your tests directly to implementation makes them brittle&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Brittle tests don't provide much in the area of refactoring and aren't that valuable. When testing the view I simple want to test that it took the data from the model and rendered, so I am going to take the HTML that the view generated and assert that it has a couple of the users details in it, not that it has an &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; or a &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;script src="https://gist.github.com/3fca134715e212c8e990.js"&gt;&lt;/script&gt;

&lt;p&gt;Now watch the test fail and write the code to pass it.&lt;/p&gt;

&lt;script src="https://gist.github.com/f52b42b2a0f587c6bef8.js"&gt;&lt;/script&gt;

&lt;p&gt;As you can see my "template" is pretty ugly and should be in an external file, see the Homework section below.&lt;/p&gt;

&lt;h2&gt;Homework&lt;/h2&gt;

&lt;p&gt;Safe refactoring is one of the top benefits of TDD. Take the aweful template in the view and change it around to be a client side template of your choice, if you are not sure which I'd recommend looking at &lt;a href="http://handlebarsjs.com/"&gt;Handlebars&lt;/a&gt;. Use your tests to make sure it all still works.&lt;/p&gt;

&lt;h2&gt;Other resources&lt;/h2&gt;

&lt;p&gt;I'll try to write more JavaScript TDD posts soon, in the meantime checkout some of these great articles and resources that helped me learn TDD.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://tinnedfruit.com/2011/03/03/testing-backbone-apps-with-jasmine-sinon.html"&gt;Testing Backbone with Jasmine and sinon&lt;/a&gt; - Jim Newbury who I got to meet and chat with at one of our &lt;a href="http://typecast.com"&gt;typecast&lt;/a&gt; parties this year has an excellent 3 part series on testing backbone.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.involver.com/2012/01/26/testing-backbone-js-best-practices-2"&gt;Testing backbone best practises&lt;/a&gt; - Oracle involver&lt;/li&gt;
&lt;li&gt;&lt;a href="http://addyosmani.com/blog/unit-testing-backbone-js-apps-with-qunit-and-sinonjs/"&gt;Testing backbone with Qunit and sinon&lt;/a&gt; - Addy osmani&lt;/li&gt;
&lt;li&gt;&lt;a href="http://liamkaufman.com/blog/2012/01/28/testing-socketio-with-mocha-should-and-socketio-client/"&gt;Testing socket.io with mocha&lt;/a&gt; - This will give you an introduction to using mocha to test node.js code.&lt;/li&gt;
&lt;/ul&gt;
</description>
      <link>http://phawk.co.uk/post/testing-backbone-with-mocha</link>
      <author>pete@phawk.co.uk (Pete Hawkins)</author>
      <guid>http://phawk.co.uk/post/testing-backbone-with-mocha</guid>
      <pubDate>Fri, 04 Jan 2013 14:03:18 +0000</pubDate>
    </item>

    <item>
      <title>Architecture process</title>
      <description>&lt;p&gt;I've long wanted to jot down the elements to my architecture process so that I could measure and improve upon it. I think once you have something nailed down as a specific process you can really see where the weaknesses are and make big improvements to your workflow.&lt;/p&gt;

&lt;p&gt;Architecture for me has long been a black art of kinds that different problems need different styles of process to solve them, I doubt this is true, and it's probably just that I've never solidified my process, I'm going to try that with an example problem and note down what I am doing.&lt;/p&gt;

&lt;h2&gt;The problem&lt;/h2&gt;

&lt;p&gt;We have some friends who are amazing at JavaScript, but don't know the first thing about HTML or CSS and they want to build an app that lives in the browser. They want to draw and position rectangles and colour them in. We need a JavaScript object that will hide the complexities of the DOM but allow us to do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a rectangle&lt;/li&gt;
&lt;li&gt;Set its width and height in pixels&lt;/li&gt;
&lt;li&gt;Position its x and y coordinates in pixels&lt;/li&gt;
&lt;li&gt;Choose it's colour&lt;/li&gt;
&lt;li&gt;Show / Hide the rectangle&lt;/li&gt;
&lt;li&gt;Oh and of course, it only needs to work in google chrome (no cross browser testing for me).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;View my &lt;a href="https://github.com/phawk/js-rect-example/blob/master/src/rect.js"&gt;solution on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Can it be smaller?&lt;/h2&gt;

&lt;p&gt;This should be on your mind before you build anything, can it be broken into smaller tasks? If so, stop right where you are, break it down into smaller tasks and architect each of those instead.&lt;/p&gt;

&lt;p&gt;I'd also as part of this inital descision ask, &lt;strong&gt;Can it be abstracted and more reusable?&lt;/strong&gt; In this instance I would say so, howver to keep this short we aren't going to dive into creating a &lt;strong&gt;Shape&lt;/strong&gt; object that &lt;strong&gt;Rect&lt;/strong&gt; can inherit from, I might do that some other time.&lt;/p&gt;

&lt;h2&gt;1. Design the public API&lt;/h2&gt;

&lt;p&gt;One of the first things I like to do when architecting code that is to be used by others (nearly all the code I write is, and so is yours!) is to jot down the public API I would like to use. This gives no thought to the internal architecture, but building from outside in will help you define a suitable API. This is how I would like to use the Rectangle:&lt;/p&gt;

&lt;script src="https://gist.github.com/4444844.js"&gt;&lt;/script&gt;

&lt;p&gt;I'd be reasonably happy to use an API like this. Once I am happy with the API design I'll move on.&lt;/p&gt;

&lt;h2&gt;2. What are the responsibilities of this object?&lt;/h2&gt;

&lt;p&gt;This is where you define how the internals of the object operate, what does it need to do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Contains an HTMLElement object&lt;/li&gt;
&lt;li&gt;Sets CSS styling properties on the HTMLElement&lt;/li&gt;
&lt;li&gt;Inserts the HTMLElement to the DOM&lt;/li&gt;
&lt;li&gt;Detatches the HTMLElement from the DOM&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;3. Dependency diagram&lt;/h2&gt;

&lt;p&gt;I've found drawing diagrams is a great way to help you focus and think when architecting. I always find it too easy to write a couple of paragraphs about something without actually doing much thinking. Once you have to draw a diagram you will realise you have a lot more work to do.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://cl.ly/Lv2S/Rect%20object%20deps.png" alt="Rect dependency"&gt;&lt;/p&gt;

&lt;p&gt;So this simply illustrates the depency upon the browsers document API, &lt;strong&gt;document.createElement&lt;/strong&gt; etc. And the realisation that the object needs to store an internal HTMLElement. This one seems quite simple, as part of a larger application it is always good to show where your object sits within the system, these diagrams help me with that decision.&lt;/p&gt;

&lt;h2&gt;4. Flow diagrams&lt;/h2&gt;

&lt;p&gt;In the interest of brevity I am going to illustrate just one flow here, creation of a new Rect object.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://cl.ly/Lvh0/Creation%20of%20Rect%20flow.png" alt="Rect creation flow"&gt;&lt;/p&gt;

&lt;p&gt;This may seem simple for this scenario, but it can really help simplify your solutions for more complex problems, thinking about these things up front is key.&lt;/p&gt;

&lt;h2&gt;5. Write your first failing test&lt;/h2&gt;

&lt;p&gt;Get stuck in, if your task is as small as this one it's time to write a failing test and start to test-drive your solution. If you don't do TDD yet, it's time you tried it! Have a look at &lt;a href="https://github.com/phawk/js-rect-example/blob/master/tests/rect.test.js"&gt;my tests for Rect&lt;/a&gt; and see if it makes any sense.&lt;/p&gt;

&lt;p&gt;I have all of the source and tests for this &lt;a href="https://github.com/phawk/js-rect-example"&gt;solution on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Solution&lt;/h2&gt;

&lt;script src="https://gist.github.com/4445781.js"&gt;&lt;/script&gt;

&lt;p&gt;I'd love to hear your thoughts and suggestions in the comments.&lt;/p&gt;
</description>
      <link>http://phawk.co.uk/post/architecture-process</link>
      <author>pete@phawk.co.uk (Pete Hawkins)</author>
      <guid>http://phawk.co.uk/post/architecture-process</guid>
      <pubDate>Thu, 03 Jan 2013 18:24:48 +0000</pubDate>
    </item>

    <item>
      <title>Module loading</title>
      <description>&lt;p&gt;So as I mentioned in my goals for 2013 I am working on a JavaScript framework, it has been really interesting so far and I am learning quite a lot from it.&lt;/p&gt;

&lt;p&gt;One of the things I wanted was to allow the library to be loaded in any of the 3 major environments, AMD, CommonJS or directly in the browser as a global. I've done a bit of tinkering and come up with my first iteration of loading.&lt;/p&gt;

&lt;script src="https://gist.github.com/4435601.js"&gt;&lt;/script&gt;

&lt;p&gt;I hate having the dependencies hard-coded for AMD and CommonJS and will hopefully find a much nicer way of doing that, maybe it's a sign though that I shouldn't be building it as a strict dependency on the framework itself. I'd love to allow people to easily use zepto as an alternative, or even something without the jQuery syntax altogether. Maybe a more modular approach is needed as an adapter interface.&lt;/p&gt;

&lt;p&gt;Although this post isn't about dependencies per-say, I'd love to hear thoughts on them in the comments. Also if you have a neater way of doing the loading do let me know!&lt;/p&gt;
</description>
      <link>http://phawk.co.uk/post/module-loading</link>
      <author>pete@phawk.co.uk (Pete Hawkins)</author>
      <guid>http://phawk.co.uk/post/module-loading</guid>
      <pubDate>Wed, 02 Jan 2013 16:16:45 +0000</pubDate>
    </item>

    <item>
      <title>Going forward, plans for 2013</title>
      <description>&lt;p&gt;So I have &lt;a href="http://phawk.co.uk/articles/wrapping-up-2012"&gt;reviewed my progress for 2012&lt;/a&gt;, the first thing I have failed at is setting valuable goals. The biggest thing I took away from 2011 was &lt;strong&gt;focus&lt;/strong&gt;, so it made made sense to me to set goals to focus on, however the taking away from 2012 would have to be &lt;strong&gt;value&lt;/strong&gt;. Value is a difficult thing to set your head around at first, but I have found reading up on &lt;a href="http://en.wikipedia.org/wiki/Lean_production"&gt;lean production&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Lean_software_development"&gt;lean software development&lt;/a&gt; to help a lot.&lt;/p&gt;

&lt;h3&gt;In 2013&amp;hellip;&lt;/h3&gt;

&lt;p&gt;I want to get my JavaScript framework (you will hear more about this in the coming months) to MVP and get it released publicly.&lt;/p&gt;

&lt;h2&gt;Breaking this down&lt;/h2&gt;

&lt;p&gt;As any software engineer knows taking a big task and breaking it down is key to accomplishing easily digestible jobs.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Finish off immediate implementation&lt;/li&gt;
&lt;li&gt;Finish off a few applications with the framework myself&lt;/li&gt;
&lt;li&gt;Tweak and adjust implementation to suit my needs&lt;/li&gt;
&lt;li&gt;Refine documentation&lt;/li&gt;
&lt;li&gt;Create a public website&lt;/li&gt;
&lt;li&gt;Get v0.1 released publicly&lt;/li&gt;
&lt;li&gt;Build example applications&lt;/li&gt;
&lt;li&gt;Promote the framework&lt;/li&gt;
&lt;li&gt;Increase uptake while taking on user feedback&lt;/li&gt;
&lt;li&gt;Build and sell traction and value&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This seems like a lot of work, and I hope there is a lot of room for learning in this process, I also think I'll be much better equipped this time next year to review this goal and write a much better wrap up of what I have learned in 2013.&lt;/p&gt;
</description>
      <link>http://phawk.co.uk/post/going-forward-plans-for-2013</link>
      <author>pete@phawk.co.uk (Pete Hawkins)</author>
      <guid>http://phawk.co.uk/post/going-forward-plans-for-2013</guid>
      <pubDate>Mon, 31 Dec 2012 12:53:37 +0000</pubDate>
    </item>

    <item>
      <title>Wrapping up 2012</title>
      <description>&lt;p&gt;Going back to &lt;a href="http://phawk.co.uk/articles/2012-is-full-of-goals"&gt;my post at the beginning of 2012&lt;/a&gt; when I set out goals for the first time, it has now reached the end of 2012 and it's time to review my progress.&lt;/p&gt;

&lt;h2&gt;Contribute to open source projects&lt;/h2&gt;

&lt;p&gt;This hasn't gone anywhere nearly as well as I would have liked. I have maybe had about 10 pull requests accepted over the year, all of which have been reasonably small (less than 5-10 lines changed). It is a start though and I have been working on my own JavaScript framework for a while now, but that may be a few more months before it gets released publicly as I am building applications with it to funnel the direction a few tricky areas are going to go in.
Overall I am going to try more often to open source modular things I have built and try to contribute bug fixes and improvements to the libraries I use.&lt;/p&gt;

&lt;h2&gt;Write at least 2 quality blog posts per month&lt;/h2&gt;

&lt;p&gt;This has been a massive failure, it is probably a crazy goal, quality blog posts are going to take a lot more time for a writer as bad as myself, trying to do two a month in a short amount of time is never going to work.&lt;/p&gt;

&lt;p&gt;I think going forward I will write more often, at least every couple of days and then I hope the quality will improve over time with practise.&lt;/p&gt;

&lt;h2&gt;Building apps&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Launch and validate 2 apps&lt;/li&gt;
&lt;li&gt;Build a native iPhone or Mac app and have it in the app store&lt;/li&gt;
&lt;li&gt;Build either a native android or windows phone app and have it in the marketplace&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These three goals are kind of stupid. Don't even think about launching two apps without first having a great business or product ideas, or at least something tangibly useful.&lt;/p&gt;

&lt;p&gt;In the course of 2012 I have worked on several apps though, but this didn't come from me trying to meet this goal, it came naturually like it should have all along.&lt;/p&gt;

&lt;p&gt;The main attraction of the year was &lt;a href="http://hedgeho.gg/"&gt;Hedgeho.gg&lt;/a&gt;, an content platform to make it easier to create and discover professional content, this took the majority of my app building time and has taught me a lot.&lt;/p&gt;

&lt;p&gt;In terms of building for the various platforms I have gathered experience with the Mac and iOS ecosystems, and have found out that the mac apps, particularly UI interfaces are about 10 years behind the iOS experience. I still haven't touched android and barely scratched the surface of windows phone, but will concentrate on business more in the future, rather than on platforms.&lt;/p&gt;

&lt;p&gt;I've another post coming shortly outlining what I'll be focusing on in 2013.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The end.&lt;/strong&gt;&lt;/p&gt;
</description>
      <link>http://phawk.co.uk/post/wrapping-up-2012</link>
      <author>pete@phawk.co.uk (Pete Hawkins)</author>
      <guid>http://phawk.co.uk/post/wrapping-up-2012</guid>
      <pubDate>Mon, 31 Dec 2012 12:31:56 +0000</pubDate>
    </item>

    <item>
      <title>Backbone.js conventions (part 1)</title>
      <description>&lt;p&gt;&lt;a href="http://backbonejs.org/"&gt;Backbone.js&lt;/a&gt; is a very popular client side framework, it is very flexible and thus has many different ways of doing things. I've found so many different examples on the web of how to do things in backbone, this makes it very difficult for beginners to know which is the right convention to follow.&lt;/p&gt;

&lt;p&gt;I want to share some of the common conventions I've picked up along the way and why I use them.&lt;/p&gt;

&lt;h2&gt;1. Event aggregator&lt;/h2&gt;

&lt;p&gt;Creating a global event system is a great way to handle decoupled messaging within your applications. You can pass this single event instance to all of your views / components and it allows a platform for evented communication.&lt;/p&gt;

&lt;script src="https://gist.github.com/4246698.js?file=events.js"&gt;&lt;/script&gt;

&lt;h2&gt;2. Dependency inversion of el&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;this.el&lt;/code&gt; element within the Backbone views can easily be passed in using a common technique called Inversion of Control. This allows us to do flexible things when testing our views:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Testing views we can pass in an in-memory DOM node $('&amp;lt;div&amp;gt;')&lt;/li&gt;
&lt;li&gt;We can use in memory nodes on list views to build up list items and then only append once to the actual DOM.&lt;/li&gt;
&lt;/ul&gt;

&lt;script src="https://gist.github.com/4246722.js?file=backbone-el-ioc.js"&gt;&lt;/script&gt;

&lt;h2&gt;3. Unbinding events&lt;/h2&gt;

&lt;p&gt;This one is major, especially if you are coming from a server side garbage collected language where it's trickier to get memory leaks. With Backbone you have to cleanup all of your own events when you remove a view.&lt;/p&gt;

&lt;p&gt;I put a &lt;strong&gt;destroy&lt;/strong&gt; method into views for this purpose, I ensure all of my views define this method and cleanup any custom bound events.&lt;/p&gt;

&lt;script src="https://gist.github.com/4246777.js?file=backbone-destroy-view.js"&gt;&lt;/script&gt;

&lt;p&gt;Looking through the Backbone source code you will see views have a &lt;strong&gt;dispose&lt;/strong&gt; method that clears up any events bound by the view onto a model or collection held within it, it also automatically undelegates all events bound in the events hash. This happens when you call &lt;code&gt;.remove&lt;/code&gt; on a Backbone view.&lt;/p&gt;

&lt;h2&gt;Wrapping up&lt;/h2&gt;

&lt;p&gt;I hope to present another one of these informative posts soon, if there is anything in particular you would like to know how to handle in Backbone, please drop a comment below. Again these are just things I have come across and find work well when building maintainable Backbone apps, if you have any better takes on the above, drop a comment, I'd love to hear about them.&lt;/p&gt;
</description>
      <link>http://phawk.co.uk/post/backbone-conventions-part-1</link>
      <author>pete@phawk.co.uk (Pete Hawkins)</author>
      <guid>http://phawk.co.uk/post/backbone-conventions-part-1</guid>
      <pubDate>Sun, 09 Dec 2012 20:14:18 +0000</pubDate>
    </item>

    <item>
      <title>Don't ever fix a bug</title>
      <description>&lt;p&gt;We'll start by a basic &lt;a href="http://en.wikipedia.org/wiki/Software_bug"&gt;definition of a bug&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A software bug is an error, flaw, mistake, failure, or fault in a computer program or system that produces an incorrect or unexpected result, or causes it to behave in unintended ways.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;What generally causes a bug&lt;/h3&gt;

&lt;p&gt;In my opinion bugs are caused by complexity, complicated code that hasn't been designed effectively to make it as simple as possible.&lt;/p&gt;

&lt;p&gt;There are code smells that you can tell simply by looking at code to know that it is complex and will potentially be bug prone.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Long methods (methods with 20+ lines of code)&lt;/li&gt;
&lt;li&gt;Long classes with more than a couple of hundred lines of code&lt;/li&gt;
&lt;li&gt;Many nested control structures&lt;/li&gt;
&lt;li&gt;In JavaScript particularly code that has many nested callbacks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don't take it for granted that these things ensure you will have code ridelled with bugs, but in my experience, the likely hood is high.&lt;/p&gt;

&lt;h2&gt;What does a bug fix look like&lt;/h2&gt;

&lt;p&gt;In most cases especially dealign with the type of code mentioned above a bug fix is generally adding more control structures or input checks, this is not necessarily a bad thing if these checks where missing in the first place.&lt;/p&gt;

&lt;p&gt;However, if we already had the appropriate checks for input coming from users or third parties and the bug arrised from within our own application a bug fix often leads to more complexity and longer methods.&lt;/p&gt;

&lt;p&gt;Under the pressures of delivering we often succome to this kind of fix, a temporary hack around the real problem. Now that we know what the real problem is (ineffective design) we can approach it in a different way.&lt;/p&gt;

&lt;h2&gt;What is the alternative&lt;/h2&gt;

&lt;p&gt;The alternative that I personally propose is refactoring. Patching up bugs will only get you so far and the likelyhood it will lead to more issues is high. &lt;em&gt;Don't ever fix a bug, refactor it out&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Simplicity is the goal, taking complicated problems and coming up with simple, well composed solutions, taking code as mentioned above with long methods and lots of controls structures and simplifying it, the goal:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Short methods that do one thing and one thing only&lt;/li&gt;
&lt;li&gt;Well composed classes that deal with one concern&lt;/li&gt;
&lt;li&gt;Simple control structures&lt;/li&gt;
&lt;li&gt;And in JavaScript, not having many nested callbacks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I personally practise TDD and find that it helps to drive the design of the code you write to have well composed short methods, especially because you think a lot more about the design of the code up front and thinking about writing a test focusses you on solving one problem at a time.&lt;/p&gt;

&lt;p&gt;I know TDD isn't for everyone, but I'd really encourage you to try it if you haven't and give it a couple of weeks to enlighten you, regardless, I hope you can see the benefits in refactoring bugs away.&lt;/p&gt;
</description>
      <link>http://phawk.co.uk/post/dont-ever-fix-a-bug</link>
      <author>pete@phawk.co.uk (Pete Hawkins)</author>
      <guid>http://phawk.co.uk/post/dont-ever-fix-a-bug</guid>
      <pubDate>Sat, 03 Nov 2012 10:50:00 +0000</pubDate>
    </item>

    <item>
      <title>Making things</title>
      <description>&lt;p&gt;If I were to put a label on myself within the category of engineer, that would be "technologist". I love researching and playing with the latest technologies, one of the downsides to this is that I don't get much live. I put a lot of time into learning and staying up to date, but if I were to list the products I have made lately, they could be counted on one hand.&lt;/p&gt;

&lt;p&gt;I recently found myself looking at HTML5 AppCache, and thinking I NEED to make something using this. I caught myself on a few days later and realised how absurd this sounded, I wanted to use a technical solution to solve a problem in a non-existent idea. I need to cut this attitude out and start looking for problems to solve before even thinking about solutions.&lt;/p&gt;

&lt;p&gt;I am learning that being a maker is much more than being a good engineer / designer and knowing the tools. Even having a good sense of business and knowledge of the industry is not enough. You need a desire to build, a will to deploy. Getting your apps or projects you tinker with on your machine onto the web for others to see or use should be a strong motivation for me, but so far I keep getting distracted. I complete 80% of one thing and I am onto the next new tech or next thing I want to play with.&lt;/p&gt;

&lt;p&gt;From here on in, I am going to try and make things. Not simply code up something and leave it lying dorment, but build, market and test products, not because it's a new piece of tech I want to use, but because it genuinely solves a problem, or at least attempts to.&lt;/p&gt;

&lt;p&gt;Note: Evidently I did get to use AppCache in a product, &lt;a href="http://napkin.io"&gt;napkin.io&lt;/a&gt;, a simple app for creating throwaway notes that &lt;a href="http://twitter.com/armstrong"&gt;@armstrong&lt;/a&gt; and I have been tinkering with.&lt;/p&gt;
</description>
      <link>http://phawk.co.uk/post/making-things</link>
      <author>pete@phawk.co.uk (Pete Hawkins)</author>
      <guid>http://phawk.co.uk/post/making-things</guid>
      <pubDate>Sat, 29 Sep 2012 17:57:00 +0000</pubDate>
    </item>

    <item>
      <title>Modular client-side development</title>
      <description>&lt;p&gt;From using node.js and sinatra quite a lot in the past few months it has really been evident that our client side JavaScript code and loaders don't seem to deal with the modular concept very easily. I have spent the past few weeks building modules or micro applications that form a piece of a bigger puzzle. Reading through &lt;a href="http://tjholowaychuk.com/post/27984551477/components"&gt;TJ's post on components&lt;/a&gt; at the end of last week and how the guys at learn boost have pretty much built their entire application based on this concept was comforting and you should definitely give it a read!&lt;/p&gt;

&lt;h2&gt;Philosophy&lt;/h2&gt;

&lt;p&gt;One of the most important things I have found is &lt;strong&gt;writing your module outside of your application&lt;/strong&gt;, this ensures a couple of things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your tests are completely isolated&lt;/li&gt;
&lt;li&gt;Adding a dependency to your module is a pain!&lt;/li&gt;
&lt;li&gt;Your module can be easily re-used, just drag and drop it into another app&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One of the most important things to take from this is coupling to a dependency is hard, it really forces you to think if that dependency is necessary and you will end up with code that has very loose coupling, which is always a good thing!&lt;/p&gt;

&lt;p&gt;To demonstrate how easy it is to move your code to another application, this source code I put together for this app was already in an &lt;a href="http://hedgeho.gg"&gt;app I was developing&lt;/a&gt; last week. It took all of about 5 minutes to piece together the &lt;a href="https://github.com/phawk/client-side-modules"&gt;sample code&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Walkthrough&lt;/h2&gt;

&lt;p&gt;The sample code is &lt;a href="https://github.com/phawk/client-side-modules"&gt;available on GitHub&lt;/a&gt;. If you are running this code you will need to set up a server in the root directory to make sure all of the assets are loaded in. I was lazy and just useds PHP 5.4's built in server:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ php -S localhost:8000
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;File structure&lt;/h3&gt;

&lt;p&gt;Inside the root &lt;code&gt;js&lt;/code&gt; folder we have a modules directory and one &lt;em&gt;extremely small&lt;/em&gt; module &lt;code&gt;action-dialog&lt;/code&gt;. Keeping all of your modules files: Views, Models, Collections and assets separate really makes things reusable. It will also help in a large team and help avoid people changing the same code at the same time.&lt;/p&gt;

&lt;h3&gt;Tests&lt;/h3&gt;

&lt;p&gt;With this modular approach, each module should have their own tests, take a look at the &lt;code&gt;modules/action-dialog/specs&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;For running the tests I have been using one &lt;code&gt;tests.html&lt;/code&gt; file in the root of the application. For me this approach just minimises the code taken to get a test runner up and going. With mocha it is really easy to still drill down and just view the tests for one particular module during development, just click on the &lt;strong&gt;Action Dialog View&lt;/strong&gt; heading and mocha will only run that spec.&lt;/p&gt;

&lt;h3&gt;Assets&lt;/h3&gt;

&lt;p&gt;I agree with TJ in his components article that each module should have it's own CSS and images, he adds that the CSS should only be structural CSS for layout, and not delve into typography or colour at all. Your applications independent CSS in &lt;code&gt;css/layout.css&lt;/code&gt; should be like a theme that covers all of those reusable style attributes.&lt;/p&gt;

&lt;p&gt;Also images and html should all be self contained in the module, currently I load in the HTML with the requirejs text plugin and compile it into a template using &lt;a href="http://handlebarsjs.com"&gt;Handlebars&lt;/a&gt;. Images can easily be placed into the modules CSS and then loaded into the core application that way, or referenced via the modules HTML templates.&lt;/p&gt;

&lt;h3&gt;Core Libraries&lt;/h3&gt;

&lt;p&gt;So for my applications I have several core libraries that I always make use of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://requirejs.org/"&gt;require js&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://visionmedia.github.com/mocha/"&gt;mocha&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://sinonjs.org/"&gt;sinon&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://chaijs.com/"&gt;chai&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://underscorejs.org/"&gt;underscore&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://backbonejs.org/"&gt;Backbone&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://handlebarsjs.com"&gt;Handlebars&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this example and in the applications I am building currently, these core libraries sit in &lt;code&gt;/js/libs&lt;/code&gt; and are shared between all of the modules. This does go a little against the modules being completely standalone, but it would be simple enough for a module to have its own &lt;strong&gt;libs&lt;/strong&gt; folder and manage its own library dependencies. If a new team member comes on board and doesn't like using mochas BDD style specs they could certainly write their own Qunit tests for their module and have it run those independently of the rest of the app.&lt;/p&gt;

&lt;h3&gt;main.js&lt;/h3&gt;

&lt;p&gt;To simplify loading the modules into the app, each module has its own &lt;code&gt;main.js&lt;/code&gt; file in the root of that module. This is just a convention for that module to export whatever public API is needed to run.&lt;/p&gt;

&lt;h3&gt;Documentation&lt;/h3&gt;

&lt;p&gt;Each module should have its own &lt;a href="https://github.com/phawk/client-side-modules/blob/master/js/modules/action-dialog/readme.md"&gt;readme file&lt;/a&gt; that explains how it can be used and go into detail on its public API and events.&lt;/p&gt;

&lt;h3&gt;Loading&lt;/h3&gt;

&lt;p&gt;Require js is used currently to load the modules into the app, and load their specs in and run them. While I like require JS a lot, I feel something else out there is required to make this modular approach even better. I want my loader to pull in CSS and assets as well as JavaScript. I have looked at various client side module loaders and haven't found any that solve this problem well yet. &lt;a href="http://jamjs.org/"&gt;Jam&lt;/a&gt; and &lt;a href="https://github.com/bpm/bpm/"&gt;bpm&lt;/a&gt; seem to be the nicest loaders out there at present, but are really geared towards loading in external open source modules rather than your own custom modules living in the file system.&lt;/p&gt;

&lt;p&gt;A build script using grunt or similar could solve this problem and over the next month or two I'm sure I will either find an excellent tool for this job, or try to write one.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;I am going to continue with this approach of building micro applications and glueing them together to form a large application. I have been very impressed so far by this approach, and for me it has made my code extremely lean.&lt;/p&gt;

&lt;p&gt;I'd love to hear more thoughts on potential problems with this, or anyone else using this approach and has any suggestions. Hit me up on &lt;a href="http://twitter.com/peteyhawkins"&gt;twitter&lt;/a&gt; or &lt;a href="http://github.com/phawk"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;
</description>
      <link>http://phawk.co.uk/post/modular-client-side-development</link>
      <author>pete@phawk.co.uk (Pete Hawkins)</author>
      <guid>http://phawk.co.uk/post/modular-client-side-development</guid>
      <pubDate>Sun, 19 Aug 2012 13:19:00 +0000</pubDate>
    </item>

    <item>
      <title>Trying BDD</title>
      <description>&lt;p&gt;When I started out as a developer and to this day I am always seeking ways to improve my workflow and the quality of the code I write.&lt;/p&gt;

&lt;p&gt;Before I started writing tests I would still try and do top down design, looking at the end goals, writing down the behaviors as comments in files before I would write the code to complete the task at hand.&lt;/p&gt;

&lt;p&gt;This approach certainly works well, although was just covering one advantage brought from top down design, thinking. Thinking before you code is absolutely neccessary, but as developers we need more than that, we should have confidence in our code and be able to safely refactor and improve our codebase at any point in time, without having to worry about extensive manual testing.&lt;/p&gt;

&lt;h2&gt;Starting out with tests&lt;/h2&gt;

&lt;p&gt;I've started to use behaviour driven development (BDD) as a major part of my workflow, definng business rules (behaviours) along with the design of each feature up front. Tests (or specs) should be based on these behaviours and entirely separated from the implementation used to pass them, making our specs extremely valuable for us devs and for the business people too.&lt;/p&gt;

&lt;p&gt;I am still very much new to this BDD thing, and missing out on a few key parts of BDD as a whole, sticking initially to unit test specs and the core language for describing behaviour (scenarios and given, when, then).&lt;/p&gt;

&lt;p&gt;I try to write stubs for all of my behaviours up front without writing any test code. Once I have this structure set up I can then start writing my first test specification, moving quickly on to writing the code to pass that spec, this schedule should follow a strict pattern.&lt;/p&gt;

&lt;h2&gt;Red, Green, Refactor&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;write a test spec to assert one behaviour&lt;/li&gt;
&lt;li&gt;run the test and see it fail (red)&lt;/li&gt;
&lt;li&gt;write the minimum code to pass the test&lt;/li&gt;
&lt;li&gt;run the test and see it pass (green)&lt;/li&gt;
&lt;li&gt;refactor the code so you are happy that it is readable and easily maintainable&lt;/li&gt;
&lt;li&gt;run the test again to ensure you refactored code passes&lt;/li&gt;
&lt;li&gt;repeat until you are happy with the refactored code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These steps should be done in very quick succession, 30seconds to 2minutes. I think it is ok to take longer depending on the complexity of the job at hand, but aim to make this progress as quick as possible so everything is fresh in your head. Another byproduct of quick progression ensures you won't have massive amounts of code tested by just one spec, you will end up having better test coverage.&lt;/p&gt;

&lt;p&gt;Hopefully this gives you an overview of the advantages and process to using TDD / BDD as a methodology for development. The main enviornment I am working in right now is JavaScript in the browser and on node.js, I will put together an article specifically on how I acomplish testing JavaScript and the tools I use (for now listed below).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://visionmedia.github.com/mocha/"&gt;mocha&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://chaijs.com/"&gt;chai.js&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://sinonjs.org/"&gt;sinon&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/domenic/sinon-chai/"&gt;sinon-chai&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have any tips or anything to add please feel free to comment.&lt;/p&gt;
</description>
      <link>http://phawk.co.uk/post/trying-bdd</link>
      <author>pete@phawk.co.uk (Pete Hawkins)</author>
      <guid>http://phawk.co.uk/post/trying-bdd</guid>
      <pubDate>Sat, 07 Jul 2012 19:02:00 +0000</pubDate>
    </item>

    <item>
      <title>2012 is full of goals</title>
      <description>&lt;p&gt;As this is the first time setting goals to measure myself against next year, I don't really have anything tangible to measure against from the start of 2011.&lt;/p&gt;

&lt;p&gt;The one thing I have accomplished though is getting a lot more into development. I started out from an early age as a web designer, got around 5 years commercial experience under my belt and then in mid 2010 after starting a fantastic job at &lt;a href="http://craftydevil.co.uk"&gt;Crafty Devil&lt;/a&gt; I started to learn more about development, starting with PHP and &lt;a href="http://codeigniter.com"&gt;Codeigniter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I enjoyed every minute writing code, even though my code wasn't anything special for the first 3-6months. 
Slowly I started to realise my passion for design disipating, I was growing rapidly in love with coding. I still have a strong appreciation for great design and tasty looking apps, but my heart has changed, I now can't wait to grab the laptop and learn something new about programming, systems or performance, gone are the days spent culminating over single pixel adjustments in photoshop and moving grid lines a few micro pixels in illustrator.&lt;/p&gt;

&lt;p&gt;I think I possibly should have been a developer all along, I always struggled profusely with artistic creativity and really treated design as a logical process, which is why most of my designs probably lacked that delight factor all these years. 
Through education my strong points where always math, ICT, business and accounting which makes sense why I enjoy figuring out how things work and taking complex problems and coming up with an efficient solution.&lt;/p&gt;

&lt;h3&gt;Summing up 2011&lt;/h3&gt;

&lt;p&gt;The past year has been a very educational one for me, learning new things daily and vastly growing my skills as a developer. I have got a real passion for what I do and always striving to learn more.&lt;/p&gt;

&lt;p&gt;It has been a great help to be surrounded by other talented and wise developers both in Crafty Devil and in the local community, to provide help, alternate solutions and share articles, thoughts and ideas. I love this industry!&lt;/p&gt;

&lt;h2&gt;Looking forward&lt;/h2&gt;

&lt;p&gt;Without setting realistic goals, next year I'll have nothing to measure my personal progress against. I have never made any resolutions or long term personal goals before, soon to be 25, I think it's time I started.&lt;/p&gt;

&lt;p&gt;The goals I have for 2012 are reasonably specific, and I'll explain them in a little more detail below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Contribute to open source projects&lt;/li&gt;
&lt;li&gt;Write at least 2 quality blog posts per month&lt;/li&gt;
&lt;li&gt;Launch and validate 2 apps&lt;/li&gt;
&lt;li&gt;Build a native iPhone or Mac app and have it in the app store&lt;/li&gt;
&lt;li&gt;Build either a native android or windows phone app and have it in the marketplace&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Contribute to open source projects&lt;/h3&gt;

&lt;p&gt;In my day to day work I use a ridiculously high amount of open source software, and up until two weeks ago, I have never contributed anything to an open source project. Sure I've shared a fair amount of code on &lt;a href="https://github.com/peteyhawkins"&gt;Github&lt;/a&gt;, but never contributed to a larger scale, more widely used project.&lt;/p&gt;

&lt;p&gt;I've had this goal in mind for some time, but never measured against it, hopefully this will give me the push I need to make progress.&lt;/p&gt;

&lt;p&gt;Last week I finally made my first &lt;a href="https://github.com/lmcalpin/Play--Paginate/pull/11"&gt;public pull request on GitHub&lt;/a&gt; to a &lt;a href="https://github.com/lmcalpin/Play--Paginate"&gt;Pagination module&lt;/a&gt; for Play Framework. I'm definitely not bragging about this, the only 1 word that changed in the diff was a css class in the view file, but it's a start, a very small start which gave me a bit of a moral boost getting that first pull request accepted.&lt;/p&gt;

&lt;p&gt;Hopefully I'll continue to contribute more to projects that I actively use, such as &lt;a href="http://codeigniter.com/"&gt;Codeigniter&lt;/a&gt; and &lt;a href="http://www.playframework.org/"&gt;Play Framework&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Write at least two quality blog posts a month&lt;/h3&gt;

&lt;p&gt;This goal doesn't look too hard to accomplish, but the main problem I always struggle with here is quality. I'll spend an hour writing a draft post and 10minutes later after a rapid spell check, it's live on my blog. I then have another read and find some phrase of text that barely translates to human readable english, then goes the mad scramble to change it round before one of my few twitter followers visits the page.&lt;/p&gt;

&lt;p&gt;To accomplish this goal and write quality posts I know I need to set more time aside for editing, spending an hour writing a draft post is only the start.&lt;/p&gt;

&lt;p&gt;I need to fire a few more hours towards making articles coherant. I have always struggled with writing, rushing things out because I don't enjoy reading over my own work, we'll see how this goes.&lt;/p&gt;

&lt;h3&gt;Launch and validate 2 apps&lt;/h3&gt;

&lt;p&gt;Over the past two years since I have "become" a developer I've come up with some class ideas (at least I think so anyway),  started building them and for whatever reason, moved onto something else, leaving these projects stagnant.&lt;/p&gt;

&lt;p&gt;I need to take an idea from start to finish, finish being a product launched to beta to validate whether it can work and also whether it can make money.&lt;/p&gt;

&lt;p&gt;In 2011 I've got the closest I ever have to validation, I had a product almost ready for alpha testing, when I decided to ditch the code and rewrite it based off an API. I doubt I made the correct descision as by now I probably could have validated the idea amongst a few hundred target users.&lt;/p&gt;

&lt;h3&gt;Build a native iPhone or Mac app and have it in the app store&lt;/h3&gt;

&lt;p&gt;Having looked at Cocoa touch around 4 separate times last year, I still really want to make an iPhone or Mac app that I can launch into the app store. Sure I've launched a phone gap and appcelerator titanium iPhone app into the app store through work, though it's not quite the same as having a native app in there.&lt;/p&gt;

&lt;p&gt;In 2011 I built my first native Mac app, it was a very basic monthly compound interest calculator for my father and really a simple primer on using Objective-C.&lt;/p&gt;

&lt;p&gt;I didn't seem to have any problems understanding or writting Objective-C, the main issue I have is hooking the code up to the interface and making the interface do fancy stuff.&lt;/p&gt;

&lt;p&gt;Having mainly done web development up to this point and not coming from a Computer Science university background, I've never made a desktop app, and I suppose once you get into this way of working, a lot of the knowledge will carry through to other platforms.&lt;/p&gt;

&lt;h3&gt;Build either a native android or windows phone 7 app and have it in the marketplace&lt;/h3&gt;

&lt;p&gt;I'm also very keen to look at the other side of the fence and develop either a windows phone or android application, and have something to compare against iOS and possibly consider which type of mobile app development I'd like to specialise in, if any.&lt;/p&gt;

&lt;p&gt;I'm not sure which I'll be attracted to yet, I'm currently using Java and probably going to be using it a lot more with Play Framework, that may help with writing an android app, but I usually don't go with the easy option, so I may well get to do a bit of C# this year.&lt;/p&gt;

&lt;h2&gt;Credits&lt;/h2&gt;

&lt;p&gt;Have to credit our &lt;a href="http://craftydevil.co.uk"&gt;Crafty Devil&lt;/a&gt; quarterly retreat in which we were asked to think about our goals for the year and spend 5minutes talking about them. There are probably a few extra ones in here than I spoke about at the meeting, but having more time for these to lament in my mind has helped a lot as to what my goals should be.&lt;/p&gt;

&lt;p&gt;Also have to credit &lt;a href="http://swanson.github.com/"&gt;Matt Swanson&lt;/a&gt; for posting his &lt;a href="http://swanson.github.com/blog/2011/12/26/one-developer-year-in-review-2011.html"&gt;developer year in review&lt;/a&gt;, this post made me see the value in posting your goals on your blog and then being able to measure them year on year.&lt;/p&gt;
</description>
      <link>http://phawk.co.uk/post/2012-is-full-of-goals</link>
      <author>pete@phawk.co.uk (Pete Hawkins)</author>
      <guid>http://phawk.co.uk/post/2012-is-full-of-goals</guid>
      <pubDate>Sun, 15 Jan 2012 11:24:00 +0000</pubDate>
    </item>
  </channel>
</rss>
