<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>Secret Weblog</title><link>http://blog.startifact.com</link><description>Martijn Faassen talks about software development - Python, Javascript, web.</description><lastBuildDate>Mon, 20 May 2013 15:53:48 GMT</lastBuildDate><generator>nikola</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Obviel 1.0!</title><link>http://blog.startifact.com/posts/obviel-1_0.html</link><description>&lt;p&gt;I'm proud to announce the release of Obviel 1.0:&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="http://www.obviel.org/en/1.0/"&gt;http://www.obviel.org/en/1.0/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Obviel is a client-side web framework that supports powerful UI
composition based on an easy to learn core. On top of that Obviel adds
a lot of features, such as templating, i18n support, form generation
and validation, and routing. Obviel stays close to HTML but lets you
build sophisticated components when you need to.&lt;/p&gt;
&lt;p&gt;Obviel has come a long way since its &lt;a class="reference external" href="http://blog.startifact.com/posts/older/obviel.html"&gt;beginnings&lt;/a&gt;. On top of the core
we've added a template language, an internationalization system that
integrates with JavaScript and templates alike and a routing
library. So it's high time for the One Dot Oh release!&lt;/p&gt;
&lt;div class="section" id="standout-features-of-obviel"&gt;
&lt;h2&gt;Standout Features of Obviel&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;a &lt;a class="reference external" href="http://blog.startifact.com/posts/the-core-of-obviel.html"&gt;powerful core&lt;/a&gt; that enables &lt;a class="reference external" href="http://blog.startifact.com/posts/powerful-composition-with-obviel-data-render.html"&gt;model-driven UI composition&lt;/a&gt;. If you
know jQuery it will be easy to learn this core.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Integrated internationalization (i18n) based on &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Gettext"&gt;gettext&lt;/a&gt;. In a world
with multiple languages we need UIs that can be easily translated to
other languages. I haven't met a client-side web framework yet that
can beat Obviel in this area.&lt;/p&gt;
&lt;p&gt;Obviel offers an i18n approach that lets you mark strings in your
JavaScript code in the gettext style:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
_("My translatable string")
&lt;/pre&gt;
&lt;p&gt;and also lets you mark translatable strings in templates:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
&amp;lt;p data-trans=""&amp;gt;My translatable string&amp;lt;/p&amp;gt;
&lt;/pre&gt;
&lt;p&gt;Obviel then offers tools to automatically extract these strings into
a &lt;tt class="docutils literal"&gt;.pot&lt;/tt&gt; file that you can offer to translators in various ways;
gettext has vary extensive tool support.&lt;/p&gt;
&lt;p&gt;See &lt;a class="reference external" href="http://www.obviel.org/en/1.0/template_i18n.html"&gt;Obviel Template i18n&lt;/a&gt; and &lt;a class="reference external" href="http://www.obviel.org/en/1.0/i18n.html"&gt;Internationalizing your Obviel Project&lt;/a&gt; for
more information.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Extensive &lt;a class="reference external" href="http://www.obviel.org/en/1.0/documentation.html"&gt;documentation&lt;/a&gt;. Documentation has been and is a priority
and Obviel has been documented to bits.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Testing is as much a priority as the docs are: Obviel is extensively
unit tested using the &lt;a class="reference external" href="http://docs.busterjs.org/en/latest/"&gt;Buster.JS&lt;/a&gt; JavaScript testing framework.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Automatic form construction using client-side validation: &lt;a class="reference internal" href="/posts/obviel-1_0.html#obviel-forms"&gt;Obviel Forms&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class="target" id="obviel-forms"&gt;Obviel Forms&lt;/span&gt;: &lt;a class="reference external" href="http://www.obviel.org/en/1.0/form.html"&gt;http://www.obviel.org/en/1.0/form.html&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Routing: path (&lt;tt class="docutils literal"&gt;/foo/bar&lt;/tt&gt;) to object and object to path with
&lt;a class="reference external" href="http://www.obviel.org/en/1.0/traject.html"&gt;Traject&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Traject lets you build a nested navigation space on the
client-side. Not only can you route a path to an object, but you can
also generate a path &lt;em&gt;for&lt;/em&gt; an object, something that results in
cleaner and more decoupled code.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="what-s-next-for-obviel"&gt;
&lt;h2&gt;What's next for Obviel?&lt;/h2&gt;
&lt;p&gt;We have been transitioning the code from bitbucket to github and from
mercurial to git to make it more accessible to JavaScript hackers who
are more familiar with git. We're still busy updating the docs, but
the transfer has been complete and the code now lives &lt;a class="reference external" href="https://github.com/obviel/obviel"&gt;on github&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;We've been doing &lt;a class="reference external" href="http://blog.startifact.com/posts/overwhelmed-by-javascript-dependencies.html"&gt;lots&lt;/a&gt; of &lt;a class="reference external" href="http://blog.startifact.com/posts/js-dependency-tools-redux.html"&gt;research&lt;/a&gt; on using a JavaScript module
system for Obviel so we can better maintain the codebase. I've been
overwhelmed by the options, but soon we'll pick one, I promise! We'll
also introduce various JavaScript codebase maintenance tools such as
the &lt;a class="reference external" href="http://gruntjs.com/"&gt;Grunt&lt;/a&gt; task runner.&lt;/p&gt;
&lt;p&gt;We are still busy working on a configurable transceiver framework for
integrating Obviel with a diversity of backends (HTTP, websockets,
localstorage): Obviel Sync. More on this soon!&lt;/p&gt;
&lt;/div&gt;
</description><guid>http://blog.startifact.com/posts/obviel-1_0.html</guid><pubDate>Mon, 20 May 2013 15:55:00 GMT</pubDate></item><item><title>JS Dependency Tools Redux</title><link>http://blog.startifact.com/posts/js-dependency-tools-redux.html</link><description>&lt;div class="section" id="introduction"&gt;
&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Recently I looked into JavaScript dependency management and wrote a
long post about it where I was &lt;a class="reference external" href="http://blog.startifact.com/posts/overwhelmed-by-javascript-dependencies.html"&gt;overwhelmed&lt;/a&gt; and found some solutions.
Since then I've learned a few new things and have thought a bit more,
and I thought I'd share.&lt;/p&gt;
&lt;p&gt;My goal is still to be able to develop &lt;a class="reference external" href="http://obviel.org"&gt;Obviel&lt;/a&gt; with smaller modules than I
do now. Obviel is a client-side web framework, but besides that you should not
need to know anything about it in order to understand this post.&lt;/p&gt;
&lt;p&gt;So, I'm looking for a JavaScript dependency framework for the client-side.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="amd-versus-commonjs"&gt;
&lt;h2&gt;AMD versus CommonJS&lt;/h2&gt;
&lt;p&gt;Last time I mentioned Asynchronous Module Definition (&lt;a class="reference external" href="http://requirejs.org/docs/whyamd.html"&gt;AMD&lt;/a&gt;) and how it
contrasts with the &lt;a class="reference external" href="http://wiki.commonjs.org/wiki/CommonJS"&gt;CommonJS&lt;/a&gt; module definition. AMD wraps everything in
your module in a function:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
define([jquery], function($) {
   var myModule = {
      foo: function() { ... };
   };
   return myModule;
});
&lt;/pre&gt;
&lt;p&gt;Whereas CommonJS uses this pattern:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
var $ = require('jquery');

module.exports = { foo: function() { ... }};
&lt;/pre&gt;
&lt;p&gt;Though AMD has sugar to make it look much like CommonJS plus the
&lt;tt class="docutils literal"&gt;define()&lt;/tt&gt; function wrapper.&lt;/p&gt;
&lt;p&gt;AMD was designed to work in web-browsers, client-side, whereas
CommonJS modules are mostly used on the server, typically in a NodeJS
environment. AMD modules can be directly loaded into the browser
without build step, which is an important advantage during
development. AMD modules like CommonJS modules can also refer to
individual modules in other packages. You could install such an
AMD-based package using &lt;a class="reference external" href="http://bower.io/"&gt;Bower&lt;/a&gt;, like you'd install a CommonJS-based
package using npm.&lt;/p&gt;
&lt;p&gt;The most well known AMD implementation is called &lt;a class="reference external" href="http://requirejs.org/"&gt;RequireJS&lt;/a&gt;. There is
a small implementation of it called &lt;a class="reference external" href="https://github.com/jrburke/almond"&gt;Almond&lt;/a&gt; that RequireJS can use in
JavaScript modules that are packaged together for release.&lt;/p&gt;
&lt;p&gt;Recently I also learned about &lt;a class="reference external" href="http://raptorjs.org/"&gt;RaptorJS&lt;/a&gt;, which is a larger framework
that features an extended AMD implementation as well as a separate
server-side module loader. It contains some interesting ideas such as
"adaptive packaging" which helps adjust codebases to different target
environments (client, servers, different browsers, etc).&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="commonjs-on-the-client"&gt;
&lt;h2&gt;CommonJS on the client&lt;/h2&gt;
&lt;p&gt;Of course people have worked on bringing CommonJS modules to the
client too. And have they! I ran into these so far:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="http://browserify.org/"&gt;browserify&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/azer/onejs"&gt;OneJS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/michaelficarra/commonjs-everywhere"&gt;commonjs-everywhere&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/krisnye/browser-build"&gt;browser-build&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/component/component"&gt;component&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;May a thousand flowers bloom! Overwhelmed, me?&lt;/p&gt;
&lt;p&gt;I've done a cursory review of these, so I apologize if I get something
wrong, but here goes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;a class="reference external" href="http://browserify.org/"&gt;browserify&lt;/a&gt; is a tool that can take a file with a CommonJS module
(and its dependencies) and bundle them all up in a &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; file you
can use in the browser.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;a class="reference external" href="https://github.com/azer/onejs"&gt;OneJS&lt;/a&gt; seems to do something very similar. The docs don't make it
immediately obvious what its distinctive features are.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;a class="reference external" href="https://github.com/michaelficarra/commonjs-everywhere"&gt;commonjs-everywhere&lt;/a&gt; does the same again (I think... the docs are
a bit technical...), but has more features.&lt;/p&gt;
&lt;p&gt;One cool thing is &lt;a class="reference external" href="http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/"&gt;source maps&lt;/a&gt; support. Source maps I just found
out about: they are basically a way to add debugging symbols to a
minified, bundled &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; file so the debugger can find out about
the original source. This is handy if you bundle plain JS, and also
makes it possible to offer better debugger support for languages
such as CoffeeScript which compile to JavaScript. Source maps are
only supported in Chrome right now, with Firefox support coming
up.&lt;/p&gt;
&lt;dl class="docutils"&gt;
&lt;dt&gt;[update: a commenter pointed out that browserify supports source maps too,&lt;/dt&gt;
&lt;dd&gt;&lt;p class="first last"&gt;pass a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--debug&lt;/span&gt;&lt;/tt&gt; flag during building to enable this]&lt;/p&gt;
&lt;/dd&gt;
&lt;/dl&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;a class="reference external" href="https://github.com/krisnye/browser-build"&gt;browser-build&lt;/a&gt; is a very recent implementation that does the same
as the others, but is focused on performance: producing a browser
version of CommonJS modules of your project really fast, so you
never have to wait for your tool during development. It has support
for source maps.&lt;/p&gt;
&lt;p&gt;But it also does something more: if you write your code in
plain JavaScript (as opposed to CoffeeScript, say), it makes your original
modules available to the browser in only very slightly edited form (the line numbers
are the same). This should help debugging a great deal in browsers
that don't support source maps.&lt;/p&gt;
&lt;p&gt;But I'm unsure about the details, as browser-build needs to have
CoffeeScript available to compile the sources and run it and I too
lazy to try this right now.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;a class="reference external" href="https://github.com/component/component"&gt;component&lt;/a&gt; is also a package manager (I'll mention it again later in
that context), but also contains a build system to generate a single
&lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; file from CommonJS modules.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All of these approaches need a build step during development, which
makes debugging harder, though source maps will help. browser-build
minimizes the build step during development to the bare minimum,
however, and this will help debugging.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="both-urequire"&gt;
&lt;h2&gt;Both? uRequire&lt;/h2&gt;
&lt;p&gt;Then there's &lt;a class="reference external" href="https://github.com/anodynos/uRequire"&gt;uRequire&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;uRequire allows you to use CommonJS &lt;em&gt;or&lt;/em&gt; AMD &lt;em&gt;or&lt;/em&gt; both and converts
them to whatever is needed. It talks about a Universal Module
Definition (&lt;a class="reference external" href="https://github.com/umdjs/umd"&gt;UMD&lt;/a&gt;) I haven't studied yet, but apparently its not
necessary to use its boilerplate when using uRequire. From what I
understand in order to use code in the browser a build step is
required.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="js-frameworks-with-a-module-system"&gt;
&lt;h2&gt;JS frameworks with a module system&lt;/h2&gt;
&lt;p&gt;There are lots of JavaScript client-side frameworks that have grown
their own module systems. I'll only talk about a few here that seem
relevant.&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="http://dojotoolkit.org/"&gt;Dojo&lt;/a&gt; had its own module system, but has started to use AMD in
recent versions and has been pushing this forward. You can use Dojo
modules directly in your own codebase - this kind of inter-package
reuse is something I will go into more detail later.&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://developers.google.com/closure/"&gt;Closure Tools&lt;/a&gt; by Google contains a lot of things, such as a
powerful JavaScript compiler and various JavaScript libraries. It
also features its own module system, which I'll also talk about more
later.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="javascript-packages-and-modules-redux"&gt;
&lt;h2&gt;JavaScript Packages and Modules Redux&lt;/h2&gt;
&lt;p&gt;In the previous blog entry I explored some concepts surrounding
dependencies and modules. I've had some new insights on how these
concepts apply to the JavaScript world. I'll review some of this
again, this time with a focus on what these concepts look like in
JavaScript.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;a &lt;em&gt;project&lt;/em&gt; is the codebase you're hacking on. In open-source JS
land, typically it's something published on github. It's in hackable
form, so contains a lot of separate &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; files to separate
concerns: modules.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;a &lt;em&gt;module&lt;/em&gt; is a JavaScript file that provides some functionality by
exposing an API.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;a module may &lt;em&gt;depend&lt;/em&gt; on another module. A module dependency is
expressed in the module itself, in source code. In JavaScript there
are multiple systems for expressing modules and their dependencies,
such as CommonJS, AMD and Google Closure.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;a &lt;em&gt;package&lt;/em&gt; is a collection of one or more modules that is published
somewhere so others may use it (this may be published on the
internet, or internal to a project). It has metadata that describes
the package, its version number, who wrote it, and what other
published packages it depends on.&lt;/p&gt;
&lt;p&gt;CommonJS packages on the server-side are distributed as essentially
an archive of a CommonJS project: a &lt;tt class="docutils literal"&gt;lib&lt;/tt&gt; directory full of
modules, with &lt;tt class="docutils literal"&gt;package.json&lt;/tt&gt; for metadata.&lt;/p&gt;
&lt;p&gt;Traditionally client-side JavaScript packages are just distributed
as URLs pointing to a &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; file that people can download. So, to
get jQuery, you download a version at the jQuery website. This is
a very large difference between browser and server.&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="http://bower.io/"&gt;Bower&lt;/a&gt; packages are a formalization of this traditional pattern:
there is a single &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; file with &lt;tt class="docutils literal"&gt;bower.json&lt;/tt&gt; metadata to
describe it. Bower adds metadata (&lt;tt class="docutils literal"&gt;bower.json&lt;/tt&gt;) and a package
index to the original story.&lt;/p&gt;
&lt;p&gt;In fact the Bower story is more complicated: it does allow you to
package up a directory of multiple modules too, which you could then
use using, say, RequireJS. This is an entirely different way to use
modules, but Bower is agnostic and just installs the stuff. Bower
also supports listing more than one &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; file in its
&lt;tt class="docutils literal"&gt;bower.json&lt;/tt&gt; configuration file; it's unclear to me what the
semantics of this is exactly.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;&lt;em&gt;Package generation&lt;/em&gt;. This is something I skipped in the previous
discussion of concepts, but is very important in the JavaScript
world especially.&lt;/p&gt;
&lt;p&gt;CommonJS packages are just archived versions of a particular project
layout: a directory with a &lt;tt class="docutils literal"&gt;package.json&lt;/tt&gt;, with a &lt;tt class="docutils literal"&gt;lib&lt;/tt&gt;
subdirectory which contains the individual &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; modules.&lt;/p&gt;
&lt;p&gt;Browser-targeted packages are most commonly shipped as a single
&lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; file as mentioned before. In the most simple case you
maintain this &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; file by hand directly and give it to others
to use.&lt;/p&gt;
&lt;p&gt;But you can also generate a single &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; package file by compiling
a bunch of &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; module files together. This is what the CommonJS
generators described above do, except for browser-build, which
actually maintains a tree of unbundled &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; modules.&lt;/p&gt;
&lt;p&gt;The realization I had, perhaps obvious, is that a client-side
JavaScript package is often shipped as a single compiled &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt;
file. It's like how a C linker works - it bundles individually units
into a larger library file (&lt;tt class="docutils literal"&gt;.so&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;.dll&lt;/tt&gt;).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;A &lt;em&gt;package manager&lt;/em&gt; is a tool that installs a package into your
system so you can start using it. &lt;tt class="docutils literal"&gt;npm&lt;/tt&gt; is popular for NodeJS,
&lt;a class="reference external" href="http://bower.io/"&gt;Bower&lt;/a&gt; is focused on client-side packages and tries to be very
package format agnostic. &lt;a class="reference external" href="https://github.com/component/component"&gt;component&lt;/a&gt; contains a package manager too,
centered around CommonJS (and also the build tool I mentioned
earlier).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;A &lt;em&gt;package registry&lt;/em&gt; is a system where packages can be registered so
that others may find and download them. npm has an index, and
so do &lt;a class="reference external" href="http://bower.io/"&gt;Bower&lt;/a&gt; and &lt;a class="reference external" href="https://github.com/component/component"&gt;component&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="mantrijs"&gt;
&lt;h2&gt;MantriJS&lt;/h2&gt;
&lt;p&gt;Another dependency system I ran into since my last post is &lt;a class="reference external" href="http://mantrijs.com/"&gt;MantriJS&lt;/a&gt;. MantriJS
is built around the Google &lt;a class="reference external" href="https://developers.google.com/closure/"&gt;Closure Tools&lt;/a&gt; but hides them from the developer,
except for the dependency system.&lt;/p&gt;
&lt;p&gt;You define a module that depends on others like this in Mantri/Closure Tools:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
goog.provide('obviel.template');
goog.require('obviel.util');

obviel.template.foo = function() { ... };
&lt;/pre&gt;
&lt;p&gt;Here you say you are defining a module called &lt;tt class="docutils literal"&gt;obviel.template&lt;/tt&gt; and
that in this module &lt;tt class="docutils literal"&gt;obviel.util&lt;/tt&gt; needs to be available. Once you
&lt;tt class="docutils literal"&gt;require&lt;/tt&gt; something you have that available to use in your module,
so you can now do this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
obviel.util.blah();
&lt;/pre&gt;
&lt;p&gt;Mantri has a build step for development, but only to build a
&lt;tt class="docutils literal"&gt;deps.js&lt;/tt&gt; file and only when you've changed a dependency. The
modules themselves are directly exposed to the browser during
development, meaning you can debug them. In this it looks quite
similar to browser-build, though browser-build does touch the
individual modules in a minor way, something MantriJS does not.&lt;/p&gt;
&lt;p&gt;MantriJS does offer a build step to generate a single file &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt;
package from your modules, using the Closure Tools.&lt;/p&gt;
&lt;p&gt;I tried to see whether MantriJS was easy to integrate with the
Buster.JS test runner; I had to wrestle quite a bit to get RequireJS
running properly before. It turned out to be very easy (it Just Worked
&amp;#8482;!). See the &lt;a class="reference external" href="https://github.com/faassen/jspakmantri"&gt;jspakmantri&lt;/a&gt; example project and compare it with the
original RequireJS-based &lt;a class="reference external" href="https://github.com/faassen/jspak"&gt;jspak&lt;/a&gt; project if you like.&lt;/p&gt;
&lt;p&gt;Thinking about MantriJS I realized something: MantriJS actually allows
you to have modules the way many existing client libraries do it:
create a namespace object and fill it with properties you want to
expose. This is important to me because that's how Obviel does it now,
and I'd prefer not to break that client API.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="global-namespace-modules"&gt;
&lt;h2&gt;Global Namespace Modules&lt;/h2&gt;
&lt;p&gt;So what is this client library module definition pattern MantriJS
supports?  Everybody is familiar with it. It's what jQuery does for
instance: it fills the &lt;tt class="docutils literal"&gt;jQuery&lt;/tt&gt; object (&lt;tt class="docutils literal"&gt;$&lt;/tt&gt;) with the jQuery API
and exposes this.&lt;/p&gt;
&lt;p&gt;For example, to make a module, you create an empty object, perhaps
listed in another object to namespace it, and make it globally
available:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
var obviel = {};
obviel.template = {};
&lt;/pre&gt;
&lt;p&gt;You then fill it with the API you want somehow, for instance like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
obviel.template.foo = function() { ... };
&lt;/pre&gt;
&lt;p&gt;or like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
(function(module) {
   module.foo = function() { ... };
}(obviel.template));
&lt;/pre&gt;
&lt;p&gt;To use a module from another one, you simply refer to it:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
obviel.template.foo();
&lt;/pre&gt;
&lt;p&gt;That's all that's needed, but there are also frameworks that help you
declare and use modules like this, such as MantriJS mentioned earlier;
YUI has another one. The primary benefit these add is the ability to
express module dependencies better, avoiding the need to mess around
with &lt;tt class="docutils literal"&gt;&amp;lt;script&amp;gt;&lt;/tt&gt; tags.&lt;/p&gt;
&lt;p&gt;So this pattern is neither CommonJS or AMD. But it is very widely used
on the client-side. Obviel uses it for instance, and Backbone too, and
Ember, and Knockout, and Mustache, and YUI, and Google Closure
Tools. To just list a few. Let's call it the Global Namespace Modules
pattern (GNM).&lt;/p&gt;
&lt;p&gt;GNM is not a module &lt;em&gt;definition&lt;/em&gt; pattern like CommonJS or AMD. Instead
it is defined by how modules are &lt;em&gt;used&lt;/em&gt;: you refer to the API of a
module using a global namespace that the module exposes (&lt;tt class="docutils literal"&gt;jQuery&lt;/tt&gt;,
&lt;tt class="docutils literal"&gt;obviel&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;Backbone&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;Mustache&lt;/tt&gt;, etc).&lt;/p&gt;
&lt;p&gt;GNM assumes that modules are loaded in a particular order,
synchronously.  You ensure this order by listing &lt;tt class="docutils literal"&gt;&amp;lt;script&amp;gt;&lt;/tt&gt; tags in
a particular order, or by using a smart module loader like MantriJS,
or by bundling modules in order into a single &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; package file.&lt;/p&gt;
&lt;p&gt;Getting this more clear for myself is quite important to me. It had
been bugging me for a while after reviewing RequireJS: if I start
using it for Obviel, do I need to to break the Obviel API, which
assumes GNM. Or do I tell all developers to start using AMD for their
code that uses Obviel too?&lt;/p&gt;
&lt;p&gt;[update: here is a &lt;a class="reference external" href="http://addyosmani.com/blog/essential-js-namespacing/"&gt;post&lt;/a&gt; with more on this pattern; here's &lt;a class="reference external" href="http://thanpol.as/javascript/development-using-namespaces/"&gt;another&lt;/a&gt;]&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="requirements"&gt;
&lt;h2&gt;Requirements&lt;/h2&gt;
&lt;p&gt;After thinking about all this, here are some varying requirements for
a JavaScript module dependency system. Ideally Obviel can adopt one
that has all of these properties, or as close as possible:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;automated loader: no &lt;tt class="docutils literal"&gt;&amp;lt;script&amp;gt;&lt;/tt&gt; tag management. (loader)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;encourage fine-grained modules. (fine)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;being able to use browser debuggers like Firebug or the Chrome Dev
tools. (debug)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;source maps not required: being able to use these debuggers without
relying on new source map technology. (nosm)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;no build step needed during development. (nobuild)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;support for exposing modules using the GNM pattern. Is this really
important? Yes, as it's a very popular pattern on the web. Dojo went
the way of telling people to use AMD for their own code, and that
does help with fine-grained reuse between packages... (gnm)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;compilation tools: bundling, minification to deliver easy to use
&lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; files. This way the browser can load a package efficiently
and it becomes easy for people to start using the API the package
exposes: just drop in a file. (comp)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;inter-package reuse: being able to require just one module from
another package without having to load all of them. (reuse)&lt;/p&gt;
&lt;p&gt;There is some tension here with the bundling into a single &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt;
package approach - if there's a module in a package that I don't
use, why does it still get shipped to a web browser? On the server
installing a bit more JS code in a package is not a problem, but on
browsers people tend to start counting bytes.&lt;/p&gt;
&lt;p&gt;This tension can be reduced in various ways: jQuery now offers
various smaller builds with less code. Build tools can cleverly only
include modules that are really required, though for inter-package
reuse this can defeat the benefit of caching.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;integration with BusterJS test runner. As this is the test runner
I use for Obviel. Preferably with the least hassle. (bjs)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;CommonJS everywhere: client definition of modules same as on server,
so CommonJS packages can be used on the client too. There is after
all potentially a lot of useful code available as a CommonJS package
that can be used on the client too, and potentially some of my
Obviel code can be run on the server too. (cjs)&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class="section" id="review"&gt;
&lt;h2&gt;Review&lt;/h2&gt;
&lt;p&gt;Let's review some of the systems mentioned in the light of these
requirements. If I get it wrong, please let me know!&lt;/p&gt;
&lt;table border="1" class="docutils"&gt;
&lt;colgroup&gt;
&lt;col width="24%"&gt;
&lt;col width="10%"&gt;
&lt;col width="7%"&gt;
&lt;col width="9%"&gt;
&lt;col width="7%"&gt;
&lt;col width="12%"&gt;
&lt;col width="5%"&gt;
&lt;col width="7%"&gt;
&lt;col width="9%"&gt;
&lt;col width="5%"&gt;
&lt;col width="5%"&gt;
&lt;/colgroup&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;td&gt;system&lt;/td&gt;
&lt;td&gt;loader&lt;/td&gt;
&lt;td&gt;fine&lt;/td&gt;
&lt;td&gt;debug&lt;/td&gt;
&lt;td&gt;nosm&lt;/td&gt;
&lt;td&gt;nobuild&lt;/td&gt;
&lt;td&gt;gnm&lt;/td&gt;
&lt;td&gt;comp&lt;/td&gt;
&lt;td&gt;reuse&lt;/td&gt;
&lt;td&gt;bjs&lt;/td&gt;
&lt;td&gt;cjs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;manual&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;RequireJS&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;browserify&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y?&lt;/td&gt;
&lt;td&gt;Y?&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;cjs-everywhere&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y?&lt;/td&gt;
&lt;td&gt;Y?&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;browser-build&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y?&lt;/td&gt;
&lt;td&gt;Y?&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;uRequire&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;?&lt;/td&gt;
&lt;td&gt;?&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y?&lt;/td&gt;
&lt;td&gt;?&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;MantriJS&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;td&gt;Y&lt;/td&gt;
&lt;td&gt;N&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;[update: source maps are also a browserify feature]&lt;/p&gt;
&lt;p&gt;A few notes from the perspective of Obviel:&lt;/p&gt;
&lt;p&gt;Nothing ticks all the boxes for Obviel from what I can see. RequireJS,
MantriJS and browser-build come closest.&lt;/p&gt;
&lt;p&gt;The manual system involves maintaining &lt;tt class="docutils literal"&gt;&amp;lt;script&amp;gt;&lt;/tt&gt; tags
yourself. That is what I'm doing with Obviel now. It involves no build
step, so debugging is easy during development. It supports the popular
global namespace modules pattern. If a framework exposes multiple
modules that users are to include using &lt;tt class="docutils literal"&gt;&amp;lt;script&amp;gt;&lt;/tt&gt; tags, like Obviel
currently does, then inter-package reuse is possible.  Compilation
into a single &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; file is not needed but there are tools that can
do it for you. But it's not fine-grained at all, breaking a
fundamental requirement for Obviel.&lt;/p&gt;
&lt;p&gt;RequireJS is quite nice; script tag management goes away, no build
step is needed but compilation to a &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; file is still possible. It
allows fine-grained reuse of modules in &lt;em&gt;other&lt;/em&gt; RequireJS based
packages, which is very nice. After some effort it integrates with
BusterJS. But it doesn't offer Global Namespace Modules support out of the
box. It shouldn't be too hard to make it do that, though, by simply
exposing some modules myself, possibly during a build step.&lt;/p&gt;
&lt;p&gt;The various CommonJS approaches are interesting. It &lt;em&gt;is&lt;/em&gt; attractive is
to be able to use same approach on the browser as on the server. But
most tools require a bundling build step and I'd like to avoid having
to rely on still uncommon source maps to do debugging. That's why
browser-build is one of the more interesting ones, as it minimizes the
build step required and makes debugging easier.&lt;/p&gt;
&lt;p&gt;I still a bit unclear to me whether fine-grained module reuse of other
npm-installed packages is possible - do these modules get exposed to
the browser too (in a bundle or directly for browser-build?). From
what I've read here and there I think so. I also haven't explored how
easy it is to integrate these with client-side Buster (server-side
Buster integration is supported by Buster), but I get the impression
it's posible.&lt;/p&gt;
&lt;p&gt;The CommonJS approaches don't offer Global Namespace Modules support
so I'd have to hack that up as for RequireJS.&lt;/p&gt;
&lt;p&gt;MantriJS was quite a revelation to me as it helped me come clarify my
thinking about the Global Namespace Modules pattern. I've contacted
the author and he's very responsive to my questions, also nice. It
turned out to be dead-easy to integrate with Buster.JS. MantriJS
assumes that external JS packages are bundled up in a single &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt;
file for reuse however, so fine-grained module reuse of other packages
is not possible.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="still-overwhelmed"&gt;
&lt;h2&gt;Still overwhelmed&lt;/h2&gt;
&lt;p&gt;I'm still &lt;a class="reference external" href="http://blog.startifact.com/posts/overwhelmed-by-javascript-dependencies.html"&gt;overwhelmed&lt;/a&gt; by the choices available as well as all the
details. But I know a bit more about what's possible and what I want
now. Are there any players in this field that I missed? Undoubtedly
more will come out of the woodwork soon. What do you think about my
requirements? Should I just give up on GNM, or forget about not having
a build step during development? Am I missing an important
requirement? Please let me know!&lt;/p&gt;
&lt;/div&gt;
</description><guid>http://blog.startifact.com/posts/js-dependency-tools-redux.html</guid><pubDate>Thu, 16 May 2013 15:20:00 GMT</pubDate></item><item><title>Obviel 1.0rc1 released!</title><link>http://blog.startifact.com/posts/obviel-1_0rc1-released.html</link><description>&lt;p&gt;I've just released &lt;a class="reference external" href="http://obviel.org"&gt;Obviel&lt;/a&gt; 1.0rc1. This, or something very close to it,
will be turned into 1.0 final.&lt;/p&gt;
&lt;p&gt;What's new in Obviel 1.0rc1?&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;upgraded and tested with newer version of jQuery (1.9.x)&lt;/li&gt;
&lt;li&gt;new Obviel Forms widget, passwordField&lt;/li&gt;
&lt;li&gt;a few fixes and improvements in Obviel Template&lt;/li&gt;
&lt;li&gt;a few fixes surrounding event handling&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;See the &lt;a class="reference external" href="http://www.obviel.org/en/1.0rc1/CHANGES.html"&gt;changelog&lt;/a&gt; for details (and ignore the (unreleased) bit -- I
have too many things to update before a release and should automate
it!). We've been getting more people contribute to Obviel lately,
thank you and please do keep it up!&lt;/p&gt;
&lt;p&gt;We're planning a few big changes for Obviel after 1.0:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;transition code from bitbucket to github, as that seems to be the
hub for JS related development and any exposure is good exposure.&lt;/li&gt;
&lt;li&gt;finally finish Obviel Sync - a very configurable framework for
syncing models with a backend (such as a HTTP server). We've made a
huge leap forward with this recently, and there is also a lot of
potential in this, so this &lt;em&gt;will&lt;/em&gt; happen.&lt;/li&gt;
&lt;li&gt;use more JavaScript tools to manage the codebase besides Buster.js
which we already use for test running. The main aim right now is to
split Obviel's code into smaller modules, and still generate a
simple usable &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; package so it's easy to include in a codebase.
See this blog entry for &lt;a class="reference external" href="http://blog.startifact.com/posts/overwhelmed-by-javascript-dependencies.html"&gt;some of my packaging explorations&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
</description><guid>http://blog.startifact.com/posts/obviel-1_0rc1-released.html</guid><pubDate>Wed, 15 May 2013 21:20:00 GMT</pubDate></item><item><title>Overwhelmed by JavaScript Dependencies</title><link>http://blog.startifact.com/posts/overwhelmed-by-javascript-dependencies.html</link><description>&lt;div class="section" id="introduction"&gt;
&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;This is about managing dependencies in a well-tested client-side
JavaScript codebase, how I got overwhelmed, and how I automated the
pieces to make it &lt;em&gt;Just work&lt;/em&gt; &amp;#8482;.&lt;/p&gt;
&lt;p&gt;The JavaScript world has grown a lot of tools for dependency
management. I've dipped my toes into it into the past, but didn't
really know much about it, especially on the client-side. Now I've
done so, and I'm somewhat overwhelmed. I also found some
solutions.&lt;/p&gt;
&lt;p&gt;If you don't feel overwhelmed by JavaScript dependency management yet,
this document can help you to &lt;em&gt;become&lt;/em&gt; overwhelmed. And then perhaps
it can help a little to become less so.&lt;/p&gt;
&lt;p&gt;[update: I've created a &lt;a class="reference external" href="http://blog.startifact.com/posts/js-dependency-tools-redux.html"&gt;followup&lt;/a&gt; to this post further analyzing the
various options available]&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="a-client-side-codebase"&gt;
&lt;h2&gt;A Client-side Codebase&lt;/h2&gt;
&lt;p&gt;The JavaScript client-side codebase I work on is fairly large; it's a
JavaScript web framework called &lt;a class="reference external" href="http://obviel.org"&gt;Obviel&lt;/a&gt;. Obviel is large immaterial to
this document however, so don't worry about it. I'm going to talk
about tools like Grunt and RequirejS and Bower and Buster instead.&lt;/p&gt;
&lt;p&gt;So far I've managed dependencies for Obviel manually by adding the
right &lt;tt class="docutils literal"&gt;&amp;lt;script&amp;gt;&lt;/tt&gt; tags on web pages. If something needs &lt;tt class="docutils literal"&gt;foo.js&lt;/tt&gt;
and &lt;tt class="docutils literal"&gt;bar.js&lt;/tt&gt; I need to manually make sure it's being included. Even
if &lt;tt class="docutils literal"&gt;bar.js&lt;/tt&gt; is only needed indirectly by &lt;tt class="docutils literal"&gt;foo.js&lt;/tt&gt; and not by the
code I'm writing myself. Managing &lt;tt class="docutils literal"&gt;&amp;lt;script&amp;gt;&lt;/tt&gt; tags by hand is doable
but annoying.&lt;/p&gt;
&lt;p&gt;Obviel is extensively unit tested. For the tests the dependency
situation is more involved. Obviel uses this nice JavaScript test
runner called &lt;a class="reference external" href="http://docs.busterjs.org/en/latest/"&gt;Buster.JS&lt;/a&gt;. It runs on the server and is based on
&lt;a class="reference external" href="http://nodejs.org/"&gt;Node.js&lt;/a&gt;. It features a command-line test runner that can be hooked
up to actual web browsers so you can run a whole set of different
tests automatically without having to go clicking about in web
browsers.&lt;/p&gt;
&lt;p&gt;Buster needs to be configured properly so that it knows what
dependencies there are to run tests in the browser. This is done in a
file called &lt;tt class="docutils literal"&gt;buster.js&lt;/tt&gt; that you put in your project. You still need
to explicitly manage dependencies by hand, though there are a few
shortcuts such as including all &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt; files in a whole directory as
dependencies at once.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="the-goal"&gt;
&lt;h2&gt;The Goal&lt;/h2&gt;
&lt;p&gt;I've always been dissatisfied with all this explicit dependency
management in Obviel's development. I want to use more fine-grained
modules in Obviel. Even it is decently modular already, I want to
break up Obviel into smaller modules still so I can more easily manage
the code during development. A hash table implementation that can have
object keys, for instance, should be in its own module, and not in,
say, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;obviel-session.js&lt;/span&gt;&lt;/tt&gt;. I want smaller modules that do one thing
well. Deployment of client-side JavaScript code favors large &lt;tt class="docutils literal"&gt;.js&lt;/tt&gt;
files, but during development I want smaller ones.&lt;/p&gt;
&lt;p&gt;Moreover, I want everything to &lt;em&gt;Just Work&lt;/em&gt; &amp;#8482;. If use a new internal
module in my project, or if I start depending on a new external
library in my project, I should be able to use them right away without
the hassle of config file editing. I should be able to hack on my code
and write unit tests and depend on stuff in them without worrying.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="my-python-background"&gt;
&lt;h2&gt;My Python Background&lt;/h2&gt;
&lt;p&gt;Over the years, I've &lt;a class="reference external" href="http://blog.startifact.com/posts/older/the-ghost-of-packaging-past-and-future.html"&gt;worked&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/older/a-history-of-python-packaging.html"&gt;a lot&lt;/a&gt; with Python dependency
management; PyPI, distutils, setuptools, pip etc. And build tools like
buildout. And of course, the Python &lt;tt class="docutils literal"&gt;import&lt;/tt&gt; statement. I learned a
lot about JavaScript tools, and the Python tools I'm already familiar
with helped me put them in the right context.&lt;/p&gt;
&lt;p&gt;For JavaScript dependency management so far I've piggy-backed on
Python's systems through &lt;a class="reference external" href="http://fanstatic.org"&gt;Fanstatic&lt;/a&gt;, which I helped create. Fanstatic
is a Python HTTP middleware that I helped create. It can automatically
insert needed dependencies (&lt;tt class="docutils literal"&gt;.js&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;.css&lt;/tt&gt;, etc) into a web page on
the server-side. Using Fanstatic-wrapped Obviel (the &lt;a class="reference external" href="https://pypi.python.org/pypi/js.obviel"&gt;js.obviel&lt;/a&gt;
Python library) works pretty well in a Python project, but it doesn't
work for &lt;em&gt;developing&lt;/em&gt; Obviel and it won't work for people who don't
use Fanstatic or even Python.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="dependency-management"&gt;
&lt;h2&gt;Dependency management&lt;/h2&gt;
&lt;p&gt;What do I mean by dependency management anyway? Let's write down a few
concepts:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;a &lt;em&gt;project&lt;/em&gt; is the codebase I'm hacking on. It could be an
application or a framework or a library. It's in hackable form,
checked out from a version control repository such as git. You can
check out jQuery or the Linux Kernel or Obviel as a project. A
project can typically be used to generate one or more packages.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;a &lt;em&gt;module&lt;/em&gt; is a source code unit (normally a file) that provides
some functionality. It contains things like functions and
classes. Usually it exposes an API. A project typically contains
multiple modules. So &lt;tt class="docutils literal"&gt;foo.js&lt;/tt&gt; is a module, and so is &lt;tt class="docutils literal"&gt;foo.py&lt;/tt&gt;,
&lt;tt class="docutils literal"&gt;foo.c&lt;/tt&gt;, etc.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;a module may &lt;em&gt;depend&lt;/em&gt; on another module. A module dependency is
expressed in the module itself, in source code.&lt;/p&gt;
&lt;p&gt;In Python and many languages this is done by &lt;em&gt;importing&lt;/em&gt; the module
by name somehow:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
import foo
&lt;/pre&gt;
&lt;p&gt;The C module system is bolted on through textual inclusion during
the compilation phase:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
#include &amp;lt;stdio.h&amp;gt;
&lt;/pre&gt;
&lt;p&gt;In JavaScript there &lt;em&gt;is&lt;/em&gt; no native way to express module
dependencies, but people have created frameworks for it such as
Node's module loading system and RequireJS, which I'll go into later.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;a &lt;em&gt;package&lt;/em&gt; is a collection of one or more modules that is published
somewhere so others may use it (this may be published on the
internet, or internal to a project). It has metadata that describes
the package, its version number, who wrote it, and what other
published packages it depends on. In Python this information is in a
file called &lt;tt class="docutils literal"&gt;setup.py&lt;/tt&gt;, in JavaScript it's... well, it depends.&lt;/p&gt;
&lt;p&gt;I'll note that Linux distributions also feature packages that can be
installed. I'll call these &lt;em&gt;deployment packages&lt;/em&gt;. Deployment
packages for various reasons are not very convenient to develop
against. This is why many languages such as Python or JavaScript or
Ruby or Perl have language-specific package systems. I'm focusing on
such development-oriented packaging systems here.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;a module in package A may depend on another module in package B. In
this case package A describes in its metadata that it depends on
package B. This is an &lt;em&gt;external&lt;/em&gt; dependency on a module in another
package.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;A &lt;em&gt;package manager&lt;/em&gt; is a tool that installs a package into your
system so you can start using it. It's distinct from a version
control system which installs a &lt;em&gt;project&lt;/em&gt; into your system so you
can start hacking on it, though package managers can be built on top
of version control system software.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;A &lt;em&gt;package registry&lt;/em&gt; is a system where packages can be registered so
that others may find and download them. CPAN is the package registry
for Perl code, for instance. Some of these systems allow manual
download of packages through a web interface as well as automated
downloads; Python's PyPI is the example I'm most familiar
with. JavaScript has several package registries.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When I develop a project I want to be able to express in my metadata
somewhere that it depends on some packages, and in my project's
modules I want to express what other modules they depend on.&lt;/p&gt;
&lt;p&gt;I don't want to have to worry about other config files for this, as
that's only more code to maintain and more mistakes to make.&lt;/p&gt;
&lt;p&gt;And if I check out a project, I want to start hacking on it as soon as
I can. To get the project's external dependencies I want to be able to
run a command that does that for me.&lt;/p&gt;
&lt;p&gt;Again, it should &lt;em&gt;Just Work&lt;/em&gt; &amp;#8482;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="server-side-js-packaging"&gt;
&lt;h2&gt;server-side js packaging&lt;/h2&gt;
&lt;p&gt;I mentioned Buster.JS before. Using Buster.JS and its various plugins
for linting (jshint) and code coverage and so on introduced me to the
world of &lt;a class="reference external" href="https://npmjs.org/"&gt;npm&lt;/a&gt;, the package manager for Node.js.&lt;/p&gt;
&lt;p&gt;npm is built around the &lt;a class="reference external" href="http://wiki.commonjs.org/wiki/CommonJS"&gt;CommonJS&lt;/a&gt; specs for JavaScript package
management. There's a file called &lt;tt class="docutils literal"&gt;package.json&lt;/tt&gt; that describes what
dependencies packages have on others. Like what &lt;tt class="docutils literal"&gt;setup.py&lt;/tt&gt; does for
Python packages.&lt;/p&gt;
&lt;p&gt;There's also a registry of published packages for npm; this is like
PyPI for Python, CPAN for Perl, etc. npm lets you to download and
install packages from this registry, either by hand or by reading a
config file. This is much like easy_install or pip for Python.&lt;/p&gt;
&lt;p&gt;So npm provides the equivalent of what pypi + distutils + distribute +
pip is for Python. I haven't really studied npm in detail yet, but it
seems nice. npm appears to be more coherent than the Python
equivalents, which grew over the years layering on top of each other,
sometimes glued together in hacky ways. npm looks cleaner.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="server-side-imports"&gt;
&lt;h2&gt;server-side imports&lt;/h2&gt;
&lt;p&gt;Unlike Python and many other languages, JavaScript doesn't have a
standard way to import modules. So people had to invent some!&lt;/p&gt;
&lt;p&gt;In Node.JS, import works like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
var otherModule = require('otherModule');
&lt;/pre&gt;
&lt;p&gt;You can then use &lt;tt class="docutils literal"&gt;otherModule&lt;/tt&gt; like a module, so call functions
on it for instance:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
otherModule.someFunction();
&lt;/pre&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;require()&lt;/tt&gt; doesn't just take names, but paths, such as relative paths,
so you can see stuff like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
``require('../otherModule');``
&lt;/pre&gt;
&lt;p&gt;to get a module from one directory higher up.&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;npm&lt;/tt&gt; installs modules in a directory called &lt;tt class="docutils literal"&gt;node_modules&lt;/tt&gt; which
is often in the project's home directory. There are some lookup rules
I don't quite fully grasp yet for getting modules with &lt;tt class="docutils literal"&gt;require()&lt;/tt&gt;
from other places.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="client-side-imports"&gt;
&lt;h2&gt;client-side imports&lt;/h2&gt;
&lt;p&gt;But Obviel isn't a server-side JavaScript codebase; it's a client-side
one that runs in the browser. So I need dependency management for
browser JS.&lt;/p&gt;
&lt;p&gt;The big fish here is &lt;a class="reference external" href="http://requirejs.org/"&gt;RequireJS&lt;/a&gt;. It is a client-side library that
implements a spec called Asynchronous Module Definition, or &lt;a class="reference external" href="https://github.com/amdjs/amdjs-api/wiki/AMD"&gt;AMD&lt;/a&gt;. AMD
modules look like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
define(['jquery', 'obviel'], function($, obviel) {
   ...
   obviel.view({...});
   ...
   $('.foo').render(blah);
   ...
});
&lt;/pre&gt;
&lt;p&gt;So, a module is wrapped in a function that's passed to
&lt;tt class="docutils literal"&gt;define()&lt;/tt&gt;. AMD also allows this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
define(function(require) {
   var otherModule = require('otherModule');
});
&lt;/pre&gt;
&lt;p&gt;which starts to look a lot like CommonJS, expect this wrapper function
around it all.&lt;/p&gt;
&lt;p&gt;AMD was actually born originally as part of the CommonJS project, but
there was some difference of opinion and the AMD folks went off on
their own.&lt;/p&gt;
&lt;p&gt;My understanding is that the main point of contention is that the AMD
folks figured an explicit function wrapper was the way to go for
client-side code to ensure maximum portability of JavaScript code (no
preprocessing of JS code necessary), and because it's good practice
anyway on the browser to avoid global variables from leaking out of
your code. The CommonJS folks wanted the client-side code to look more
like server-side modules. See the &lt;a class="reference external" href="http://requirejs.org/docs/whyamd.html"&gt;Why AMD?&lt;/a&gt; document for more on
this from the AMD perspective.&lt;/p&gt;
&lt;p&gt;How does RequireJS know where to find modules? You need to specify
where it looks for modules in a config file. In that config file you
can map one path to another, so you can tell it where to look for your
project's modules, and where jQuery is and so on.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="client-side-packaging"&gt;
&lt;h2&gt;client-side packaging&lt;/h2&gt;
&lt;p&gt;That brings us to client-side packaging. One popular package manager
for doing this is called &lt;a class="reference external" href="http://bower.io/"&gt;Bower&lt;/a&gt;. It introduces this config file called
&lt;tt class="docutils literal"&gt;bower.json&lt;/tt&gt; which is like &lt;tt class="docutils literal"&gt;package.json&lt;/tt&gt; (or &lt;tt class="docutils literal"&gt;setup.py&lt;/tt&gt;) but
then for declaring front-end package metadata and dependencies. Bower
also introduces its own package registry and you have a command-line
tool to install packages, which end up in a &lt;tt class="docutils literal"&gt;components&lt;/tt&gt; directory,
much like the way &lt;tt class="docutils literal"&gt;npm&lt;/tt&gt; installs server-side packages into
&lt;tt class="docutils literal"&gt;node_modules&lt;/tt&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="overwhelmed-yet"&gt;
&lt;h2&gt;Overwhelmed yet?&lt;/h2&gt;
&lt;p&gt;So we have two different ways to define modules, and two different way
to do packaging. I am simplifying matters here - CommonJS does offer
definitions to transport modules to the client too, and there are ways
to manage client-side packages using npm too, and I'm sure other
package managers exist too.&lt;/p&gt;
&lt;p&gt;You may start to agree with me that this is all somewhat overwhelming,
especially if you're new to this! But we're not there yet...&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="bower-and-requirejs"&gt;
&lt;h2&gt;bower and RequireJS&lt;/h2&gt;
&lt;p&gt;bower is agnostic as to what's in packages and how they're used;
RequireJS is just one possibility. So when I install a package into a
project using bower, RequireJS has no clue that it is even there, let
alone where to import it from. As a result, I cannot import any
modules from that package in my project's modules. I need to tell
RequireJS first.&lt;/p&gt;
&lt;p&gt;So if I install jQuery using bower, I still need to manually tell
RequireJS in its config file how to find it: look in
&lt;tt class="docutils literal"&gt;components/jquery/jquery.js&lt;/tt&gt; for it, please. Only after that I can
depend on it in my module.&lt;/p&gt;
&lt;p&gt;This doesn't &lt;em&gt;Just Work&lt;/em&gt; &amp;#8482;. I want to install something with bower
and start using it right away. We need something to help glue this
together.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="grunt"&gt;
&lt;h2&gt;Grunt&lt;/h2&gt;
&lt;p&gt;To construct complex Python projects I use this system called
&lt;a class="reference external" href="http://www.buildout.org/"&gt;buildout&lt;/a&gt;. It can be used to pull in dependencies, install scripts,
and automate all sorts of other tasks too. Buildout is driven from a
configuration file - it's kind of like &lt;tt class="docutils literal"&gt;Make&lt;/tt&gt; and the &lt;tt class="docutils literal"&gt;Makefile&lt;/tt&gt;
configuration file.&lt;/p&gt;
&lt;p&gt;So JavaScript has some build automation systems too. One popular one
is called &lt;a class="reference external" href="http://gruntjs.com/"&gt;Grunt&lt;/a&gt;. It takes a config file called &lt;tt class="docutils literal"&gt;Gruntfile.js&lt;/tt&gt;. It
can be extended with plugins which you install with npm.&lt;/p&gt;
&lt;p&gt;Grunt is pretty useful to automate jobs such as gluing bower and RequireJS
together.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="grunt-bower-requirejs"&gt;
&lt;h2&gt;grunt-bower-requirejs&lt;/h2&gt;
&lt;p&gt;Using npm, you can install a grunt plugin called
&lt;cite&gt;grunt-bower-requirejs_&lt;/cite&gt;. You configure it up in &lt;tt class="docutils literal"&gt;Gruntfile.js&lt;/tt&gt;. Now
if you run &lt;tt class="docutils literal"&gt;grunt&lt;/tt&gt; it will automatically make any dependencies
installed using bower available for RequireJS. It does this by
manipulating the RequireJS config to tell it where bower-installed
packages are.&lt;/p&gt;
&lt;p&gt;So now (at least after I run &lt;tt class="docutils literal"&gt;grunt&lt;/tt&gt;), I can &lt;tt class="docutils literal"&gt;require()&lt;/tt&gt; whatever
bower-installed packages I like from my own JS code. Awesome!&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="gluing-up-buster"&gt;
&lt;h2&gt;Gluing up Buster&lt;/h2&gt;
&lt;p&gt;We're not all there yet. Remember the test runner I use, Buster? There
is already a &lt;a class="reference external" href="https://github.com/busterjs/buster-amd"&gt;buster-amd&lt;/a&gt; plugin, which is needed to let Buster
behave properly around RequireJS. Making this work did take somewhat
tricky configuration featuring a &lt;tt class="docutils literal"&gt;pathMapper&lt;/tt&gt; and a regex I don't
quite understand, but okay.&lt;/p&gt;
&lt;p&gt;There is also already a &lt;a class="reference external" href="https://github.com/busterjs/grunt-buster"&gt;grunt-buster&lt;/a&gt; plugin. This can automatically
start a &lt;a class="reference external" href="http://phantomjs.org/"&gt;PhantomJS&lt;/a&gt; based web browser to run the tests, and then run
them, if I type &lt;tt class="docutils literal"&gt;grunt test&lt;/tt&gt;. Pretty neat!&lt;/p&gt;
&lt;p&gt;Is this enough to make things &lt;em&gt;Just Work&lt;/em&gt; &amp;#8482;? After all I should be
able to rely on RequireJS declare the dependencies for my test
modules. But no...&lt;/p&gt;
&lt;p&gt;As mentioned before, Buster actually has a special requirement if you
run tests against a web browser: it needs to know what JS resources to
publish to the web browser it runs the tests in, so that they are even
available for RequireJS at all. It is kind of what Fanstatic does,
actually!&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="grunt-bower-busterjs"&gt;
&lt;h2&gt;grunt-bower-busterjs&lt;/h2&gt;
&lt;p&gt;So now Buster needs to know what client-side packages are installed
through Bower too, just like RequireJS.&lt;/p&gt;
&lt;p&gt;Unfortunately there wasn't a grunt plugin for this yet that I could
find. Balazs Ree, my friend from the Zope community who also is doing
lots of stuff with JavaScript, suggested creating something like
grunt-bower-requirejs to create bower integration for buster. Good idea!&lt;/p&gt;
&lt;p&gt;It turned out grunt-bower-requirejs was extremely close to what was
needed already, so I forked it and hacked it up into
&lt;a class="reference external" href="https://github.com/faassen/grunt-bower-busterjs"&gt;grunt-bower-busterjs&lt;/a&gt;. When plugged into grunt, this generates a
&lt;tt class="docutils literal"&gt;bowerbuster.json&lt;/tt&gt; file. Following Balazs' advice I then tweaked
Buster's &lt;tt class="docutils literal"&gt;buster.js&lt;/tt&gt; configuration file to load up
&lt;tt class="docutils literal"&gt;bowerbuster.json&lt;/tt&gt; into the test sources list.&lt;/p&gt;
&lt;p&gt;And then, at last, everything started to &lt;em&gt;Just Work&lt;/em&gt; &amp;#8482;!&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="jspak-a-sample-project"&gt;
&lt;h2&gt;jspak - a sample project&lt;/h2&gt;
&lt;p&gt;There is a good chance you're now overwhelmed as I was. Hopefully I
can help: I've pulled all this together into a sample project called
&lt;a class="reference external" href="https://github.com/faassen/jspak"&gt;jspak&lt;/a&gt;. It integrates bower and buster and grunt and RequireJS and
seems to &lt;em&gt;Just Work&lt;/em&gt; &amp;#8482;.&lt;/p&gt;
&lt;p&gt;I will consult it myself when I start the job of converting Obviel to
use it but perhaps it's useful for others too.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="thoughts"&gt;
&lt;h2&gt;Thoughts&lt;/h2&gt;
&lt;p&gt;Here are a few thoughts concerning all this.&lt;/p&gt;
&lt;p&gt;It &lt;em&gt;would&lt;/em&gt; be nice if the JavaScript world could work out a system
where I don't need 5 or 6 configuration files just to get a project
going where I can install client-side packages and run the tests
(&lt;tt class="docutils literal"&gt;Gruntfile.js&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;bower.json&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;bowerbuster.json&lt;/tt&gt;,
&lt;tt class="docutils literal"&gt;buster.js&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;package.json&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;rjs.js&lt;/tt&gt;). I'm sure glad I got it
working though!&lt;/p&gt;
&lt;p&gt;Maybe such a system already exists; there just &lt;em&gt;might&lt;/em&gt; be a parallel
JavaScript ecosystem out there with yet another way to do packaging
and imports that has &lt;em&gt;Just Worked&lt;/em&gt; &amp;#8482; all the time already. One never
knows with JavaScript!&lt;/p&gt;
&lt;p&gt;The Python packaging world feels a lot more comfortable to me than the
JavaScript one. One obvious reason that doesn't really count is just
because I'm used to Python packaging and am familiar with its quirks.&lt;/p&gt;
&lt;p&gt;Another reason is that JavaScript actually runs in web browsers as
well as on the server, while Python is used on the server only. This
means JavaScript needs to solve problems that Python just doesn't
have. (Though various projects exist that make something like Python
run in the browser too. One wonders how packaging works for them.)&lt;/p&gt;
&lt;p&gt;Finally an important reason is that Python actually has a freaking
built-in &lt;tt class="docutils literal"&gt;import&lt;/tt&gt; statement! People then kind of naturally gravitate
towards using something that is already, instead of creating several
different ways. JavaScript clearly doesn't follow the Zen of Python:
"There should be one-- and preferably only one --obvious way to do
it."  ("Although that way may not be obvious at first unless you're
Dutch."  -- I'm Dutch :) )&lt;/p&gt;
&lt;p&gt;Finally, a funny thing about JavaScript project names: Buster.JS,
Node.js, CommonJS, RequireJS - not being very consistent with the
spelling of the &lt;em&gt;JS&lt;/em&gt; bit, are we? I'm a programmer and I'm trained to
pay attention to irrelevant things like that.&lt;/p&gt;
&lt;/div&gt;
</description><guid>http://blog.startifact.com/posts/overwhelmed-by-javascript-dependencies.html</guid><pubDate>Mon, 13 May 2013 14:50:00 GMT</pubDate></item><item><title>Powerful composition with Obviel data-render</title><link>http://blog.startifact.com/posts/powerful-composition-with-obviel-data-render.html</link><description>&lt;p&gt;&lt;a class="reference external" href="http://obviel.org"&gt;Obviel&lt;/a&gt; is the client side web framework I've been hacking on for the
past few years. It has among many other things a client-side template
language called &lt;a class="reference external" href="http://www.obviel.org/en/1.0b6/template.html"&gt;Obviel Template&lt;/a&gt;. Obviel Template has a bunch of
nice features such as &lt;a class="reference external" href="http://www.obviel.org/en/1.0b6/template_i18n.html"&gt;i18n support&lt;/a&gt;, about which I'll write more
later. But the feature I want to talk about today is the
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-render&lt;/span&gt;&lt;/tt&gt; directive.&lt;/p&gt;
&lt;p&gt;Obviel lets you define views for types of objects, and render them on
elements. See my previous blog entry on the &lt;cite&gt;core of Obviel&lt;/cite&gt; for a
very short introduction to this concept (note: &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-handler&lt;/span&gt;&lt;/tt&gt; has
been renamed to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-on&lt;/span&gt;&lt;/tt&gt;). In web applications your Obviel views
are mostly going to have small templates - each view doing its own
thing: rendering a template for a model, handling user events,
etc. Complex UIs are created by composing views together.&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-render&lt;/span&gt;&lt;/tt&gt; is the tool that lets you do this easily from within a
template. If we have the following model of a todo list:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
{
  iface: 'todo-list',
  todos: [{ iface: 'todo-item', title: 'Item 1' },
          { iface: 'todo-item', title: 'Item 2' }]
}
&lt;/pre&gt;
&lt;p&gt;Then we can create a view to render that &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;todo-list&lt;/span&gt;&lt;/tt&gt; like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
obviel.view({
   iface: 'todo-list',
   obvt: '&amp;lt;ul&amp;gt;&amp;lt;li data-repeat="todos" data-render="@."&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;'
});
&lt;/pre&gt;
&lt;p&gt;What, you may ask, is &lt;tt class="docutils literal"&gt;@.&lt;/tt&gt;? It is Obviel Template's way to express
"the current object", in this case the current item in the &lt;tt class="docutils literal"&gt;todos&lt;/tt&gt;
array that we are looping through using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-repeat&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;And we also need to be able to render an individual &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;todo-item&lt;/span&gt;&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
obviel.view({
   iface: 'todo-item',
   obvt: '&amp;lt;div&amp;gt;{title}&amp;lt;/div&amp;gt;'
});
&lt;/pre&gt;
&lt;p&gt;Some benefits:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;each view is doing one and only one thing&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;if &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;todo-item&lt;/span&gt;&lt;/tt&gt; has an event handler (for instance to handle
&lt;tt class="docutils literal"&gt;click&lt;/tt&gt; events), this will just work:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
obviel.view({
   iface: 'todo-item',
   obvt: '&amp;lt;div data-on="click|clicked"&amp;gt;{title}&amp;lt;/div&amp;gt;',
   clicked: function() {
     alert("You clicked!");
   }
});
&lt;/pre&gt;
&lt;p&gt;[update: fixed bug in code example by renaming 'click' to 'clicked']&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;The &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;todo-list&lt;/span&gt;&lt;/tt&gt; view is agnostic about what individual entries of
the &lt;tt class="docutils literal"&gt;todos&lt;/tt&gt; array actually are. So if you add a
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;special-todo-item&lt;/span&gt;&lt;/tt&gt; to the &lt;tt class="docutils literal"&gt;todos&lt;/tt&gt; array, and provide a view for
it, it will just work too. This is pretty powerful if you are going
to hook up entirely different events in it!&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Have fun with Obviel and see you next time!&lt;/p&gt;
</description><guid>http://blog.startifact.com/posts/powerful-composition-with-obviel-data-render.html</guid><pubDate>Tue, 07 May 2013 12:30:00 GMT</pubDate></item><item><title>Obviel 1.0b6</title><link>http://blog.startifact.com/posts/obviel-10b6.html</link><description>&lt;p&gt;I've just released Obviel 1.0b6!&lt;/p&gt;
&lt;p&gt;There are backwards incompatible changes to Obviel Template in this
release. I figured now was the time to do so, as it's not widely
adopted yet. The general pattern is to use imperative verbs where
possible for directives (though &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-on&lt;/span&gt;&lt;/tt&gt; is an exception):&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-each&lt;/span&gt;&lt;/tt&gt; was renamed to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-repeat&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;@each&lt;/tt&gt; to
&lt;tt class="docutils literal"&gt;@repeat&lt;/tt&gt;.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-view&lt;/span&gt;&lt;/tt&gt; becomes &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-render&lt;/span&gt;&lt;/tt&gt;, as it really calls Obviel's
&lt;tt class="docutils literal"&gt;render()&lt;/tt&gt; function to render an object.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-func&lt;/span&gt;&lt;/tt&gt; becomes &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-call&lt;/span&gt;&lt;/tt&gt;, as it really calls a function on
the view.&lt;/li&gt;
&lt;li&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-handler&lt;/span&gt;&lt;/tt&gt; becomes &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;data-on&lt;/span&gt;&lt;/tt&gt; as its shorter and &lt;tt class="docutils literal"&gt;on&lt;/tt&gt; typically
implies event handling.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A simple search and replace in your templates for these names should
be enough to update your templates.&lt;/p&gt;
&lt;p&gt;There is also a bugfix in Obviel Template that was contributed by
Daniel Havlik, thank you!&lt;/p&gt;
&lt;p&gt;You can find out more about Obviel here:&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="http://www.obviel.org/en/1.0b6/"&gt;http://www.obviel.org/en/1.0b6/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;and here is the complete changelog:&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="http://www.obviel.org/en/1.0b6/CHANGES.html"&gt;http://www.obviel.org/en/1.0b6/CHANGES.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Have fun and let me know what you think of it!&lt;/p&gt;
</description><guid>http://blog.startifact.com/posts/obviel-10b6.html</guid><pubDate>Thu, 04 Apr 2013 16:15:00 GMT</pubDate></item><item><title>Looking for project!</title><link>http://blog.startifact.com/posts/looking-for-project.html</link><description>&lt;p&gt;I'm looking for a project to inspire me! I'm a creative developer with
15 years experience doing Python and the web. So much experience doing
web applications does not mean I'm stuck in the past; I'm an accomplished
client-side JavaScript developer too (who created a &lt;a class="reference external" href="http://obviel.org"&gt;client-side web
framework&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;You can check out &lt;a class="reference external" href="http://startifact.com"&gt;http://startifact.com&lt;/a&gt; for an overview of some of the
stuff I've done.&lt;/p&gt;
&lt;p&gt;If you're interested in hiring me to help you improve your project or
create something new, please contact me at &lt;a class="reference external" href="mailto:faassen@startifact.com"&gt;faassen@startifact.com&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I hope to hear from you!&lt;/p&gt;
</description><guid>http://blog.startifact.com/posts/looking-for-project.html</guid><pubDate>Tue, 26 Feb 2013 16:15:08 GMT</pubDate></item><item><title>The Story of None: Part 6 - Avoiding It</title><link>http://blog.startifact.com/posts/none_06_avoiding_it.html</link><description>&lt;p&gt;&lt;a class="reference external" href="http://blog.startifact.com/posts/none_01_the_beginning.html"&gt;part 1&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_02_recognizing.html"&gt;part 2&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_03_handling_it.html"&gt;part 3&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_04_guard_clauses.html"&gt;part 4&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_05_more_on_guarding.html"&gt;part 5&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_06_avoiding_it.html"&gt;part 6&lt;/a&gt;&lt;/p&gt;
&lt;div class="section" id="last-time"&gt;
&lt;h2&gt;Last time...&lt;/h2&gt;
&lt;p&gt;&lt;a class="reference external" href="http://blog.startifact.com/posts/none_05_more_on_guarding.html"&gt;Last time&lt;/a&gt; we've discussed guard clauses and when not to use
them. We've discussed the paranoia developers sometimes feel that
causes them to write useless or even harmful guard clauses. The best
way to reduce paranoia about &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; is to make sure it can't be
there in the first place.&lt;/p&gt;
&lt;p&gt;So let's talk about ways to accomplish this.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="date-validator-redux"&gt;
&lt;h2&gt;Date Validator Redux&lt;/h2&gt;
&lt;p&gt;The date validator in its last incarnation looked like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
def validate_end_date_later_than_start(start_date, end_date):
    if start_date is None or end_date is None:
        return
    if end_date &amp;lt;= start_date:
        raise ValidationError(
            "The end date should be later than the start date.")
&lt;/pre&gt;
&lt;p&gt;Here we want to validate two date values which may be missing, in
which case we treat &lt;tt class="docutils literal"&gt;start_date&lt;/tt&gt; as "indefinite past" and
&lt;tt class="docutils literal"&gt;end_date&lt;/tt&gt; as "indefinite future".&lt;/p&gt;
&lt;p&gt;We could create special sentinel objects for "indefinite future" and
"indefinite past":&lt;/p&gt;
&lt;pre class="literal-block"&gt;
INDEFINITE_PAST = date(datetime.MINYEAR, 1, 1)
INDEFINITE_FUTURE = date(datetime.MAXYEAR, 12, 31)
&lt;/pre&gt;
&lt;p&gt;where &lt;tt class="docutils literal"&gt;MINYEAR&lt;/tt&gt; is &lt;tt class="docutils literal"&gt;1&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;MAXYEAR&lt;/tt&gt; is &lt;tt class="docutils literal"&gt;9999&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;(Too bad datetime doesn't allow negative dates, or we could've used
&lt;a class="reference external" href="http://en.wikipedia.org/wiki/Ussher_chronology"&gt;Bishop Ussher's date&lt;/a&gt; for the creation of the universe,
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;date(-4004,&lt;/span&gt; 10, 23)&lt;/tt&gt;. Though that's in the proleptic Julian
calendar, and I don't care to know what that is right now. Plus it's
bogus. But it'd be amusing.)&lt;/p&gt;
&lt;p&gt;If we can be sure that those are used instead of &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; before the
&lt;tt class="docutils literal"&gt;validate_end_date_later_than_start&lt;/tt&gt; function is called, we can
simplify it to this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
def validate_end_date_later_than_start(start_date, end_date):
    if end_date &amp;lt;= start_date:
        raise ValidationError(
            "The end date should be later than the start date.")
&lt;/pre&gt;
&lt;p&gt;which is what we started out with in the first place in &lt;a class="reference external" href="http://blog.startifact.com/posts/none_01_the_beginning.html"&gt;Part 1&lt;/a&gt; long
ago, without any guards! Awesome!&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="edge-case"&gt;
&lt;h2&gt;Edge case&lt;/h2&gt;
&lt;p&gt;This handwaves the edge case where &lt;tt class="docutils literal"&gt;start_date&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;end_date&lt;/tt&gt; are
both equal to &lt;tt class="docutils literal"&gt;INDEFINITE_PAST&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;INDEFINITE_FUTURE&lt;/tt&gt;, which can
be argued should not raise &lt;tt class="docutils literal"&gt;ValidationError&lt;/tt&gt;. In software for a time
machine it might be important to get this right, but in many
applications not handling this edge case is fine.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="really-avoiding-the-edge-cases"&gt;
&lt;h2&gt;Really avoiding the edge cases&lt;/h2&gt;
&lt;p&gt;If we insist on making the edge case go away, we could deal with it by
subclassing the &lt;tt class="docutils literal"&gt;date&lt;/tt&gt; class to construct these sentinels instead:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
class IndefinitePast(date):
    def __lt__(self, other):
        return True

    def __le__(self, other):
        return True

    def __gt__(self, other):
        return False

    def __ge__(self, other):
        return False

class IndefiniteFuture(date):
    def __lt__(self, other):
        return False

    def __le__(self, other):
        return False

    def __gt__(self, other):
        return True

    def __ge__(self, other):
        return True

INDEFINITE_PAST = IndefinitePast(datetime.MINYEAR, 1, 1)
INDEFINITE_FUTURE = IndefiniteFuture(datetime.MAXYEAR, 12, 31)
&lt;/pre&gt;
&lt;p&gt;This is a lot more code though, and therefore in many situations this
would be overkill.&lt;/p&gt;
&lt;p&gt;(As a puzzle for the reader in this case one could safely skip
implementing &lt;tt class="docutils literal"&gt;__le__&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;__ge__&lt;/tt&gt; for these classes and still
have it all work for any possible date. I kept them in for clarity.)&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="normalization"&gt;
&lt;h2&gt;Normalization&lt;/h2&gt;
&lt;p&gt;So what have we done here? We've made sure that our input was
&lt;a class="reference external" href="https://en.wikipedia.org/wiki/Data_normalization"&gt;normalized&lt;/a&gt; to a date before it even reached the validation
function. This way we don't have to worry about our friend &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; when
we deal with dates.&lt;/p&gt;
&lt;p&gt;The idea is to normalize the input a soon as possible before it
reaches the rest of our codebase, so we can stop worrying about
non-normalized cases (such as &lt;tt class="docutils literal"&gt;None&lt;/tt&gt;) everywhere else. In effect you
put the guard clauses as far on the outside of the calling chain as
possible.&lt;/p&gt;
&lt;p&gt;In the case of our date input, somewhere in the input processing we'd
call these functions:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
def process_start_date(d):
     if d is None:
         return INDEFINITE_PAST
     return d

def process_end_date(d):
     if d is None:
         return INDEFINITE_FUTURE
     return d
&lt;/pre&gt;
&lt;p&gt;None of those &lt;tt class="docutils literal"&gt;None&lt;/tt&gt;'s to worry about anymore after that!&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="drawbacks"&gt;
&lt;h2&gt;Drawbacks&lt;/h2&gt;
&lt;p&gt;Normalization also has some potential drawbacks. Here are some that
may apply to this case:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;to understand how empty date fields are treated in the validation
function, we need to read normalization code that may be somewhere
else. Our validation function that worried about None was all in one
place.&lt;/li&gt;
&lt;li&gt;it's more code to understand and maintain, especially with the
custom date subclasses.&lt;/li&gt;
&lt;li&gt;normalization of &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; to a date may be nice during validation,
but it may not be what we want to store in a database; we might want
to store None there. If we have this requirement we'd need two code
paths: one for storage and one for validation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It all depends on the exact details of your project. If the project is
going to compare a lot of dates in many places, it makes sense to
normalize missing values to proper dates as soon as possible, and it's
a much better approach than having to worry about &lt;tt class="docutils literal"&gt;None&lt;/tt&gt;
everywhere. But if the project only needs a single validation rule
that can handle missing dates, then it makes more sense to write one
that deals with &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; directly.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="conclusion"&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;This concludes the Story of None! I hope you've enjoyed it! Perhaps
you've learned something.&lt;/p&gt;
&lt;p&gt;Let me know if you would like to see more stuff like this -
discussions of fairly low-level patterns that happen during
development.&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="http://blog.startifact.com/posts/none_01_the_beginning.html"&gt;part 1&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_02_recognizing.html"&gt;part 2&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_03_handling_it.html"&gt;part 3&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_04_guard_clauses.html"&gt;part 4&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_05_more_on_guarding.html"&gt;part 5&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_06_avoiding_it.html"&gt;part 6&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
</description><guid>http://blog.startifact.com/posts/none_06_avoiding_it.html</guid><pubDate>Wed, 06 Feb 2013 11:30:00 GMT</pubDate></item><item><title>The Story of None: Part 5 - More on Guarding</title><link>http://blog.startifact.com/posts/none_05_more_on_guarding.html</link><description>&lt;p&gt;&lt;a class="reference external" href="http://blog.startifact.com/posts/none_01_the_beginning.html"&gt;part 1&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_02_recognizing.html"&gt;part 2&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_03_handling_it.html"&gt;part 3&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_04_guard_clauses.html"&gt;part 4&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_05_more_on_guarding.html"&gt;part 5&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_06_avoiding_it.html"&gt;part 6&lt;/a&gt;&lt;/p&gt;
&lt;div class="section" id="last-time"&gt;
&lt;h2&gt;Last time...&lt;/h2&gt;
&lt;p&gt;&lt;a class="reference external" href="http://blog.startifact.com/posts/none_04_guard_clauses.html"&gt;Last time&lt;/a&gt; in the Story of None we've discussed the concept of a
&lt;em&gt;guard clause&lt;/em&gt;. This is simply an &lt;tt class="docutils literal"&gt;if&lt;/tt&gt; statement at the beginning of
a function that returns early if a certain condition is true.&lt;/p&gt;
&lt;p&gt;The guard clause pattern is applicable to more than just the &lt;tt class="docutils literal"&gt;None&lt;/tt&gt;
scenario. We could be writing a function where we need a specific
treatment if an integer value is &lt;tt class="docutils literal"&gt;0&lt;/tt&gt;, for instance. A guard clause
often does more than just a bare boned &lt;tt class="docutils literal"&gt;return&lt;/tt&gt;. Instead, it could
return a value or raise an exception.&lt;/p&gt;
&lt;p&gt;So let's discuss some other brief examples of guard clauses.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="raising-exceptions"&gt;
&lt;h2&gt;Raising exceptions&lt;/h2&gt;
&lt;p&gt;Raising an exception is good when the input really should be
considered an error and the developer should know about it:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
def divide(a, b):
    if b == 0:
       raise ZeroDivisionError("Really I don't know, man!")
    return a / b
&lt;/pre&gt;
&lt;p&gt;In this case you know that the way to handle &lt;tt class="docutils literal"&gt;b&lt;/tt&gt; being &lt;tt class="docutils literal"&gt;0&lt;/tt&gt; is to
&lt;em&gt;not&lt;/em&gt; handle it and instead to loudly complain that there is some
error, by raising an exception.&lt;/p&gt;
&lt;p&gt;Complaining loudly is important: it is tempting to make up some return
value and let the code proceed. We'll go into this in more detail
later on.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="dictionary-get-guard-clause"&gt;
&lt;h2&gt;Dictionary &lt;tt class="docutils literal"&gt;.get&lt;/tt&gt; guard clause&lt;/h2&gt;
&lt;p&gt;Let's say we want to implement dictionary &lt;tt class="docutils literal"&gt;.get&lt;/tt&gt; as a function
ourselves, with a guard against a missing dictionary key:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
def get(d, key, default=None):
    if key not in d:
        return default
    return d[key]
&lt;/pre&gt;
&lt;p&gt;This guard clause returns a default value if the guard clause
condition is true. As you can see here the guard clause can be
dependent on multiple arguments, in this case &lt;tt class="docutils literal"&gt;d&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;key&lt;/tt&gt;; if
the function in question is a method, it can be dependent on object
state as well.&lt;/p&gt;
&lt;p&gt;Complain loudly for possible input that you cannot handle; it makes
debugging easier.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="guard-clauses-in-recursion"&gt;
&lt;h2&gt;Guard clauses in recursion&lt;/h2&gt;
&lt;p&gt;Guard clauses often occur in &lt;a class="reference external" href="https://en.wikipedia.org/wiki/Recursion_%28computer_science%29"&gt;recursive&lt;/a&gt; functions, where they guard
against the &lt;em&gt;stopping criterion&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Let's consider this rather contrived (as there are much better
implementations without recursion in Python) example where we
recursively add all numbers in a list:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
def add(numbers):
    if len(numbers) == 0:
        return 0
    return numbers[0] + add(numbers[1:])
&lt;/pre&gt;
&lt;p&gt;The main part of the function says: the sum of all the numbers in a
list &lt;tt class="docutils literal"&gt;numbers&lt;/tt&gt; is the first entry in that list added to the sum of
the rest of the entries in the list.&lt;/p&gt;
&lt;p&gt;But what if the list of numbers is empty? We cannot obtain the first
entry in that case, so we need some kind of guard clause to handle
this.  We know that the resulting sum of an empty list should be
&lt;tt class="docutils literal"&gt;0&lt;/tt&gt;, so we can simply return this. This is also the stopping
criterion for the recursion, because we cannot recurse further into an
empty list.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="don-t-be-paranoid"&gt;
&lt;h2&gt;Don't be paranoid&lt;/h2&gt;
&lt;p&gt;Don't overdo it, and put in guard clauses that guard against cases
where you don't actually &lt;em&gt;know&lt;/em&gt; they can happen, or where you don't
know how to &lt;em&gt;handle&lt;/em&gt; them. Guard against &lt;em&gt;exceptional&lt;/em&gt; forms of
&lt;em&gt;expected&lt;/em&gt; input, not against input that is &lt;em&gt;unexpected
altogether&lt;/em&gt;. Guarding against the expected is sensible, but guarding
against the unexpected is paranoia.&lt;/p&gt;
&lt;p&gt;So, in the case of &lt;tt class="docutils literal"&gt;None&lt;/tt&gt;, we don't want to clutter our code with
lots of guard clauses just to make sure that the input wasn't &lt;tt class="docutils literal"&gt;None&lt;/tt&gt;
if we don't even know that the input can be &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; in the first
place.&lt;/p&gt;
&lt;p&gt;Python tends to do the right thing in the face of the unexpected: its
core operations tends to fail early with an exception if asked to do
something which they cannot handle: dividing by zero, comparing a date
with None, getting a non-existent key from a dictionary. Rely on this
behavior, be happy Python is eager to tell you something is wrong, and
avoid clutter in your code.&lt;/p&gt;
&lt;p&gt;Other languages like JavaScript instead of failing sometimes continue
even in the face of the unexpected: they let you add a string to a
number, and if a property is missing you don't get an exception but a
value &lt;tt class="docutils literal"&gt;undefined&lt;/tt&gt;. This makes JavaScript in my experience harder to
debug than Python. But I still don't clutter my JavaScript code with
all kinds of paranoid guard clauses, because I &lt;em&gt;still&lt;/em&gt; don't expect
these cases, and I don't want to clutter up my code.&lt;/p&gt;
&lt;p&gt;In statically typed programming languages such as Java you have to
specify exactly what type of input arguments to expect, and the system
will fail loudly if you do something that isn't expected. Languages
like that are a bit paranoid by nature and you'll have to follow their
rules. What they do in the case of failure is correct: fail loudly as
early as possible. In dynamically typed languages such as Python or
JavaScript you don't specify types for the sake of less cluttered,
more generic code.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="if-you-feel-paranoid"&gt;
&lt;h2&gt;If you feel paranoid&lt;/h2&gt;
&lt;p&gt;We all feel paranoid sometimes. Sometimes we think we need to handle
some type of input. If you feel inclined to handle something but
aren't sure what to do, here is a list of things to consider:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;Don't return from the function early. This is the worst thing you
can do.&lt;/p&gt;
&lt;p&gt;If you handle an unexpected value by returning something you make
up, you really are creating a bug. Made-up data is now propagating
further through the codebase. You either end up with an exception
deeper down in call chain where it becomes harder to debug, or you
end up with something worse: a seemingly functional program which
delivers bogus data.&lt;/p&gt;
&lt;p&gt;You may think you can avoid returning something made-up by using a
plain &lt;tt class="docutils literal"&gt;return&lt;/tt&gt; statement. But if you do that in case of a function
that really needs to return a result, you are implicitly returning
&lt;tt class="docutils literal"&gt;None&lt;/tt&gt; (in Python) or &lt;tt class="docutils literal"&gt;undefined&lt;/tt&gt; (in JavaScript). This is the
same as returning made-up value: this value will likely be used
later on, and you'll either get a harder to debug error later on, or
bogus results.&lt;/p&gt;
&lt;p&gt;It might seem like we did such a plain return in case of the date
validation function; we got a &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; and we handled it by
returning early.&lt;/p&gt;
&lt;p&gt;But in the case of the date validation function, &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; was
according to our requirements &lt;em&gt;expected&lt;/em&gt; input, just an
&lt;em&gt;exceptional&lt;/em&gt; case where the normal case was date input. And a
validation function like this has no return value at all, and can
stop validating right away as soon as the input is judged valid, so
returning early is fine.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Don't overuse &lt;tt class="docutils literal"&gt;print&lt;/tt&gt; statements.&lt;/p&gt;
&lt;p&gt;You could use a print statement to print out arguments, so you can
see whether they are unexpected by reading the output.&lt;/p&gt;
&lt;p&gt;Using &lt;tt class="docutils literal"&gt;print&lt;/tt&gt; is a totally legitimate debugging strategy, but make
sure the print is removed from your code after you're done
debugging!&lt;/p&gt;
&lt;p&gt;You don't want to clutter up your code with a lot of print
statements. You'll get a &lt;em&gt;lot&lt;/em&gt; of output that will be hard to
understand.&lt;/p&gt;
&lt;p&gt;While print is quick and appropriate sometimes, do consider learning
a debugger tool for your language as it can help a lot for more
complex cases. For Python the built-in debugger is &lt;tt class="docutils literal"&gt;pdb&lt;/tt&gt; for
instance.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Don't log everything.&lt;/p&gt;
&lt;p&gt;Logging for debugging purposes is the advanced form of using
&lt;tt class="docutils literal"&gt;print&lt;/tt&gt; statements. At least it doesn't clutter the standard
output. But logging is still clutter in the code. And if you fill
log files with a lot of debugging information, it will still be hard
to find out whether, if anything, is really wrong.&lt;/p&gt;
&lt;p&gt;Logging is very useful, but I prefer logging to be
application-appropriate, not to help debug the program flow. If I
want to debug program flow I use a debugger or a bunch of throw-away
print statements.&lt;/p&gt;
&lt;p&gt;Of course there are exceptions to this rule; you might for instance
want to log debugging information if a bug is hard to find and turns
up in production. But use it in moderation, only when necessary.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Don't print or log and &lt;em&gt;then&lt;/em&gt; return early with a made up value.&lt;/p&gt;
&lt;p&gt;You can print or log some diagnostic information (the value of
arguments, say) and then return early with a made-up value.&lt;/p&gt;
&lt;p&gt;The impulse to want to get diagnostics is correct. The impulse to
stop going further is &lt;em&gt;also&lt;/em&gt; correct. But returning with a made-up
value is still &lt;em&gt;wrong&lt;/em&gt; -- and you aren't really stopping your
program anyway.&lt;/p&gt;
&lt;p&gt;If you return with a made-up value, your program will continue and
will likely log some more information. But since you've returned a
made-up value, everything logged after this case is nonsense;
there's no point in continuing.&lt;/p&gt;
&lt;p&gt;You could instead print diagnostics and do an early exit
(&lt;tt class="docutils literal"&gt;sys.exit()&lt;/tt&gt; in Python). Instead, if your language lets you do
it, just throw an exception.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Throw an exception.&lt;/p&gt;
&lt;p&gt;If you feel you need to handle an unexpected case, throw an exception.
In Python you can throw a basic exception like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
if value == 0:
  raise Exception("Something went wrong")
&lt;/pre&gt;
&lt;p&gt;But of course it makes sense to use more specific exceptions when you
can:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
if not isinstance(value, basestring):
   raise TypeError("Expected string, not %r" % value)
&lt;/pre&gt;
&lt;p&gt;Sometimes you need to make up new exceptions specific to your library or
application as none of the built-in ones is appropriate:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
class WorkflowError(Exception):
  pass

...

if invalid(workflow_state):
   raise WorkflowError("Invalid workflow state: %s" % workflow_state)
&lt;/pre&gt;
&lt;p&gt;Exceptions do the right stuff automatically:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;bail out early when you can't handle something.&lt;/li&gt;
&lt;li&gt;give diagnostic information in the form of a message and a
traceback of the function call chain.&lt;/li&gt;
&lt;li&gt;allow you to handle them after all if you want.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p class="first"&gt;Do nothing.&lt;/p&gt;
&lt;p&gt;Doing nothing is often the right impulse.&lt;/p&gt;
&lt;p&gt;In the case of unexpected input, I can often rely on the language to
fail with an exception anyway in the appropriate spot.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a class="reference external" href="http://blog.startifact.com/posts/none_06_avoiding_it.html"&gt;Next time&lt;/a&gt; we'll consider a way to avoid having to scatter guard clauses
throughout our codebase: normalization.&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="http://blog.startifact.com/posts/none_01_the_beginning.html"&gt;part 1&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_02_recognizing.html"&gt;part 2&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_03_handling_it.html"&gt;part 3&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_04_guard_clauses.html"&gt;part 4&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_05_more_on_guarding.html"&gt;part 5&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_06_avoiding_it.html"&gt;part 6&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
</description><guid>http://blog.startifact.com/posts/none_05_more_on_guarding.html</guid><pubDate>Mon, 04 Feb 2013 17:45:00 GMT</pubDate></item><item><title>The Story of None: Part 4 - Guard Clauses</title><link>http://blog.startifact.com/posts/none_04_guard_clauses.html</link><description>&lt;p&gt;&lt;a class="reference external" href="http://blog.startifact.com/posts/none_01_the_beginning.html"&gt;part 1&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_02_recognizing.html"&gt;part 2&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_03_handling_it.html"&gt;part 3&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_04_guard_clauses.html"&gt;part 4&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_05_more_on_guarding.html"&gt;part 5&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_06_avoiding_it.html"&gt;part 6&lt;/a&gt;&lt;/p&gt;
&lt;div class="section" id="last-time"&gt;
&lt;h2&gt;Last time...&lt;/h2&gt;
&lt;p&gt;&lt;a class="reference external" href="http://blog.startifact.com/posts/none_03_handling_it"&gt;Last time&lt;/a&gt; in the Story of None we ended up with this validation
function:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
def validate_end_date_later_than_start(start_date, end_date):
    if start_date is None and end_date is None:
        return
    if start_date is None:
        return
    if end_date is None:
        return
    if end_date &amp;lt;= start_date:
        raise ValidationError(
            "The end date should be later than the start date.")
&lt;/pre&gt;
&lt;p&gt;So let's apply some boolean logic and rewrite all this to be slightly
shorter (but still readable):&lt;/p&gt;
&lt;pre class="literal-block"&gt;
def validate_end_date_later_than_start(start_date, end_date):
    if start_date is None or end_date is None:
        return
    if end_date &amp;lt;= start_date:
        raise ValidationError(
            "The end date should be later than the start date.")
&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="section" id="guard-clauses"&gt;
&lt;h2&gt;Guard Clauses&lt;/h2&gt;
&lt;p&gt;What we've written above with the &lt;tt class="docutils literal"&gt;if ... return&lt;/tt&gt; statement is a
&lt;a class="reference external" href="http://c2.com/cgi/wiki?GuardClaus"&gt;guard clause&lt;/a&gt;: a check at the beginning of a function that returns
early from the function if the condition is true.&lt;/p&gt;
&lt;p&gt;Instead we could have written the function without guard clauses, like
this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
def validate_end_date_later_than_start(start_date, end_date):
    if start_date is not None and end_date is not None:
        if end_date &amp;lt;= start_date:
           raise ValidationError(
              "The end date should be later than the start date.")
&lt;/pre&gt;
&lt;p&gt;or like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
def validate_end_date_later_than_start(start_date, end_date):
    if (start_date is not None and
        end_date is not None and
        end_date &amp;lt;= start_date):
           raise ValidationError(
              "The end date should be later than the start date.")
&lt;/pre&gt;
&lt;p&gt;I think both alternatives are a lot less clear than the one with the
guard clause in it.&lt;/p&gt;
&lt;p&gt;With a function it's better if the main thing that it is doing, in
this case comparing &lt;tt class="docutils literal"&gt;end_date&lt;/tt&gt; with &lt;tt class="docutils literal"&gt;start_date&lt;/tt&gt;, is also the main
unindented block. This makes the function easier to read. If you make
the main behavior of the function be inside some other &lt;tt class="docutils literal"&gt;if&lt;/tt&gt; clause,
it becomes slightly harder to read.&lt;/p&gt;
&lt;p&gt;The second alternative doesn't create this second level of
indentation, but still makes the comparison between start and end date
rather hidden among the &lt;tt class="docutils literal"&gt;is not None&lt;/tt&gt; clauses. This makes the
expression a bit harder to think about compared to the version of the
function that uses the guard clause.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="guard-and-stop-worrying"&gt;
&lt;h2&gt;Guard and Stop Worrying&lt;/h2&gt;
&lt;p&gt;The great advantage of a guard clause is that you can &lt;em&gt;forget&lt;/em&gt; about
the stuff you're guarding against in the code you write after the
guard clause. So once we have passed the guards, we can entirely stop
worrying about &lt;tt class="docutils literal"&gt;start_date&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;end_date&lt;/tt&gt; being &lt;tt class="docutils literal"&gt;None&lt;/tt&gt;; we've
already guarded against those cases and handled them.&lt;/p&gt;
&lt;p&gt;This is great! We can compare &lt;tt class="docutils literal"&gt;start_date&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;end_date&lt;/tt&gt; freely.
We can call methods on &lt;tt class="docutils literal"&gt;start_date&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;end_date&lt;/tt&gt; safely. We can
pass &lt;tt class="docutils literal"&gt;start_date&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;end_date&lt;/tt&gt; to other functions and &lt;em&gt;they&lt;/em&gt;
don't need to worry about them being &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; either, unless of course
such functions can be called from somewhere else where it could be
&lt;tt class="docutils literal"&gt;None&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;We worry about &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; so we can &lt;em&gt;stop&lt;/em&gt; worrying about &lt;tt class="docutils literal"&gt;None&lt;/tt&gt; when
it matters. Ah, such a relief! Thank you guard clauses!&lt;/p&gt;
&lt;p&gt;We'll talk a bit more about guard clauses &lt;a class="reference external" href="http://blog.startifact.com/posts/none_05_more_on_guarding.html"&gt;next&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="http://blog.startifact.com/posts/none_01_the_beginning.html"&gt;part 1&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_02_recognizing.html"&gt;part 2&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_03_handling_it.html"&gt;part 3&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_04_guard_clauses.html"&gt;part 4&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_05_more_on_guarding.html"&gt;part 5&lt;/a&gt; &lt;a class="reference external" href="http://blog.startifact.com/posts/none_06_avoiding_it.html"&gt;part 6&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
</description><guid>http://blog.startifact.com/posts/none_04_guard_clauses.html</guid><pubDate>Fri, 01 Feb 2013 17:00:00 GMT</pubDate></item></channel></rss>