<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"
	    xml:lang="en-US" 
	    xml:base="http://peter.michaux.ca">
  <id>http://peter.michaux.ca/</id>
  <title>peter.michaux.ca</title>
  <updated>2011-07-29T17:50:00-07:00</updated>
  <link href="/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="/" rel="alternate" type="text/html"/>

<entry>
    <id>http://peter.michaux.ca/articles/scheme-from-scratch-royal-scheme-v0_1-integers</id>
    <title>Scheme from Scratch - Royal Scheme v0.1 - Integers</title>
    <updated>2011-07-29T17:50:00</updated>
    <link href="/articles/scheme-from-scratch-royal-scheme-v0_1-integers" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;It took a while but the plan is back on track. Royal Scheme is a go.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ve been picking away at the project at a leisurely pace trying to determine exactly what it will be. Blog articles seem like a great way to keep folks informed about the state of development. Hopefully some folks will want to follow along creating their own implementations again like they did with Bootstrap Scheme. It was a whole bunch of fun.&lt;/p&gt;

&lt;p&gt;I think a book format would be a better format for truly documenting the iterative development of a real Scheme interpreter that is implemented in C. I still haven&amp;rsquo;t found that book and if I was taking a university course then I&amp;rsquo;d want to take the course that has that book as its primary text. So a lot of what Royal Scheme development will be about is ensuring the order of introducing features is just right and that the concepts underlying the implementation are explained in code comments and in a book. It is a lofty goal. We&amp;rsquo;ll see how it goes. I&amp;rsquo;m hoping for feedback from you.&lt;/p&gt;

&lt;p&gt;Just as Bootstrap Scheme started with integers, so does Royal Scheme.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ./scm
Welcome to Royal Scheme. Ctrl-c to exit.
&gt; 123
123
&gt; -123
-123
&gt; +007
7
&gt; ^C
$&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I&amp;rsquo;ve put the code on github. I&amp;rsquo;m new to git and github which adds a bit more flavor to the project. You can browse the code at the following address&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://github.com/petermichaux/royal-scheme&quot;&gt;http://github.com/petermichaux/royal-scheme&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and I created a branch specifically for this integers-only version&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/petermichaux/royal-scheme/tree/v0.1&quot;&gt;https://github.com/petermichaux/royal-scheme/tree/v0.1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can get the code with the following command&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone https://github.com/petermichaux/royal-scheme.git&lt;/pre&gt;&lt;/code&gt;

&lt;p&gt;You should be able to just run &lt;code&gt;make&lt;/code&gt; and then the above REPL session example should work.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;m hoping you will scrutinize the code and really give me grief that I&amp;rsquo;ve done something silly, stupid, overly complex, have a trailing space on a line, haven&amp;rsquo;t written a comment where a comment would be helpful, etc. I&amp;rsquo;m particularly interested in what you think of checking the return value of &lt;code&gt;printf&lt;/code&gt; and &lt;code&gt;scm_write&lt;/code&gt; in &lt;code&gt;repl&lt;/code&gt;. It doesn&amp;rsquo;t feel right quite to me. Also the overflow checking in &lt;code&gt;scm_read_number&lt;/code&gt; happens each iteration which is not as efficient as it possibly could be.&lt;/p&gt;

&lt;p&gt;By the way, registration for the &lt;a href=&quot;http://scheme2011.ucombinator.org/&quot;&gt;Scheme 2011 Workshop&lt;/a&gt; will open in August. I&amp;rsquo;m planning on going and hoping to talk with some people there about Royal Scheme.&lt;/p&gt;

&lt;p&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-royal-scheme-planning&quot;&gt;Introduction&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/javascript-is-dead-long-live-javascript</id>
    <title>JavaScript is Dead. Long Live JavaScript!</title>
    <updated>2011-06-25T20:21:00</updated>
    <link href="/articles/javascript-is-dead-long-live-javascript" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;For 16 years, JavaScript has been the language of the web browser. This language has enabled the building of compelling web applications and contributed to the success of the web. Other scripting languages could have filled the role JavaScript does but JavaScript was in the right place at the right time. Microsoft added Basic to Internet Explorer a long time ago but JavaScript was the language in all the browsers and so it won. Since JavaScript has been cross-browser and at least good enough, the browser makers have not needed to add other language interpreters to the browsers themselves.&lt;/p&gt;

&lt;p&gt;But we still didn&amp;rsquo;t choose to use JavaScript. It has been the one option. It has powerful features like closures and we learned to love these features while turning a blind eye to the warty parts of the language. There has even been a lot of apologizing to the programming community along the lines of &amp;ldquo;Yes we know it is a pretty funky language but please give it a chance. I&amp;rsquo;m sure you&amp;rsquo;ll learn to love it.&amp;rdquo; It shouldn&amp;rsquo;t be that hard to sell the quality of the language. JavaScript has been described as an experiment that escaped the lab a little too early and we&amp;rsquo;ve been stuck with the warts ever since.&lt;/p&gt;

&lt;p&gt;Still, JavaScript has been a great language. In 2007, Steve Yegge declared JavaScript as &lt;a href=&quot;http://steve-yegge.blogspot.com/2007/02/next-big-language.html&quot;&gt;The Next Big Language&lt;/a&gt; and it has been. Between then and now JavaScript-based web-applications have become bigger and better. With JavaScript&amp;rsquo;s help, the web has continued to flourish even with the threat of native mobile apps taking over the same space.&lt;/p&gt;

&lt;p&gt;In very recent times, JavaScript has been making its most successful attempt at being a server-side language. The Node.js platform, with its non-blocking I/O, may be solving a problem that programmers have needed solved for a long time. JavaScript is the language for Node.js and so JavaScript may go along for the ride to become a successful server-side language after many failed attempts in the past.&lt;/p&gt;

&lt;p&gt;Overall, JavaScript has been a wild success. The most popular programming language in the world. But if the browser died today, how much new JavaScript code would be written tomorrow? Most likely JavaScript would become a fringe language overnight. But the browser isn&amp;rsquo;t dying tomorrow. We will be programming for it for years to come.&lt;/p&gt;

&lt;p&gt;As JavaScript has been used for more and larger programs, the warts of the language have become more apparent and caused increasing amounts of grief for many developers. It may come as a surprise that even though I&amp;rsquo;ve written about JavaScript for years, I&amp;rsquo;m not a JavaScript fanboy. Most of my JavaScript articles have been about me working through the difficulties of finding a peaceful coexistence with the language. Yes I&amp;rsquo;ve enjoyed programming in JavaScript and have learned a lot but there certainly have been times when I&amp;rsquo;ve felt like I&amp;rsquo;m wallowing in the muck.&lt;/p&gt;

&lt;p&gt;One of the most obvious deficiencies in JavaScript is with its user interface: its syntax. The inability to quickly improve its syntax has lead to the language&amp;rsquo;s downfall.&lt;/p&gt;


&lt;h2&gt;The Case for Syntax Extensions: Verbose Idioms&lt;/h2&gt;

&lt;p&gt;Here we look at just four of the many examples where JavaScript&amp;rsquo;s syntax could be improved to remove verbose idioms from our daily programming existences.&lt;/p&gt;


&lt;h3&gt;Optional Parameters and Default Values&lt;/h3&gt;

&lt;p&gt;JavaScript functions can be called with a variable number of actual parameters. This makes some of the formal parameters in the function&amp;rsquo;s declaration optional. Often times these optional parameters must be set to a default value if no actual parameter is supplied. I&amp;rsquo;m willing to bet you&amp;rsquo;ve written and seen plenty of code like the following.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function(a, b, option) {
    option = option || {};
    // ...
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I have and still write code like this. This code is wrong. If a falsy value is passed to the function for &lt;code&gt;option&lt;/code&gt; then the wrong thing happens. We should be writing the following.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function(a, b, option) {
    option = arguments.length &amp;gt; 2 ? option : {};
    // ...
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Although this code expresses the intended thought of setting a default value if an actual parameter for &lt;code&gt;option&lt;/code&gt; is not supplied, it is too verbose and less readable. The reader must count to determine which variable is has index 2. Beyond the awkwardness, the correct version is more difficult to maintain. It is easy to produce the following buggy code if the &lt;code&gt;b&lt;/code&gt; parameter is removed.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function(a, option) {
    option = arguments.length &amp;gt; 2 ? option : {};
    // ...
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If your application uses optional parameters with default values, some new syntax would be beneficial for you.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function(a, b, option = {}) {
    // ...
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The addition of this new syntax tunes the language better to your application&amp;rsquo;s needs.&lt;/p&gt;


&lt;h3&gt;Let&lt;/h3&gt;

&lt;p&gt;Does the following buggy code look familiar?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for (var i=0, ilen=elements.length; i&amp;lt;ilen; i++) {
    var element = elements[i];
    LIB_addEventListener(element, 'click', function(event) {
        alert('I was originally number ' + i);
    });
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All the elements were the same number?! The solution is to use an immediately evaluated function expression so each alert reports a different number. This is the &amp;ldquo;let&amp;rdquo; idiom after Lisp&amp;rsquo;s various &lt;code&gt;let&lt;/code&gt; forms.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for (var i=0, ilen=elements.length; i&amp;lt;ilen; i++) {
    var element = elements[i];
    (function(num) {
        LIB_addEventListener(element, 'click', function(event) {
            alert('I was originally number ' + num);
        });
    }(i));
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sure sometimes delegate listeners might be better than the above code but sometimes the above code is the desired idea. In the above case, we are trying to bind the order of the elements when the loop runs. This order could be lost with the delegate pattern if the elements are rearranged in the DOM.&lt;/p&gt;

&lt;p&gt;This syntax is particularly awkward because of the distance between the formal &lt;code&gt;num&lt;/code&gt; and actual &lt;code&gt;i&lt;/code&gt; parameters of the immediate function.&lt;/p&gt;

&lt;p&gt;The immediate function could be factored out to another location and called inside the loop.&lt;/p&gt;
    
&lt;pre&gt;&lt;code&gt;function attachListener(element, num) {
    LIB_addEventListener(element, 'click', function(event) {
        alert('I was originally number ' + num);
    });
}
for (var i=0, ilen=elements.length; i&amp;lt;ilen; i++) {
    attachListener(elements[i], i);
}&lt;/code&gt;&lt;/pre&gt;
   
&lt;p&gt;Even with this option, sometimes programmers still use the immediate function because it conveys better their intended message to readers.&lt;/p&gt;  
 
&lt;p&gt;If your application uses the let idiom, wouldn&amp;rsquo;t it be nice to have new syntax for it?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;for (var i=0, ilen=elements.length; i&amp;lt;ilen; i++) {
    var element = elements[i];
    let (num = i) {
        LIB_addEventListener(element, function(event) {
            alert('I was originally number ' + num);
        });
    };
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With &lt;code&gt;num&lt;/code&gt; and &lt;code&gt;i&lt;/code&gt; together it is much easier to read this code and a new scope containing &lt;code&gt;new&lt;/code&gt; has been introduced so the closure works properly. Once again, the addition of new syntax can tune the language better to your application&amp;rsquo;s needs.&lt;/p&gt;


&lt;h3&gt;Modules&lt;/h3&gt;

&lt;p&gt;One of the most common idioms in JavaScript programs we all know and love is usually called &amp;ldquo;the module pattern.&amp;rdquo; This idiom provides the benefits of encapsulated variables that are private to the module and imparts sanity to our code.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var event = (function() {

    // private variables
    var listeners = [];
   
    function addEventListener(f) {
        listeners.push(f);
    }

    function clearEventListeners() {
        listeners = [];
    }
    
    // ...
    
    // export the module's API
    return {
        addEventListener: addEventListener,
        clearEventListeners: clearEventListeners
        // ...
    };
}());&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The goal of encapsulation here isn&amp;rsquo;t security. It is to ensure that other developers keep their dirty, monkey-patching hands off your module&amp;rsquo;s data.&lt;/p&gt;

&lt;p&gt;The exporting can be done a few ways but no matter which way there is some boiler plate.&lt;/p&gt;

&lt;p&gt;Importing is verbose also&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(function() {

    // import desired properties of the event module
    var addEventListener = event.addEventListener;
    var clearEventListeners = event.clearEventListeners;
    
    // ...
}());&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Some new syntax would be nice to convey the intent of the module pattern better.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;module event {
    
    // private variables
    var listeners = [];
   
    export function addEventListener(f) {
        listeners.push(f);
    }

    export function clearEventListeners() {
        listeners = [];
    }
    
    // ...
}&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code&gt;(function() {

    import event;
    
    // ...
}());&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The module pattern is almost everywhere and some new syntax to better express this idiom would better tune the language to all of our applications.&lt;/p&gt;


&lt;h3&gt;Inheritance&lt;/h3&gt;

&lt;p&gt;These idiom examples have been growing in how they each span an increasing number of lines of code. The JavaScript idiom that potentially spans the most lines of your program may be the inheritance idiom.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function Employee(first, last, position) {
    // call the superclass constructor
    Person.call(this, first, last);
    this.position = position;
};
// inherit from Person
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;

// define an overridding toString() method
Employee.prototype.toString = function() {
    // call superclass's overridden toString() method
    return Person.prototype.toString.call(this) +
           ' is a ' + this.position;
};&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What a mess. Yes JavaScript makes single inheritance like this possible by linking prototypes but it takes a lot of code to manually make super things happen they way you might like inheritance to work.&lt;/p&gt;

&lt;p&gt;If there could be a maintenance nightmare that is it. The strings &amp;ldquo;Person&amp;rdquo; and &amp;ldquo;Employee&amp;rdquo; are sprawled throughout the code for the Employee &amp;ldquo;class&amp;rdquo;.&lt;/p&gt;

&lt;p&gt;If classes with inheritance are a big part of your application, some syntax would really help clean up the code. Perhaps something like the following.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Employee extends Person {
    constructor(first, last, position) {
        super(first, last);
        public position = position;
    }
 
    update(camera) {
        return super.update() + ' is a ' + position;
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That is a major improvement.&lt;/p&gt;


&lt;h3&gt;Learning from Idioms&lt;/h3&gt;

&lt;p&gt;These common idioms make it clear that JavaScript could use some new syntax for several reasons. The ability to do what we want is usually in JavaScript somewhere. The verbosity of the idioms is sometimes too much. We cut corners. Sometimes we avoid certain algorithms because the idioms are too verbose. The idioms are not self explanatory. They are inside secrets of the community and the intent is not easily recognized by outsiders.&lt;/p&gt;

&lt;p&gt;The ECMAScript committee has recognized that the above idioms, and others idioms, are common across a wide variety of applications. There are proposals for all of these syntax cases in Harmony. Some of them may make it into the next version of ECMAScript and into the browsers for your use. If new syntax gets there eventually then you can use use it.&lt;/p&gt;

&lt;p&gt;Idioms may have emerged in &lt;em&gt;your&lt;/em&gt; application that do not appear in a wide variety of applications. For example, Node.js&amp;rsquo;s non-blocking I/O and heavy reliance on callbacks will undoubtedly result in idioms that do not appear in browser scripts. Syntax specific to just your applications will likely never make it into ECMAScript.&lt;/p&gt;

&lt;p&gt;If you would like to use specialized syntax for the general idioms like those shown above or you would like syntax for your own idioms, what can you do?&lt;/p&gt;


&lt;h2&gt;If Only We Had Macros&lt;/h2&gt;

&lt;p&gt;Lisp languages have had full-blown macros for decades. Through macros, Lisp gives programmers the ability to tune the language&amp;rsquo;s syntax to best match their own applications. Because of its macros, Lisp has been described as &amp;ldquo;the programmable programming language.&amp;rdquo; Powerful stuff.&lt;/p&gt;

&lt;p&gt;Lisp&amp;rsquo;s s-expression syntax, you know, the syntax with all those parens, gives the language a special property called homoiconicity. It roughly means the syntax of the language is also the syntax of its data structures or that a program&amp;rsquo;s parse tree uses the languages data structures. This homoiconicity makes Lisp&amp;rsquo;s macros possible.&lt;/p&gt;

&lt;p&gt;JavaScript doesn&amp;rsquo;t have macros. A major reason for this is that adding macros to languages with C-like syntax, languages that are not homoiconic, is still a research topic. Excerpts from a short conversation on Twitter with littlecalculist, Dave Herman, Ph.D., ECMAScript committee member, and Mozilla Researcher:&lt;/p&gt;

&lt;blockquote&gt;
    &lt;dl&gt;
        &lt;dt&gt;@petermichaux&lt;/dt&gt;
        &lt;dd&gt;I believe @littlecalculist knows more about the research and possibilities for macros in ECMAScript. I&amp;rsquo;d like to know more also.&lt;/dd&gt;
        
        &lt;dt&gt;@littlecalculist&lt;dt&gt;
        &lt;dd&gt;I have thoughts on it for sure. But macros for non-sexp languages is still very much a challenging research topic&lt;/dd&gt;

        &lt;dt&gt;@petermichaux&lt;dt&gt;
        &lt;dd&gt;[...] We&amp;rsquo;ll turn blue waiting for macros.&lt;/dd&gt;

        &lt;dt&gt;@littlecalculist&lt;dt&gt;
        &lt;dd&gt;Might I suggest you keep breathing? ;-) Seriously, I do hope to try, but trust me, macro system design is hard.&lt;/dd&gt;
    &lt;/dl&gt;
&lt;/blockquote&gt;

&lt;p&gt;The message is pretty clear. JavaScript macros are not just around the corner.&lt;/p&gt;


&lt;h2&gt;Harmony Syntax ETA&lt;/h2&gt;

&lt;p&gt;Perhaps the syntax extensions in Harmony are all you dream of and more. If Harmony becomes ECMAScript&amp;nbsp;6 and ECMAScript&amp;nbsp;6 becomes ubiquitous then you&amp;rsquo;ll be all set. So all you need to do is wait...patiently.&lt;/p&gt;

&lt;p&gt;First, let&amp;rsquo;s look at browser adoption. Unfortunately I&amp;rsquo;ll pick on Internet Explorer a bit, which has almost become a cliche, but not because I have something against the Internet Explorer development team at Microsoft or the newest versions of Internet Explorer. It is necessary to look at this browser because it is perhaps the most important case study for estimating when features will be available for all visitors to your web site.&lt;/p&gt;

&lt;p&gt;As of May 2011, &lt;a href=&quot;http://www.w3schools.com/browsers/browsers_stats.asp&quot;&gt;w3schools&lt;/a&gt;, which has relatively tech-savvy visitors, reports Internet Explorer&amp;nbsp;6 still has 2.4% market share. &lt;a href=&quot;http://marketshare.hitslink.com/browser-market-share.aspx?spider=1&amp;amp;qprid=2&quot;&gt;Net Market Share&lt;/a&gt; reports Internet Explorer&amp;nbsp;6 still has 10.36% market share. Your site probably has a market share somewhere between those two numbers but this browser is still hanging on even though it was superseded by Internet Explorer&amp;nbsp;7 in November 2006. How many people are still using Internet Explorer&amp;nbsp;6 or 7? The w3schools site shows 7.7% and Net Market Share shows 18.2%. These browsers just aren&amp;rsquo;t going away fast enough. A publicly available site (e.g. Amazon) cannot afford to ignore market share numbers this large.&lt;/p&gt;
    
&lt;p&gt;There is no point in spending any energy moaning that users should upgrade their browsers or that &amp;ldquo;IE should die!&amp;rdquo; It won&amp;rsquo;t happen. I don&amp;rsquo;t know if it is true but someone once said to me &amp;ldquo;Internet Explorer users upgrade their browser when they upgrade their hardware.&amp;rdquo; For the past few years, hardware has certainly become sufficient that people don&amp;rsquo;t need to upgrade anything to use email, Facebook, Twitter, etc.&lt;/p&gt;

&lt;p&gt;Suppose your web app is sufficiently advanced that you&amp;rsquo;ve decided that you only care about users with &amp;ldquo;modern&amp;rdquo; browsers. Google Apps recently announced that on August 1, 2011 they will stop supporting Internet Explorer&amp;nbsp;7. That is almost 5 years after Internet Explorer&amp;nbsp;7 was released.&lt;/p&gt;
    
&lt;p&gt;Now think about this: Internet Explorer&amp;nbsp;10 is not out yet but, of course, even it won&amp;rsquo;t have Harmony&amp;rsquo;s syntax extensions. Let&amp;rsquo;s estimate that Harmony is approved as ECMAScript&amp;nbsp;6 in mid-2012 and Internet Explorer&amp;nbsp;11 is released in early 2013 with support for all of Harmony&amp;rsquo;s syntax. Five years after that, in 2018, the Google Apps team can drop support for Internet Explorer&amp;nbsp;11 and finally use Harmony syntax freely. (On August 1, 2011, they are also dropping support for the four-year-old Safari&amp;nbsp;3 and two-year-old Firefox&amp;nbsp;3.5 so the waits are still long-ish for other browsers too.)&lt;/p&gt;

&lt;p&gt;Amazon developers might need to wait an additional 5&amp;nbsp;years before they can use Harmony syntax. That&amp;rsquo;s 2023!&lt;/p&gt;

&lt;p&gt;Will you be satisfied waiting 7-12&amp;nbsp;years before you can start using syntax that would be helpful developing your web apps today? Being more optimistic, even if the wait is just 5&amp;nbsp;years, will you wait?&lt;/p&gt;


&lt;h2&gt;JavaScript is Dead.&lt;/h2&gt;

&lt;p&gt;Cause of death: semicolon cancer.&lt;/p&gt;

&lt;p&gt;Perhaps due to JavaScript&amp;rsquo;s syntax alone, JavaScript does not have macros now and won&amp;rsquo;t have them soon if ever. Millions of programmers use JavaScript now and plenty of them are tired or tiring of the verbose idioms confronting them daily. They want new syntax now and won&amp;rsquo;t wait. For this growing group of developers, JavaScript the source code language is dead.&lt;/p&gt;

&lt;p&gt;You had a good reign, JavaScript. We had some good times and wrote some cool apps together. May you rest in peace.&lt;/p&gt;


&lt;h2&gt;Long Live JavaScript!&lt;/h2&gt;

&lt;p&gt;Programmers like to control their own destiny and they are taking action. You can have all the new syntax you want for your browser scripts, right now, if you write in another source language and compile to the ECMAScript&amp;nbsp;3 dialect of JavaScript for the billions of browsers in the wild. By compiling to ECMAScript&amp;nbsp;3 you are completely freed from JavaScript&amp;rsquo;s syntactic evolution. As an added bonus, you can even &lt;em&gt;fix&lt;/em&gt; some of JavaScript&amp;rsquo;s semantic gotchas with a sufficiently sophisticated compiler. JavaScript&amp;rsquo;s new life is as a compilation target.&lt;/p&gt;


&lt;h2&gt;Languages that Compile to JavaScript&lt;/h2&gt;

&lt;p&gt;There have been compilers to JavaScript for years now. In 2007, I started collecting a list of languages with compilers to JavaScript. There were JavaScript extension languages: the now-defunct ECMAScript&amp;nbsp;4, Narrative JavaScript, and Objective-J. There were pre-existing languages: Scheme, Common Lisp, Smalltalk, Ruby, Python, Java, C#, Haskell, etc. There were even brand new languages HaXe, Milescript, Links, Flapjax that were designed to address web programming needs.&lt;/p&gt;

&lt;p&gt;Of these compiler projects, Google&amp;rsquo;s GWT Java-to-JavaScript compiler has probably been the most successful but I don&amp;rsquo;t see programmers who first learned a language other than Java rushing to use Java as their source code language. In fact, none of these compiler projects have accrued a significant long-term user base. At least in the parts of the web that I frequent, with the exception of GWT, it is rare to read about programmers using these compilers for any real projects. There are several legitimate reasons not to use one of these compilers.&lt;/p&gt;

&lt;p&gt;Imagine you build a big project using one of these compilers and part way through find a bug in the compiler. The one maintainer of the compiler may have lost interest or time. Do you want to maintain a compiler? Does anyone on your team have the skills to do that? That ulcer is going to get pretty big while you prepare to explain to the CEO that you now need to rewrite the UI in JavaScript.&lt;/p&gt;

&lt;p&gt;Just the thought of debugging compiled code when a production bug is filed makes my stomach churn. Core dump. What line number in the source code matches the line number Firebug is reporting for the compiled code? HURRY! That bug needs to be fixed now!!!!&lt;/p&gt;

&lt;p&gt;You&amp;rsquo;ve used Objective-J for a big project and now you need to hire a new good programmer. What are your chances of finding the right person? They are probably very low. Just finding an available JavaScript programmer is difficult enough. If you use one of these alternate languages, it is very likely you&amp;rsquo;ll need to train each new person you add to your team.&lt;/p&gt;

&lt;p&gt;Even without these compiler projects being wildly successful, the list of languages that compile to JavaScript has continued to grow. There is no doubt that writing a to-JavaScript compiler is a very cool project. Please pay me to write one.&lt;/p&gt;

&lt;p&gt;There is one notable new entry in the list of languages that compile to JavaScript that is actually causing a big stir and is possibly changing the game for good.&lt;/p&gt;


&lt;h3&gt;CoffeeScript&lt;/h3&gt;

&lt;p&gt;I can tell you right now, I don&amp;rsquo;t know why CoffeeScript has the magic combination of features to garner the attention it has when other projects have failed. Significant whitespace and arrow function syntax. My gut reaction is yuck. There is plenty of things to like: default parameter values, rest parameters, spread, destructuring, fixing the whole implied global mess, even classes if you&amp;rsquo;re into that kind of thing and more. Many of CoffeeScript&amp;rsquo;s features are part of Harmony and so may be in browsers sometime in the future but if you use CoffeeScript then you can have them now. There is nothing like instant gratification.&lt;/p&gt;

&lt;p&gt;Programmers are bursting with affection for CoffeeScript.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;dl&gt;
        &lt;dt&gt;@pyronicide&lt;/dt&gt;
        &lt;dd&gt;Being able to use default values for function arguments in #coffeescript makes me immensely happy.&lt;/dd&gt;
    &lt;/dl&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
    &lt;dl&gt;
        &lt;dt&gt;@_jdpage&lt;/dt&gt;
        &lt;dd&gt;CoffeeScript is wonderful. Now I am spoiled and will complain whenever I have to write JavaScript instead.&lt;/dd&gt;
    &lt;/dl&gt;
&lt;/blockquote&gt;

&lt;p&gt;At the TXJS 2011 conference, Douglas Crockford apparently shared that he thinks &amp;ldquo;CoffeeScript is clearly good stuff.&amp;rdquo;&lt;/p&gt;

&lt;p&gt;There is one aspect of the CoffeeScript project that I really like and it is summed up by the following two quotations. The first comes from Trevor Burnham, author of &lt;a href=&quot;http://pragprog.com/titles/tbcoffee/coffeescript&quot;&gt;CoffeeScript: Accelerated JavaScript Development&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;dl&gt;
        &lt;dt&gt;@trevorburnham&lt;/dt&gt;
        &lt;dd&gt;[...] It&amp;rsquo;s not about turning JS into Ruby/Python; it&amp;rsquo;s about having a syntax better suited to JavaScript&amp;rsquo;s inner goodness.&lt;/dd&gt;
    &lt;/dl&gt;
&lt;/blockquote&gt;

&lt;p&gt;The second is from &lt;a href=&quot;http://arcturo.com/library/coffeescript/00_introduction.html&quot;&gt;The Little Book on CoffeeScript&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;CoffeeScript neatly sidesteps these [JavaScript issues], by only exposing a curated selection of JavaScript features.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The attitude expressed in these quotations really is great and genius marketing too. It is not CoffeeScript verse JavaScript. It is CoffeeScript enhancing the JavaScript programming experience. Could some different syntax and a restricted subset of JavaScript&amp;rsquo;s features really be better than plain old JavaScript?&lt;/p&gt;

&lt;p&gt;Douglas Crockford seems to think so. For years, his &lt;a href=&quot;http://jslint.com/&quot;&gt;JSLint&lt;/a&gt; has been hurting our feelings and demanding that we use very specific whitespace and syntax and that we avoid dangerous JavaScript features. Source code that passes JSLint has access to a true subset of JavaScript: the subset that he calls &amp;ldquo;the good parts.&amp;rdquo; This dialect of JavaScript deserves a name. Maybe GoodScript? After all, you are only allowed to use JSLint for good and not for evil.&lt;/p&gt;

&lt;p&gt;The ECMAScript committee also thinks this is a good idea. The &lt;code&gt;&quot;use strict&quot;&lt;/code&gt; pragma introduced in ECMAScript&amp;nbsp;5 not only restricts some language features like &lt;code&gt;with&lt;/code&gt;, strict mode even changes/fixes the semantics of some parts of the language. Because of the semantic changes, ECMAScript&amp;nbsp;5 strict is a different language or at least a different dialect than ECMAScript&amp;nbsp;5 non-strict.&lt;/p&gt;

&lt;p&gt;CoffeeScript, GoodScript, and ECMAScript&amp;nbsp;5 strict share common goals of keeping you away from the dangerous parts of JavaScript while giving you access to the valuable, safe parts. Each enforces these goals differently but they are enforced one way or another. You don&amp;rsquo;t get new syntax with GoodScript. It is already JavaScript and ready for the browser. You don&amp;rsquo;t get to use ECMAScript&amp;nbsp;5 strict because it is not available in all browsers yet and won&amp;rsquo;t be for years.&lt;/p&gt;

&lt;p&gt;So CoffeeScript seems to be targeting a particular need of web development and maybe that is something other to-JavaScript compilers haven&amp;rsquo;t done or haven&amp;rsquo;t done well before.&lt;/p&gt;

&lt;p&gt;CoffeeScript is also a reasonably thin skin over JavaScript. One consequence of this is that the compiled JavaScript code is reasonably easy to read and not brutal to debug (so I&amp;rsquo;m told.) This reduced debugging fear is contributing to interest in CoffeeScript.&lt;/p&gt;

&lt;p&gt;CoffeeScript almost feels like a set of macros for writing JavaScript programs.&lt;/p&gt;

&lt;p&gt;Since CoffeeScript compilers are in the hands of developers rather than the visitors to sites, you control which version of CoffeeScript you are using. You can upgrade at will. This also means CoffeeScript does not need to be standardized and go through the subsequent painfully slow growth of a standardized language. CoffeeScript can grow at the rate of its community&amp;rsquo;s imagination and desire. JavaScript standardization was essential to the success of the web but the same constraints do not apply to CoffeeScript.&lt;/p&gt;


&lt;h2&gt;Well then I&amp;rsquo;m going to invent my own language.&lt;/h2&gt;

&lt;p&gt;You can do this and it would be a great exercise. You&amp;rsquo;ll be able to call yourself a compiler writer which is pretty darned prestigious.&lt;/p&gt;

&lt;p&gt;The danger of inventing your own language lies in the thinking that you can do better than JavaScript has done &lt;strong&gt;in the long run&lt;/strong&gt;. Language design is hard and I bet your language will grow its share of unsightly hairs. Maybe not as many hairs as JavaScript but still. CoffeeScript hasn&amp;rsquo;t hit puberty yet but there are already signs that hair follicles may exist.&lt;/p&gt;

&lt;blockquote&gt;
    &lt;dl&gt;
        &lt;dt&gt;@maxsamukha&lt;/dt&gt;
        &lt;dd&gt;CoffeeScript: The way variables in outer scopes can be accidentally overwritten is fishy. How bad is it in practice?&lt;/dd&gt;
    &lt;/dl&gt;
&lt;/blockquote&gt;

&lt;p&gt;You were so proud that your simple, readable compiled code was easy to debug. That will become harder as you realize the compiled code&amp;rsquo;s semantics aren&amp;rsquo;t quite what they were supposed to be in corner cases.&lt;/p&gt;
    
&lt;p&gt;Idioms will appear in your language and someone will fork your compiler to do away with those idioms (unless your language happens to have macros.)&lt;/p&gt;

&lt;p&gt;Enough with this nay saying. Go and write your own language right now. You&amp;rsquo;ll be a better programmer for it.&lt;/p&gt;


&lt;h2&gt;What&amp;rsquo;s missing from JavaScript the target language?&lt;/h2&gt;

&lt;p&gt;JavaScript is embarking on a new life as a compilation target. It is a capable target for many language features but it could use improvement. In his &lt;a href=&quot;http://brendaneich.com/2011/05/my-jsconf-us-presentation/&quot;&gt;JSConf.US talk&lt;/a&gt;, Brendan Eich stated that one the goals for Harmony is to be a better target for to-JavaScript compilers.&lt;/p&gt;

&lt;p&gt;Compiled JavaScript can run slower than hand-written JavaScript just like compiled C can be slower than hand-written Assembly (though not always.) Some inefficiency in compiled JavaScript is tolerable because JavaScript virtual machines are fast and the DOM is the bottleneck anyway. That said, some potential source code languages have semantics sufficiently far from JavaScript&amp;rsquo;s semantics that the compiled code is so inefficient that it cannot realistically be used in production web apps. There are already features in Harmony that will enable some of these languages to be compiled to efficient code and thus make these viable source code languages.&lt;/p&gt;


&lt;h3&gt;Proper Tail Calls&lt;/h3&gt;

&lt;p&gt;Programs in Lisp languages depend heavily on recursive calls to properly tail recursive procedures. The &lt;a href=&quot;http://wiki.ecmascript.org/doku.php?id=harmony:proper_tail_calls&amp;amp;s=const&quot;&gt;proper tail calls&lt;/a&gt; proposal in Harmony will allow these programs to be compiled to JavaScript without the inefficient trampoline technique currently necessary to avoid overflowing the call stack. Awesome! Let&amp;rsquo;s look at that in a bit more detail.&lt;/p&gt;

&lt;p&gt;The following JavaScript is an example of mutually recursive functions which would work fine in Lisp but since JavaScript currently does not have proper tail calls, will overflow the JavaScript call stack when &lt;code&gt;number&lt;/code&gt; is large.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function isEven(number) {
    if (number === 0) {
        return true;
    }
    else {
        return isOdd(number - 1);
    }
}

function isOdd(number) {
    if (number === 0) {
        return false;
    }
    else {
        return isEven(number - 1);
    }
}

isEven(100000); // InternalError: too much recursion&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the above code, a call to &lt;code&gt;isEven(100000)&lt;/code&gt; isn&amp;rsquo;t complete and removed from the call stack until &lt;code&gt;isOdd(99999)&lt;/code&gt; returns which isn&amp;rsquo;t complete and removed from the call stack until &lt;code&gt;isEven(99998)&lt;/code&gt; returns and so on. That&amp;rsquo;s a lot of calls on the call stack! And there is no need for them all to be on the call stack. &lt;code&gt;isEven(100000)&lt;/code&gt; doesn&amp;rsquo;t have anything intelligent remaining to do once it calls &lt;code&gt;isOdd(99999)&lt;/code&gt; because the call to &lt;code&gt;isOdd(99999)&lt;/code&gt; is the very last thing in &lt;code&gt;isEven(100000)&lt;/code&gt;. The call is said to be in tail position. &lt;code&gt;isEven(100000)&lt;/code&gt; is just waiting to return the value returned by &lt;code&gt;isOdd(99999)&lt;/code&gt; and a clever language can just pop &lt;code&gt;isEven(100000)&lt;/code&gt; off the call stack and replace it with the call to &lt;code&gt;isOdd(99999)&lt;/code&gt; thus saving space on the call stack.&lt;/p&gt; 

&lt;p&gt;With trampolines we can ensure that even in JavaScript the call stack doesn&amp;rsquo;t grow large. The following is just a sketch of how trampolines might be implemented and how &lt;code&gt;isEven&lt;/code&gt; and &lt;code&gt;isOdd&lt;/code&gt; might be compiled for a JavaScript interpreter that doesn&amp;rsquo;t have proper tail calls.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function bounce(ret) {
    while (typeof ret === 'function') {
        ret = ret();
    }
    return ret;
}

function isEven(number) {
    if (number === 0) {
        return true;
    }
    else {
        return function() {
            return isOdd(number - 1);
        };
    }
}

function isOdd(number) {
    if (number === 0) {
        return false;
    }
    else {
        return function() {
            return isEven(number - 1);
        };
    }
}

bounce(function() {return isEven(100000);}); // true&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can see the extra overhead of the &lt;code&gt;bounce&lt;/code&gt; function which implements the trampoline calls is quite onerous. It creates closures for each continuation, doubles the number of calls, and must examine the returned value after each bounce; however, &lt;code&gt;isEven(10000)&lt;/code&gt; does complete and is removed from the callstack &lt;em&gt;before&lt;/em&gt; &lt;code&gt;isOdd(99999)&lt;/code&gt; is called. The full computation completes in constant call stack space.&lt;/p&gt;

&lt;p&gt;The bottom line is trampolines incur too much overhead. They incur so much overhead that when Rich Hickey created Clojure, his Lisp for the JVM, he decided his language could not have proper tail calls because trampolines were too expensive. That must have been a painful decision for someone who loves the Lisp family of languages.&lt;/p&gt;

&lt;p&gt;The good news is that the ECMAScript committee has recognized this deficiency in JavaScript and has added proper tail calls to Harmony. This will benefit programmers writing directly in JavaScript and developers of to-JavaScript compilers.&lt;/p&gt;


&lt;h3&gt;Lambdas&lt;/h3&gt;

&lt;p&gt;Another strawman proposal not yet promoted to Harmony is the &lt;a href=&quot;http://wiki.ecmascript.org/doku.php?id=strawman:block_lambda_revival&quot;&gt;Block Lambda Revival&lt;/a&gt; which combines some new syntax with a new language construct called a lambda. A lambda is a callable thing, like a function, but it obeys Tennent&amp;rsquo;s Correspondence Principle. Tennent&amp;rsquo;s Correspondence Principle states that wrapping an expression or block of code in an immediate lambda should not change the meaning of that wrapped code. JavaScript functions are not lambdas and do not obey Tennent&amp;rsquo;s Correspondence Principle. For example,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function one() {
    return 1;
}

one(); // 1&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;is not the same when the return line is wrapped in an immediate function&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function one() {
    (function() {
        return 1;
    }());
}

one(); // undefined&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The syntax of the block lambda proposal for a lambda that takes two arguments and sums them is &lt;code&gt;{|a,&amp;nbsp;b|&amp;nbsp;a&amp;nbsp;+&amp;nbsp;b}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using the syntax of the block lambda proposal  we can wrap the return line in an immediate lambda without changing the meaning.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function one() {
    ({||
        return 1;
    }());
}

one(); // 1&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Thanks to lambdas obeying Tennent&amp;rsquo;s Correspondence Principle, the &lt;code&gt;return&lt;/code&gt; still means return from the function &lt;code&gt;one&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You may ask &amp;ldquo;Who cares about this computer science-y stuff?&amp;rdquo; to which I reply &amp;ldquo;Did any of that macro business seem useful?&amp;rdquo; because lambdas are a fundamental building block in the code generated by many macros. And since lambdas play nicely with JavaScript&amp;rsquo;s &lt;code&gt;return&lt;/code&gt;, &lt;code&gt;arguments&lt;/code&gt;, and &lt;code&gt;this&lt;/code&gt;, lambdas would be a valuable addition to the language for compiler writers.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;m focusing on the JavaScript-as-a-target aspect of the block lambda proposal. If you plan on writing in JavaScript forever after, the proposal has other benefits for you too that don&amp;rsquo;t matter to compiler writers.&lt;/p&gt;

&lt;p&gt;The block lambda strawman has not yet been promoted to Harmony and so currently doesn&amp;rsquo;t have a path into ECMAScript&amp;nbsp;6 and browsers around the world. The standardization process is slow. Maybe there will be another decade stall in evolving the language like there was after ECMAScript&amp;nbsp;3. Maybe the next edition of ECMAScript will be the last edition. Better to get the best stuff in there than hope to get it in later. If you care about the future of JavaScript and think lambdas would be a benefit to the language then &lt;strong&gt;let the ECMAScript committee know by sending a message to the &lt;a href=&quot;https://mail.mozilla.org/listinfo/es-discuss&quot;&gt;es-discuss mailing list&lt;/a&gt; in support of the block lambda proposal&lt;/strong&gt;. Powerful, orthogonal language features like tail calls and lambdas which have no efficient workarounds may be the most important additions in the next edition of ECMAScript.&lt;/p&gt;


&lt;h2&gt;What&amp;rsquo;s missing from the browser?&lt;/h2&gt;

&lt;p&gt;The browser can help JavaScript succeed in its new life as a compilation target. The browser interprets compiled JavaScript and complains when the compiled code has an error. The error messages contain line numbers in the compiled code but you want to know the line number in source code.&lt;/p&gt;

&lt;p&gt;The Mozilla crowd has &lt;a href=&quot;https://bugzilla.mozilla.org/show_bug.cgi?id=618650&quot;&gt;an open ticket&lt;/a&gt; and &lt;a href=&quot;https://wiki.mozilla.org/DevTools/Features/SourceMap&quot;&gt;a dev tools project&lt;/a&gt; to ease debugging source code by mapping compiled code lines to source code lines. Since you control the browser in which you primarily develop and test, you can use this feature as soon as it is ready.&lt;/p&gt;

&lt;p&gt;Somewhere I read that the Webkit folks are developing similar source code debugging assistance but I cannot find any concrete evidence.&lt;/p&gt;


&lt;h2&gt;Polyglot&lt;/h2&gt;

&lt;p&gt;JavaScript&amp;rsquo;s monopoly in the browser has meant front-end programmers all speak the same language. This has given the community a &lt;i&gt;lingua franca&lt;/i&gt; enabling good communication and code sharing. We are now headed for a polyglot future where you choose the language(s) you want to learn and use for developing browser applications. Maybe you are only using a few JavaScript language extensions via &lt;a href=&quot;http://code.google.com/p/traceur-compiler/&quot;&gt;Traceur&lt;/a&gt; and compiling templates to JavaScript with &lt;a href=&quot;http://mustache.github.com/&quot;&gt;Mustache&lt;/a&gt;. This still means that the CoffeeScript programmer won&amp;rsquo;t understand your source code immediately.&lt;/p&gt;

&lt;p&gt;This divergence was inevitable as it has happened on all previously successful platforms. There have been multiple languages for building native applications almost forever. C is still common but C++, Objective-C, and many other languages are available. The JVM was intended to run programs written in Java but clever developers have added other languages like Clojure and JRuby as options. Microsoft recognized what seems to be a human psychological need for a variety of languages and developed its .NET CLR platform for multiple languages from the beginning. Programs written in Basic, C#, IronPython, etc can all be run on the CLR.&lt;/p&gt;

&lt;p&gt;Barriors to communication in the front-end community are not new anyway. A developer using Dojo cannot immediately understand the source code of an application written with jQuery or YUI. There is a plethora of libraries, some very different, that add the ideas of class-based inheritance to JavaScript. So we already have our barriors even within JavaScript.&lt;/p&gt;

&lt;p&gt;Having multiple source languages will increase the barriers in our community. Programmers will still need to know JavaScript, at least for a while, but in a few years they may know other source languages better then they know JavaScript.&lt;/p&gt;

&lt;p&gt;Choice is a blessing and a curse.&lt;/p&gt;


&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;It is great having the opportunity to watch JavaScript&amp;rsquo;s transition to a new life as it happens. It&amp;rsquo;s impossible to say which languages will end up winning market share in the to-JavaScript compilation but it is sure to be interesting. CoffeeScript is gaining momentum now but I think many other successful source code languages will follow.&lt;/p&gt;

&lt;p&gt;What do you think will happen?&lt;/p&gt;


</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/sicp-classes-for-javascript</id>
    <title>SICP Classes for JavaScript</title>
    <updated>2011-06-05T11:56:00</updated>
    <link href="/articles/sicp-classes-for-javascript" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; This article is intended for JavaScript programmers. Parens are coming but only briefly and you can handle it and it will be good for you.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;Structure and Interpretation of Programming Languages Second Edition&lt;/strong&gt; (SICP) on page 182, the authors introduce the idea of &lt;i&gt;message passing&lt;/i&gt; with the following example in Scheme of a complex number constructor function.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (make-from-real-imag x y)
  (define (dispatch op)
    (cond ((eq? op 'real-part) x)
          ((eq? op 'imag-part) y)
          ((eq? op 'magnitude)
           (sqrt (+ (square x) (square y))))
          ((eq? op 'angle) (atan y x))
          (else
           (error &quot;Uknown op -- MAKE-FROM-REAL-IMAG&quot; op))))
  dispatch)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The important part to note here is that the value returned by the &lt;code&gt;make-from-real-imag&lt;/code&gt; constructor function is actually a dispatch procedure that you can call with a message argument. You can send messages to get the real part or magnitude of the complex number.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define c (make-from-real-imag 3 4))
(c 'real-part) ; 3
(c 'imag-part) ; 4
(c 'magnitude) ; 5
(c 'angle)     ; 0.927295218001612
(c 'asdf)      ; ERROR: Uknown op -- MAKE-FROM-REAL-IMAG: asdf&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Let&amp;rsquo;s see what the above code looks like in JavaScript, our &lt;i&gt;lingua franca&lt;/i&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function makeFromRealImag(x, y) {
    function dispatch(op) {
        switch (op) {
            case 'realPart': return x;
            case 'imagPart': return y;
            case 'magnitude':
                return Math.sqrt(x*x + y*y);
            case 'angle': return Math.atan2(y, x);
            default:
                throw 'Unknown op -- makeFromRealImag: ' + op;
        }
    }
    return dispatch;
}

var c = makeFromRealImag(3, 4);
c('realPart');  // 3
c('imagPart');  // 4
c('magnitude'); // 5
c('angle');     // 0.9272952180016122
c('asdf');      // &quot;Unknown op -- makeFromRealImag: asdf&quot;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now this probably doesn&amp;rsquo;t look like any object-oriented JavaScript you&amp;rsquo;ve seen before but it illustrates an important point. In JavaScript, we can represent the idea of an object as a function of its messages. The constructor function returns a dispatch function that you wrote that can dispatch any message any way that you want it to. This immediately gives you Spidermonkey&amp;rsquo;s &lt;code&gt;__nosuchmethod__&lt;/code&gt;, Smalltalk&amp;rsquo;s &lt;code&gt;doesNotUnderstand&lt;/code&gt;, and Ruby&amp;rsquo;s &lt;code&gt;method_missing&lt;/code&gt;. Powerful stuff but unfortunately the JavaScript code above runs very slowly. We can move towards a faster and more familiar JavaScript style.&lt;/p&gt;

&lt;p&gt;SICP page 223, introduces the idea of mutable objects but the most interesting point is the variation on the dispatch procedure.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(define (make-account balance)
  (define (withdraw amount)
    (if (&gt;= balance amount)
        (begin (set! balance (- balance amount))
               balance)
        &quot;Insufficient funds&quot;))
  (define (deposit amount)
    (set! balance (+ balance amount))
    balance)
  (define (dispatch m)
    (cond ((eq? m 'withdraw) withdraw)
          ((eq? m 'deposit) deposit)
          (else (error &quot;Unknown request -- MAKE-ACCOUNT&quot;
                       m))))
  dispatch)

(define account (make-account 10))
((account 'deposit) 5)  ; 15
((account 'withdraw) 3) ; 12&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Converting this to JavaScript we have the following.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function makeAccount(balance) {
    function withdraw(amount) {
        if (balance &gt;= amount) {
            balance = balance - amount;
            return balance;
        }
        return &quot;Insufficient funds&quot;;
    }
    function deposit(amount) {
        balance = balance + amount;
        return balance;
    }
    function dispatch(m) {
        switch (m) {
            case 'withdraw': return withdraw;
            case 'deposit': return deposit;
            default:
                throw &quot;Unknown request -- makeAccount: &quot; + m;
        }
    }
    return dispatch;
}

var account = makeAccount(10);
account('deposit')(5);  // 15
account('withdraw')(3); // 12&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The way the &lt;code&gt;dispatch&lt;/code&gt; function works for accounts is quite different than in the complex numbers case. In the case of complex numbers, when a message was sent to the &lt;code&gt;dispatch&lt;/code&gt; function, it executed the associated operation (i.e. the method) immediately. In contrast, the account &lt;code&gt;dispatch&lt;/code&gt; function returns the method associated with the message and that method can then be called.&lt;/p&gt;

&lt;p&gt;This is very similar to how JavaScript&amp;rsquo;s &lt;code&gt;reciever.message(arg)&lt;/code&gt; syntax works and we can move to a more familiar object-oriented JavaScript style.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function makeAccount(balance) {
    function withdraw(amount) {
        if (balance &gt;= amount) {
            balance = balance - amount;
            return balance;
        }
        return &quot;Insufficient funds&quot;;
    }
    function deposit(amount) {
        balance = balance + amount;
        return balance;
    }
    return {
      withdraw: withdraw,
      deposit: deposit
    };
}

var account = makeAccount(10);
account.deposit(5);  // 15
account.withdraw(3); // 12&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this last version, we&amp;rsquo;ve stopped writing our own dispatch logic and use JavaScript&amp;rsquo;s built-in property lookup. This increases speed significantly. We&amp;rsquo;ve lost the ability to do the &lt;code&gt;__noSuchMethod__&lt;/code&gt; type of dispatching when using standard ECMAScript but that doesn&amp;rsquo;t seem to be commonly useful anyway.&lt;/p&gt;

&lt;p&gt;For the well-read JavaScript programmers out there, you may recognize this last version as &lt;i&gt;durable objects&lt;/i&gt; from Douglas Crockford&amp;rsquo;s book &lt;strong&gt;JavaScript: The Good Parts&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I find it interesting that the word &amp;ldquo;inheritance&amp;rdquo; does not appear in SICP&amp;rsquo;s index even though the book goes on to implement complex programs like language interpreters and compilers in a message passing style. That shows this simple style of object-oriented programming can take you far.&lt;/p&gt;

&lt;p&gt;The moral of the story is that old books are worth reading and can change the way you program today. You can even &lt;a href=&quot;http://mitpress.mit.edu/sicp/&quot;&gt;read SICP for free&lt;/a&gt;.&lt;/p&gt;


</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/now-with-tweets</id>
    <title>Now with Tweets</title>
    <updated>2011-05-29T20:01:00</updated>
    <link href="/articles/now-with-tweets" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;I'm giving Twitter a try. If you &lt;a href=&quot;http://twitter.com/petermichaux&quot;&gt;follow me&lt;/a&gt; then you can watch the experiment unfold.&lt;/p&gt;
</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/mvc-to-do-application</id>
    <title>MVC To-do Application</title>
    <updated>2011-04-02T11:59:00</updated>
    <link href="/articles/mvc-to-do-application" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;The &lt;a href=&quot;/articles/mvc-architecture-for-javascript-applications&quot;&gt;MVC Architecture for JavaScript Applications&lt;/a&gt; article was well received. The clocks application showed a complete working MVC application but more examples are helpful when exploring new ideas.&lt;/p&gt;

&lt;p&gt;Today I&amp;rsquo;m posting another complete example: &lt;a href=&quot;/examples/to-do-application&quot;&gt;a to-do application&lt;/a&gt;. For best results, use the application but do not look at the source code. Try to program the application yourself and then compare and contrast source code. Then try to extend the application by adding features.&lt;/p&gt;
</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/javascript-weekly-email-newsletter</id>
    <title>JavaScript Weekly Email Newsletter</title>
    <updated>2011-04-02T11:23:00</updated>
    <link href="/articles/javascript-weekly-email-newsletter" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;A few weeks ago I subscribed to the JavaScript Weekly email newsletter. A nice collection of links to new JavaScript articles arrives in my email inbox each Friday. Perfect for weekend reading. It fills a hole left by Ajaxian&amp;rsquo;s demise. You might like it too.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://javascriptweekly.com/&quot;&gt;http://javascriptweekly.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://javascriptweekly.com/archive/&quot;&gt;JavaScript Weekly back issues&lt;/a&gt;&lt;/p&gt;</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/mvc-architecture-for-javascript-applications</id>
    <title>MVC Architecture for JavaScript Applications</title>
    <updated>2011-02-24T22:05:00</updated>
    <link href="/articles/mvc-architecture-for-javascript-applications" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;There isn&amp;rsquo;t enough discussion to read about using the model-view-controller architecture for client-side JavaScript web application development and it&amp;rsquo;s a shame.&lt;/p&gt;


&lt;h2&gt;History&lt;/h2&gt;

&lt;p&gt;When I first ventured into client-side web development, &amp;ldquo;graceful degradation&amp;rdquo; or &amp;ldquo;progressive enhancements&amp;rdquo; were all the rage. Most web pages were required to work in browsers that did not have JavaScript enabled or had very poor JavaScript support. This is still true in many cases.If feature tests showed that the browser supported the required JavaScript then little bits of interactivity were added to the page usually to save full page reloads or make a little animation. The bits of JavaScript were often so small that they didn&amp;rsquo;t require being architected. Spaghetti code was just fine and anything fancier was overkill.&lt;/p&gt;

&lt;p&gt;Early server-side programs were similar. The mainly disorganized CGI scripts accomplished their simple tasks with spaghetti code but they were manageable. Web applications became more sophisticated and the server-side shouldered the brunt of the workload as the client-side was too wimpy to do its share. The server-side code quickly grew larger and required organization. Eventually the Struts server-side web framework came along with its &amp;ldquo;Model 2&amp;rdquo; architecture and ever since most server-side web frameworks (e.g. Rails, Catalyst, etc) have touted &amp;ldquo;MVC&amp;rdquo; as the optimal way to take requests and generate responses. The request arrives to the controller. The controller gathers data from a model that talks with a database. The controller finally forwards that data to a view template to generate the response. It doesn&amp;rsquo;t have to work exactly like that but it usually does. Somewhere along the line, the terms &amp;ldquo;Model 2&amp;rdquo; and &amp;ldquo;MVC&amp;rdquo; became unnecessarily conflated but that can be left as a side discussion. Whatever you call it, the Struts-style architecture has been highly successful at organizing server-side code for its specific request-response functionality and at the very least is MVC-like...but it is definitely not the classic MVC architecture.&lt;/p&gt;

&lt;p&gt;It seems that history will mark the release of GMail as the turning point to big &amp;ldquo;Ajax&amp;rdquo; client-side web applications. The browsers had grown powerful enough and JavaScript programmers had figured out clever ways of using its features that had been around for a while. The client-side could really start doing some thinking of its own. Full blown &amp;ldquo;one-page apps&amp;rdquo; that had functionality resembling desktop application were, and still are, exciting to use. The problem is the code is big and the spaghetti style that is fine for small enhancements is definitely not fine anymore. Some kind of architecture is required for developer sanity.&lt;/p&gt;

&lt;p&gt;Many web developers have heard about MVC and have probably been introduced to it in the non-traditional Struts-like incarnation. There have even been attempts to subdue client-side program complexity by porting the Struts-style MVC that was designed for a  request-response application to the client-side. Ugg. Not the right choice.&lt;/p&gt;

&lt;p&gt;I struggled looking for books on the right way, or even just a good way, to organize large bodies of JavaScript code. I was disappointed that I couldn&amp;rsquo;t find any. I started venturing out to books on design patterns, Scheme, Java, and one particularly good book ostensibly on ActionScript but more importantly about design patterns. I learned that the classic MVC that started in the Smalltalk world and works so well for desktop applications is a perfect fit for what we do as JavaScript web application developers. I&amp;rsquo;ve been building JavaScript web applications this way for several years now and feel like there is an MVC hole in my collection articles. This article is an attempt to fill that hole.&lt;/p&gt;


&lt;h2&gt;Real MVC&lt;/h2&gt;

&lt;p&gt;In a nutshell the classic MVC architecture work like this. There is a model that is at the heart of the whole thing. If the model changes, it notifies its observers that a change occurred. The view is the stuff you can see and the view observes the model. When the view is notified that the model has changed, the view changes its appearance. The user can interact with the view (e.g. clicking stuff) but the view doesn&amp;rsquo;t know what to do. So the view tells the controller what the user did and assumes the controller knows what to do. The controller appropriately changes the model. And around and around it goes.&lt;/p&gt;

&lt;p&gt;This description is probably a nice summary for those who already understand the MVC architecture but it is way too short for someone who doesn&amp;rsquo;t know it already. Describing all the ins and outs of MVC is a task I&amp;rsquo;m not going to attempt in full. I will do three things:&lt;/p&gt;

&lt;ol&gt;
    &lt;li&gt;Recommend the books I found that explain MVC well.&lt;/li&gt;
    &lt;li&gt;Describe some tips about MVC for JavaScript web applications in particular that I have found helpful.&lt;/li&gt;
    &lt;li&gt;Provide a complete example.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Good MVC Books&lt;/h2&gt;

&lt;p&gt;There at two books in particular that were a big help for me understanding the classic MVC architecture.&lt;/p&gt;

&lt;iframe src=&quot;http://rcm.amazon.com/e/cm?t=petermichauxc-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=as1&amp;amp;asins=0596006527&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=FFFFFF&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;width:120px;height:240px;float:right;clear:right;&quot; scrolling=&quot;no&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;

&lt;p&gt;Without question, the best description of MVC I&amp;rsquo;ve read is in Colin Moock&amp;rsquo;s book &amp;ldquo;Essential ActionScript 2.0&amp;rdquo;. His follow-up book &amp;ldquo;Essential ActionScript 3.0&amp;rdquo; does not contain the in depth MVC discussion so make sure you find the correct edition. Although the book examples are in ActionScript, ActionScript is pretty darn close to JavaScript as they are both ECMAScript implementations. I don&amp;rsquo;t think I could improve on Moock&amp;rsquo;s discussions of the observer pattern and MVC. Although I&amp;rsquo;ve had the book for many years, I re-read the MVC chapter last night and it was a treat. I must be getting old if I&amp;rsquo;m drawing inspiration from books that are out of print. Maybe actually reading paper books is enough of an indicator that I&amp;rsquo;m getting old.&lt;/p&gt;

&lt;iframe src=&quot;http://rcm.amazon.com/e/cm?t=petermichauxc-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=as1&amp;amp;asins=0596007124&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=FFFFFF&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;width:120px;height:240px;float:right;clear:right;&quot; scrolling=&quot;no&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;

&lt;p&gt;The other book I found that covers MVC accessibly is &amp;ldquo;Head First Design Patterns&amp;rdquo;. The examples are in Java. Even if Java is not your favourite language, it is a perfectly suitable language for depicting an object-oriented architecture like MVC. This book gives a solid description of why the architecture is beneficial. There is also a section on the Model 2 architecture so you can compare and contrast  the two.&lt;/p&gt;

&lt;iframe src=&quot;http://rcm.amazon.com/e/cm?t=petermichauxc-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=as1&amp;amp;asins=0201633612&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=FFFFFF&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;width:120px;height:240px;float:right;clear:right;&quot; scrolling=&quot;no&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; frameborder=&quot;0&quot;&gt;&lt;/iframe&gt;

&lt;p&gt;Another book worth mention is the standard on design patterns. &amp;ldquo;Design Patterns: Elements of Reusable Object-Oriented Software&amp;rdquo; describes the three patterns that comprise MVC: observer, composite, and strategy.&lt;/p&gt;

&lt;p&gt;I feel a bit like I&amp;rsquo;m hawking books here but my earned Amazon affiliate commissions over the years is a measly $9.22. I guess I&amp;rsquo;m not a good salesman. Anyway these books are all well worthwhile and if you can borrow them from a friend or library then you are doing yourself a great favour.&lt;/p&gt;


&lt;h2&gt;MVC Example&lt;/h2&gt;

&lt;p&gt;I was planning on giving the example third but perhaps now is a better time.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;/examples/clocks/mvc-clock&quot;&gt;MVC clock example&lt;/a&gt; is a port to JavaScript of the MVC example in Moock&amp;rsquo;s book. It is a fancy stopwatch. There is a single clock model. There are two views, digital and analog, that show the model&amp;rsquo;s time. There is another view that allows the user to click on knobs and a single controller that knows how to handle those clicks to manipulate the model. Click on &amp;ldquo;start&amp;rdquo;, &amp;ldquo;stop&amp;rdquo;, and &amp;ldquo;reset&amp;rdquo;.&lt;/p&gt;

&lt;p&gt;To ensure the MVC concepts are shown clearly I have not used a library like YUI, jQuery, etc. The example uses addEventListener directly. It uses canvas with no feature testing. These choices make the MVC aspects of the example much more accessible which is the goal. Use a modern version of Firefox, Safari, or Chrome and all will be well.&lt;/p&gt;

&lt;p&gt;You can meditate on the example for days and days. I have. There is no doubt that you will see bits of the code and decide you would do things differently. If that is the case then both you and the example are doing your jobs well. I use the Scheme-style OOP that Crockford has promoted. You may not. I use has-a composition for the observer pattern. You may use is-a inheritance. I use a pull model for the observer pattern here. You may want to use a push model.&lt;/p&gt;

&lt;p&gt;To be completely honest, and slightly heretical, I don&amp;rsquo;t always use MVC properly myself. Gasp! I haven&amp;rsquo;t found that the flexibility provided by the strategy pattern (i.e. separation of view and controller) is always beneficial in the type of applications I program. Sometimes I glue them together into widgets and this does save a bit of code. I guess I could tell people I use a model-widget architecture. Here is a &lt;a href=&quot;/examples/clocks/mw-clock&quot;&gt;MW clock example&lt;/a&gt; which is close to how I sometimes program. Please feel free to flame me for my lack of separate views and controllers in the comments but at least do it nicely and in good humour.&lt;/p&gt;


&lt;h2&gt;MVC in JavaScript&lt;/h2&gt;


&lt;h3&gt;Model&lt;/h3&gt;

&lt;p&gt;The model is all about data and the model is where the primary data in the system lives.&lt;/p&gt;

&lt;p&gt;If your application needs to gather data from the server, local storage, cookies, etc then the model is where this should happen. In fact, the model is the only place in the whole system that should know anything about &lt;code&gt;XMLHttpRequest&lt;/code&gt;, for example. You may have code in the model layer that loads code for you and creates model objects. For example,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// model constructor function
//
var makeEmailModel = function(data) {
    // ...
    
    return {
        // ...
    };
}; 

// load data from the server and create
// model objects
//
var loadEmails = function(callbacks) {
    doXHR('GET', '/emails', {
        on200: function(xhr) {
            var emailsData = fromJSON(xhr.responseText);
            var emails = [];
            for (var i=0, ilen=emailsData.length; i&amp;lt;ilen; i++) {
                emails.push(makeEmailModel(emailsData[i]));
            }
            callbacks.success(emails);
        },
        onComplete: function(xhr) {
            callbacks.failure();
        }
    })
};&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;loadEmails&lt;/code&gt; function above is called by bootstrap or controller code. Give the callback properties names like &amp;ldquo;success&amp;rdquo;, &amp;ldquo;failure&amp;rdquo;, and &amp;ldquo;invalid&amp;rdquo; that are semantically meaningful to the calling code as shown in the example. Don&amp;rsquo;t use names like &amp;ldquo;on200&amp;rdquo;, &amp;ldquo;on500&amp;rdquo;, and &amp;ldquo;on400&amp;rdquo; as those names are related to the mechanics of the actual data getting. Only the model should know about those mechanics and those mechanics may change.&lt;/p&gt;

&lt;p&gt;When the model notifies its observers, the model should tell the observers what happened to the model. A model may say &amp;ldquo;hey, I changed!&amp;rdquo; and the observers then decide what they do based on that information. The model should never tell the observers what they need to do. The model will never say &amp;ldquo;you need to redraw the time you are displaying&amp;rdquo; because the model has no idea if there is any view that is drawing time. The model is oblivious to what its observers might be doing.&lt;/p&gt;

&lt;p&gt;The model can notify different groups of observers. For example some observers may only be interested in withdrawals in the following account model.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var makeBankAccountModel = function() {

    var balance = 0;
    var depositObservableSubject = makeObservableSubject();
    var withdrawalObservableSubject = makeObservableSubject();
    
    var deposit = function(amount) {
        if ((typeof amount !== 'number) || (amount &lt;= 0)) {
            throw new Error('deposits must be positive numbers');
        }
        balance += amount;
        depositObservableSubject.notifyObservers(amount);
    };
    
    var withdrawal = function(amount) {
        if ((typeof amount !== 'number) || (amount &lt;= 0)) {
            throw new Error('withdrawals must be positive numbers');
        }
        if (amount &gt; balance) {
            throw new Error('not enough money in account');
        }
        balance -= amount;
        withdrawalObservableSubject.notifyObservers(amount);
    };
    
    return {
        getBalance: function() {
            return balance;
        },
        addDepositObserver: depositObservableSubject.addObserver,
        addWithdrawalObserver: withdrawalObservableSubject.addObserver
    };
};&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note also that this bank account example pushes the amount of the deposit or the withdrawal to the observers. That information is not available for the observers to pull from the model so pushing the data helps give a more complete description of what happened to the model.&lt;/p&gt;

&lt;p&gt;If you have limited time for code reviews start by reviewing the models.&lt;/p&gt;

&lt;p&gt;The model &lt;em&gt;definitely&lt;/em&gt; should know &lt;em&gt;nothing&lt;/em&gt; about the DOM. That would be a break down of all that is good.&lt;/p&gt;


&lt;h3&gt;View&lt;/h3&gt;

&lt;p&gt;The view is what you see and what you click.&lt;/p&gt;

&lt;p&gt;The view is the &lt;em&gt;only&lt;/em&gt; part of the system that knows about the DOM. (Except perhaps some of the bootstrapping code.)&lt;/p&gt;

&lt;p&gt;Do your best to avoid using &lt;code&gt;id&lt;/code&gt; attributes on DOM elements in the view. The ability to put two views of the same class in a page twice depends on not having the same id value in the two views. There are other strategies but you should not need to find elements in the page if your views are well constructed.&lt;/p&gt;

&lt;p&gt;When a user clicks on something in the view, the view is too stupid to know what to do. In the clock example, when the user clicks the &amp;ldquo;start&amp;rdquo; button, the view doesn&amp;rsquo;t understand the user&amp;rsquo;s desire to get the clock model to start ticking. The view has to rely on its controller to make that decision. Since the view doesn&amp;rsquo;t know what should be done, it should be calling methods on the controller that explain what happened in the view. For example, the view could call a controller method named &amp;ldquo;handleStartClick&amp;rdquo;. The view should not be calling a controller method named &amp;ldquo;startTheClock&amp;rdquo; because ironically some other interchangeable controller may not start the clock when the start button is clicked.&lt;/p&gt;

&lt;p&gt;A templating library that generates HTML is &lt;em&gt;not&lt;/em&gt; a view and this is a confusion that seems to come from the Struts-style architecture. In the classic MVC, a view is a living object that observes a model and stays in sync when the model changes. A view might call a templating library to help update the view but the template is not the view. For example,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var makePersonView = function(personModel) {

    var rootEl = document.createElement('div');

    var personModelChangeObserver = function() {
        // The view calls a templating library to generate
        // HTML that shows the person's name.
        rootEl.innerHTML =
            processTemplate('personTemplate',
                            {name: personModel.getName()});
    };

    personModel.addObserver(personModelChangeObserver);

    return {
        // ...
    };
};&lt;/code&gt;&lt;/pre&gt;


&lt;h3&gt;Controller&lt;/h3&gt;

&lt;p&gt;The controller is a decision maker. When a user clicks in the view, the view forwards that event on to the controller so the controller can decide what needs doing.&lt;/p&gt;

&lt;p&gt;There is nothing particularly JavaScript-specific about controllers. They don&amp;rsquo;t know about host objects like those in the DOM or about XMLHttpRequest.&lt;/p&gt;

&lt;p&gt;Controllers mutate models and sometimes mutate views. Controllers mutating views is often not the right choice however. Suppose you have a to-do list web app. When the user checks an item as completed, the click is forwarded to the controller. The controller modifies the model item as complete which makes an async save to the server. The controller could tell the view to show a throbber while the model is saving. For example, suppose we have the following to-do model&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var makeTodoModel = function() {
    // The id is used in server's database.
    // It is set here when to-do is created.
    var id = '';
    var complete = false;
    var saving = false;
    var changeObservableSubject = makeObservableSubject();

    // ...

    var saveAsComplete = function(callbacks) {
        var saving = true;
        changeObservableSubject.notifyObservers();
        doXHR('POST', '/todo/'+id+'?complete=true', {
            on200: function(xhr) {
                saving = false;
                complete = true;
                changeObservableSubject.notifyObservers();
                callbacks.success();
            },
            onComplete: function() {
                saving = false;
                changeObservableSubject.notifyObservers();
                callbacks.failure();
            }
        });
    };

    return {
        saveAsComplete: saveAsComplete,
        addChangeObserver: changeObservableSubject.addObserver,
        getId: getId,
        getDescription: getDescription,
        getComplete: getComplete,
        getSaving: getSaving
        // ...
    };
};&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are two ways we could use the above model in the view and controller interaction. The first is the controller turns the throbber on in the view.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var makeTodoView = function(todoModel, todoController) {
    var rootEl = document.createElement('div');
    
    var todoEl = document.createElement('div');
    rootEl.appendChild(todoEl);
    
    var throbberEl = document.createELement('div');
    throbberEl.style.display = 'none';
    throbberEl.innerHTML = 'saving...';
    rootEl.appendChild(throbberEl);
    
    var todoModelChangeObserver = function() {
        todoEl.innerHTML =
            (todoModel.getComplete() ? 
                'complete' : 'incomplete') +
            ': ' + todoModel.getDescription;
    };

    todoModel.addChangeObserver(todoModelChangeObserver);

    var showThrobber = function() {
        todoEl.style.display = 'none';
        throbberEl.style.display = '';
    };
    
    var hideThrobber = function() {
        throbberEl.style.display = 'none';
        todoEl.style.display = '';
    };
    
    todoEl.addEventListener('click', function() {
        todoController.handleTodoClick(todoModel);
    }, false);

    // ...

    return {
        showThrobber: showThrobber,
        hideThrobber: hideThrobber
        // ...
    };
};

var makeTodoController = function() {
    
    var handleTodoClick = function(todoModel) {
        if (!todoModel.getComplete()) {
            todoModel.showThrobber();
            todoModel.markAsComplete({
                success: function() {
                    todoModel.hideThrobber();
                },
                failure: function() {
                    todoModel.hideThrobber();
                }
            });
        }
    };
    
    // ...
    
    return {
        handleTodoClick: handleTodoClick
        // ...
    };
};&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The potential problem with the above example is if the to-do is represented in the UI twice and that when the to-do is being saved to the server &lt;em&gt;both&lt;/em&gt; representations should show the throbber. The fix is to have the view show the throbber based on the model state. Since the model above notifies observers when it is saving we can write the following for the view and controller.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var makeTodoView = function(todoModel, todoController) {
    var rootEl = document.createElement('div');
    
    var todoEl = document.createElement('div');
    rootEl.appendChild(todoEl);
    
    var throbberEl = document.createELement('div');
    throbberEl.style.display = 'none';
    throbberEl.innerHTML = 'saving...';
    rootEl.appendChild(throbberEl);
    
    var todoModelChangeObserver = function() {
        if (todoModel.getSaving()) {
            showThrobber();
        }
        else {
            hideThrobber();
        }
        todoEl.innerHTML =
            (todoModel.getComplete() ?
                'complete' : 'incomplete') +
            ': ' + todoModel.getDescription;
    };

    todoModel.addChangeObserver(todoModelChangeObserver);

    var showThrobber = function() {
        todoEl.style.display = 'none';
        throbberEl.style.display = '';
    };
    
    var hideThrobber = function() {
        throbberEl.style.display = 'none';
        todoEl.style.display = '';
    };
    
    todoEl.addEventListener('click', function() {
        todoController.handleTodoClick(todoModel);
    }, false);

    // ...

    return {
        // ...
    };
};

var makeTodoController = function() {
    
    var handleTodoClick = function(todoModel) {
        if (!todoModel.getComplete()) {
            todoModel.markAsComplete({
                success: function() {
                    // nothing to do
                },
                failure: function() {
                    // nothing to do
                }
            });
        }
    };
    
    // ...
    
    return {
        handleTodoClick: handleTodoClick
        // ...
    };
};&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Because the view now depends on the model&amp;rsquo;s saving state data, all instances of the same to-do visible to the user will stay in sync: when the model is being saved all related views will show the throbber. This is the power of the primary data kept in the model.&lt;/p&gt;


&lt;h3&gt;Boostrap&lt;/h3&gt;

&lt;p&gt;The boostrap code is likely a bit messy to get the whole big one-page app up and running at &lt;code&gt;window.onload&lt;/code&gt;. Models, views, and controllers need to be created and their relationships need to be established. There is no standard way to do these things. Find a way that is appropriate for your application and try to keep the mess as small as possible.&lt;/p&gt;


&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Hopefully everyone noticed that an MVC &lt;em&gt;framework&lt;/em&gt; is not required to build an application using the MVC architecture. It is great that people get excited about a new design pattern or architecture but that doesn&amp;rsquo;t mean it needs to become a framework. A few helper libraries like the library for observable subjects is sufficient.&lt;/p&gt;

&lt;p&gt;Using an MVC style has helped me tame the complexity of some of the bigger projects I&amp;rsquo;ve work on and it might help you too. If you have experience with the MVC architecture then please leave a comment with any tips that you&amp;rsquo;ve discovered about how to implement using MVC in the JavaScript world. I&amp;rsquo;m very curious what others have learned through their experimentation.&lt;/p&gt;

&lt;p&gt;I've added another example in a follow-up article: &lt;a href=&quot;/articles/mvc-to-do-application&quot;&gt;MVC To-Do Application&lt;/a&gt;&lt;/p&gt;


</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/jsmentors-mailing-list</id>
    <title>JSMentors Mailing List</title>
    <updated>2010-11-25T09:34:00</updated>
    <link href="/articles/jsmentors-mailing-list" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;Recently I was pointed towards a new JavaScript mailing list called JSMentors. The list of mentors is an impressive group of experienced JavaScript programmers. If you are looking for a place to discuss JavaScript-related topics (and cannot stomach Usenet's comp.lang.javascript) then the &lt;a href=&quot;http://jsmentors.com/&quot;&gt;JSMentors mailing list&lt;/a&gt; might be just the community for you.&lt;/p&gt;
</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/scheme-from-scratch-royal-scheme-stall</id>
    <title>Scheme from Scratch - Royal Scheme Stall</title>
    <updated>2010-11-23T09:45:00</updated>
    <link href="/articles/scheme-from-scratch-royal-scheme-stall" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;Clearly the Royal Scheme project has stalled. I have real work and plenty of it filling my brain. Hopefully Royal Scheme can get going again in the future but I don&amp;rsquo;t know when that might be.&lt;/p&gt;

</content>
</entry>

<entry>
    <id>http://peter.michaux.ca/articles/scheme-from-scratch-royal-scheme-planning</id>
    <title>Scheme from Scratch - Royal Scheme Planning</title>
    <updated>2010-08-06T15:45:00</updated>
    <link href="/articles/scheme-from-scratch-royal-scheme-planning" rel="alternate" type="text/html"/>
		<content type="html">&lt;p&gt;The &lt;a href=&quot;/articles/scheme-from-scratch-introduction&quot;&gt;Scheme from Scratch introduction&lt;/a&gt; included a list of several possible Scheme implementations. &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-v0_1-integers&quot;&gt;Bootstrap Scheme&lt;/a&gt; covered one of those possibilities: a quick and dirty C implementation of a Scheme interpreter. Folks reported that they enjoy the Bootstrap Scheme series and the way the interpreter was developed incrementally. I really enjoyed the feedback and that others were actually following along, coding their own interpreters, and experiencing the excitement when they saw their REPL&amp;rsquo;s running. I&amp;rsquo;m excited to continue the Scheme from Scratch series but which implementation next?&lt;/p&gt;

&lt;p&gt;The next implementation I will attack is a bytecode interpreting virtual machine which I&amp;rsquo;m calling Royal Scheme. This kind of implementation is actually both a compiler and an interpreter. The source code is read and converted to an abstract syntax tree. The AST is then compiled to bytecode. Finally the bytecode is interpreted by the virtual machine. The fact that the compiler and virtual machine can both be written in ANSI C means that the implementation can be portable and there is no need to learn hardware-specific machine code.&lt;/p&gt;

&lt;p&gt;I want Royal Scheme to be a good-enough Scheme implementation. A Scheme implementation that can actually be used for real programming. An engine that can be embedded in other C programs. Real garbage collection. Real error handling. A foreign-function interface. Even with these features, I&amp;rsquo;m hoping Royal Scheme won&amp;rsquo;t top 8000 lines of code. It will still be a simple version of the Scheme family of languages. It won&amp;rsquo;t be R6RS. It might be R4RS. It doesn&amp;rsquo;t need to be the world&amp;rsquo;s fastest Scheme. I&amp;rsquo;ll trade some speed for source code readability. It does need to be fast enough for some real world applications like writing a blog web application or writing a simple vim/emacs-like text editor.&lt;/p&gt;

&lt;p&gt;Implementing a bytecode interpreting Scheme has been written about. You may already have the books. Chapter 5 of &lt;b&gt;Structure and Interpretation of Computer Programs&lt;/b&gt; and chapter 7 of &lt;b&gt;Lisp in Small Pieces&lt;/b&gt; both present byte-code interpreting virtual machines for Scheme that are implemented in Scheme. I&amp;rsquo;ve spent a lot of time reading these chapters and thinking about how they might port to C. 
  
&lt;p&gt;&lt;a href=&quot;http://synthcode.com/wiki/chibi-scheme&quot;&gt;Chibi Scheme&lt;/a&gt; is a small byte-code interpreting virtual machine for Scheme that is implemented in C. Chibi is an admirable implementation and I think will influence certain parts of Royal Scheme. I&amp;rsquo;ve talked with Alex Shinn, author of Chibi, about this and he is ok with such influence.&lt;/p&gt;

&lt;p&gt;One thing I am sure about is that the pace of the project will be slower than Bootstrap Scheme. Readers kept me excited and moving faster than I thought I would but the coding plus a blog post per day was tall order. Once the Royal Scheme project is genuinely under way, I&amp;rsquo;m thinking something along the lines of a post per week would be better this time around.&lt;/p&gt;

&lt;p&gt;A major detail still up in the air is how to document the project. Bootstrap Scheme was small and simple. The incremental development seemed to work well. I&amp;rsquo;m not sure that the same will work well for a fully fledged Royal Scheme. Perhaps something along the lines of Knuth&amp;rsquo;s literate programming style would be better. After all, the interpreters in chapters 5 of SICP and 7 of LiSP are presented much like the woven output of the literate programming style. I haven&amp;rsquo;t programmed in the literate style before but have thought an interpreter would be an ideal subject for such a style. Any thoughts on documenting the project? What would you like to see in your feed reader?&lt;/p&gt;

&lt;p&gt;If you are interested in watching when things start moving, I have made a place holder project on Github. There is not much there for now. I was impressed that fitzgen already found the new repository and is following it.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://github.com/petermichaux/royal-scheme&quot;&gt;http://github.com/petermichaux/royal-scheme&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
  Previous article: &lt;a href=&quot;/articles/scheme-from-scratch-bootstrap-conclusion&quot;&gt;Bootstrap Scheme Conclusion&lt;/a&gt;&lt;br&gt;
  Next article: &lt;a href=&quot;/articles/scheme-from-scratch-royal-scheme-v0_1-integers&quot;&gt;Integers&lt;/a&gt;
&lt;/p&gt;

</content>
</entry>


</feed>

