Wednesday, July 21, 2010

Exploring the ecosystem

There are a number of things on my todo list, including F#, ASP.NET MVC, MSpec, Munq, Moq and maybe an alternative markup engine such as StringTemplate. My other todo list includes HTML5, PhoneGap, and iPhone development. My Amazon order showed up a couple of weeks ago including Douglas Crockford's excellent JavaScript: The Good Parts, Building iPhone Apps with HTML, CSS, and JavaScript, and Expert F# 2.0.

I will admit to being a long time fan of JavaScript and it's nice to see it finally getting mind-share. Crockford's book is dense and contains a lot of good stuff and as a consequence I'm still working my way through it. I'm also still not entirely comfortable with the idea of modifying the prototypes of the predefined objects such as Object, String, etc, guess I'll reserve judgment until I'm finished reading.

As part of getting back up to date with JavaScript I've also started using QUnit to unit test a module I'm writing for accessing Safari's SQLite database. There are some excellent links here, and here (blog comments are particularly useful) re using QUnit for testing asynchronous calls. Calls to SQLite are all based on callbacks so being able to unit test asynchronously is essential.

Below is a very early example of the database access code I'm working on. Clearly I've got a long way to go but it's a start and demonstrates the module pattern I use to hide the internals. I plan to replace the database version update code so I can seamlessly update the database when upgrading the application version. I'm still trying to get my head around the K&R indenting which is now the most common indenting style in JavaScript. Having coded C++ for many years and subsequently C# I'm pretty committed to the Allman style indenting. But some of the reasons for using Allman style are no longer relevant and with LINQ in particular it is difficult (impossible?) to use Allman style consistently. In JavaScript there is at least one situation where using Allman style will result in code breaking. Douglas Crockford mentions this in his book:

I always use K&R style, putting the { at the end of a line instead of the front, because it avoids a horrible design blunder in JavaScript's return statement.
To really nail my Allman addiction I read a tweet recently from I think one of either Rob Conery or Dave Ward to the effect that JavaScript written using Allman was not to be trusted (hmm maybe it was Gruber, can't find the reference). So I guess my first job is to reformat using the new hotness and once again feel like I am worthy.

BTW, if anyone knows a good code formatter for Blogger I would be grateful, the one I used to produce the code below is not quite there.

1 var db = function ()
2 {
3 /**
4 * private
5 */
6 var CURRENT_VERSION = 1.01;
7 var shortName = 'rtt';
8 var version;
9 var displayName = 'Red Tomato Timesheet';
10 var maxSize = 65536; // bytes
11 var database;
12
13 var init = function(transaction) { console.info("Updating database to version 1"); };
14
15 var success = function(){ console.info("success");};
16 var failure = function(error){ console.info("fail: " + error);};
17
18 var up101 = function(t)
19 {
20     var sql = "\
21         CREATE TABLE projects (\
22             id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\
23             name TEXT NOT NULL,\
24             description TEXT NOT NULL);";
25      console.info("sql:" + sql);
26      t.executeSql( sql, [], function(){ console.info("version 1.0 -> 1.01 success");});
27 };
28
29 var updateVersion = function()
30 {
31     console.info("Updating database to current version " + CURRENT_VERSION);
32     try
33      {
34         switch(version)
35         {
36             case '' : database.changeVersion('', '1', init, failure, success);
37             case "1" : database.changeVersion("1", "1.01", up101, failure, success);
38         }
39     }
40     catch (e)
41     {
42         console.error("database.changeVersion failed " + e.message);
43      }
44 };
45
46 return {
47
48     /**
49     * open database
50     */
51     open: function()
52     {
53          database = openDatabase(shortName, "", displayName, maxSize);
54         version = database.version;
55         console.info("database: " + displayName + " " + version);
56         if (version != CURRENT_VERSION)
57         {
58             updateVersion();
59         }
60     },
61
62      /**
63     * isOpen
64     */
65     isOpen: function()
66     {
67         return null != database;
68     },
69
70     /**
71     * version
72     */
73     version: function()
74     {
75         return version;
76     },
77 }
78
79 } ();
80
  

No comments: