Skip to content

Instantly share code, notes, and snippets.

Today I had a brief debate on twitter about AMD vs CommonJS, https://twitter.com/TechWraith/status/441387541778808832. It's a debate worth having for sure. But I had to bail out of this one. The thing that annoyed me is that the first argument that people bring up to disquality AMD is "the syntax is too complex". I disagree with this. There are lots of reasons to prefer CommonJS over AMD, but the module authoring syntax is not a very good one. There was also a related statement that AMD authoring introduces more "cognitive overhead". This is absolutely true. But I don't consider this to be synonymous with "complexity" by any means.

So I thought I'd explore some comparable examples. Here's a simple one that was offered up by someone else in the thread. This was on twitter so I can forgive erring on the side of brevity.

AMD

define('myThing', ['some', 'deps'], function (some, deps) {
  //my code
  
 return myThing;
@polotek
polotek / gist:9985338
Created April 4, 2014 23:52
Single page apps and "fast switching"
"broad question: how does fast load work? site is a one page app, switches categories similarly, but no URL relation."
https://twitter.com/drewdil/status/452228299150721024
def format_number(num):
numstr = str(num)
chunks = []
while numstr:
numstr, newchunk = numstr[:-3], numstr[-3:]
chunks.append(newchunk)
return ','.join(chunks[::-1])
"""
various python utils just for my reference
"""
# convert string to datetime
import time
from datetime import datetime
day = datetime.fromtimestamp(time.mktime(time.strptime(d, '%B %d, %Y')))
var sys = require('sys');
var XML = require('libxmljs');
var doc = null;
function mem() {
sys.debug(sys.inspect(process.memoryUsage()).replace(/\n/g, ''));
sys.debug(sys.inspect(XML.memoryUsage()));
}
/**
* simple.tal file
<?xml version="1.0" encoding="UTF-8"?>
<div xmlns:tal="http://axiomstack.com/tale">
<p id="testing" tal:omit="true">
Hello <span tal:content="data.name"/>
You have just won $<span tal:content="data.value"/>!
<span tal:if="data.in_ca">
Well, $<span tal:content="data.taxed_value"/>, after taxes.
/*
* Simple example of using fs methods to append to a file
*/
var sys = require('sys'),
fs = require('fs');
fs.open('my.log', 'a', undefined, function(err, fd) {
if(err) throw err;
fs.write(fd, 'Appending to this file\n', undefined, undefined, function(err, written) {
npm configfile /Users/polotek/.npmrc
npm sudo false
npm install pkg npm
npm fetch data npm
npm response
npm install pkg npm
npm install pkg npm@0.1.4
npm install pkg http://github.com/isaacs/npm/tarball/v0.1.4
npm fetch http://github.com/isaacs/npm/tarball/v0.1.4 to /Users/polotek/.node_libraries/.npm/.tmp/http---github-com-isaacs-npm-tarball-v0-1-4-1273332589145-0.8430941854603589.tgz
npm fetch http://github.com/isaacs/npm/tarball/v0.1.4