Archive for the 'Code' Category

The Death of JavaScript

December 24, 2009

Jash Kenas released CoffeeScript today, a programming language that compiles to human-readable JavaScript.

It addresses a real need, exemplified by this image:

JavaScript was designed quickly and extended by dumb committees. It’s a messy language with a potent core. CoffeeScript aims give programmers the core without the mess. It’s nice. It’s broken in many of the same ways as JavaScript, because it’s just syntactic sugar on JavaScript, and JavaScript’s problems run deeper than syntax. But it can grow to fix these.

CoffeeScript and languages like it are the reason why JavaScript is going to die. Steve Yegge thinks JavaScript is the next big language, poised to take over the programming world. I disagree.

CoffeeScript is a baby in the growing family of languages that compile to JavaScript. These include Scheme, Hop, Java (via GWT), Smalltalk (via Clamato) and Objective-J. These languages are good. A programming language is a programmer’s main tool—and is thus the most rewarding place for improvement.

Imagine you can quantify the productivity of programmer using JavaScript. Over time, everything’s getting better: Libraries, JavaScript VMs, tools like Google Closure. JavaScript programmers are getting better at writing more better faster code as time goes on. Hurray!

There is another productivity curve. This is the curve of compiles-to-JavaScript language users. In this world, you can increase your productivity by using better languages.

Here’s the question: Does the compiles-to-JavaScript curve intersect the raw JavaScript curve anytime soon? If not, Steve Yegge is right. JavaScript wins as long as web browsers are the dominant application platform. But if it does… if it has happened already, even… then JavaScript as a human-use language will die, relegated to being a target language for compilers, like assembly code.

Here’s a brainstorm of some implications.

  • The ECMAScript committee should stop making up features to make JavaScript easier on programmers, and start changing features to make JavaScript more of a good target language.
  • The V8, SpiderMonkey, Carakan and other JavaScript implementations should start benchmarking on workloads that come out of code generators. (Actually, V8 already does this. They should do it more.)
  • The folks working on server-side JavaScript frameworks should just stop and go lie down, for the love of god.
  • Finally, future HTML standards should make allowances for JavaScript to be replaced. Something along the lines of <script language="CoffeeScript" compiler="coffee-script.js">code</script> could mean: Run code if this browser understands CoffeeScript natively. Otherwise, load coffee-script.js, find the compile function inside, call compile(code), and eval the result as JavaScript. I say this not as a proposal of how the forward-compatibility should work (I can think of at least one other way) but to make the point that we should be talking about it.

Units 0.03 is out

October 31, 2009

Units 0.03 is on PyPI. It includes a bug fix (A litre is a cubic decimetre, not a cubic centimetre!). It also has patches for PyPy that let you run a Python interpreter with built-in support for quantity literals. With these patches you can type 239 cm * cm * cm + 0.502 L and it will do what you expect.

Improve your Java: Restrict public methods to interface implementations

October 29, 2009

Good programming is all about restriction and constraint. Most possible programs do nothing interesting. Type systems, unit tests, code reviews and all that jazz are just ways of throwing out uninteresting programs.

Here is a chart view.

There are many more broken programs that good ones.

Notice the thickness of the blue slice.

This realization is what makes me feel comfortable proposing that we, as programmers, should mostly be thinking about new ways we can restrict ourselves.

Here is one way you can restrict your Java (or other classical Object-Oriented) programs to make them better.

Aran’s Public Interface Implementation Rule: All public methods on a class should be implementations of some explicit interface.

See, Java is tricky, because interfaces are interfaces, but so are classes. When you expose public methods on a class that are not part of an interface, a user of the method must specify the class name on the left hand side:

YourClassThatsAlsoAnInterface p = new YourClassThatsAlsoAnInterface();

That means their code is now glued to your implementation. They might not like that.

It would be better if you let them code to an interface and substitute implementations as desired. Thus, don’t bother putting public methods on a class that aren’t implementations of an interface.

YourInterface p = new yourClassThatsJustAClass();
YourInterface p2 = new theirReimplementationOfYourClassThatsJustAClass();

Incidentally, this rule totally enables Joshua Bloch’s Effective Java, Item 34: Refer to objects by their interfaces.

As Zak points out, the tradeoff is that you may hurt some compiler optimizations. If you have benchmarks to prove that following this rule causes unacceptable performance degradation, you have my permission to ignore it. Thanks for asking.

Leave your thoughts in the comments.

A Hack for “Unknown or expired link”

October 28, 2009

You’re using a web site, you fill in a big long form, and, upon submitting, you get “Unknown or expired link.” Why does this happen? The short[1] explanation is that the web site gave you a hidden code to submit with your form, and while you were typing, your code expired. If you hit “Refresh” to get a new code, your browser will probably throw away the contents of your form. You’re lucky you can hit “Back” from the “Unknown or expired link” page.

What to do?

Choice number 1: Copy out your form contents, refresh the page, and paste the contents back in. Slow, boring and painful.

Choice number 2: Get a new code and edit your current page to use it. Fast and fun!

How to get a new code: Open a new browser window, go to the page with the form, use “View source”, find the form you were filling in, find the hidden field in the form with a weird value, and copy the strange value.

How to edit your current page to use it: Go to the window where you’ve filled in your form, pop open your DOM editor, find the weird hidden field, and replace its value with your new code. Now when you submit your form, it will be using a fresh code!

OK, so maybe it isn’t easy, but at least it’s fast.

[1]: The long explanation is that the web site is probably using a continuation-passing style for managing client-side state and it garbage-collected your form submission continuation. I didn’t think you wanted to know.

Cite this page

September 3, 2009

Get BibTex-formatted citations for web pages in one click.

WordPress won’t let me embed JavaScript links in a page, so:

  1. Go here.
  2. Bookmark the link called “Cite this page”, e.g. by dragging it to your browser bookmarks bar.

Unescaped code (you might need to View Source on this page to get the whole thing):

javascript:(function(){var d = Date().toString().split(" "); alert("@misc{" + document.title.split(" ")[0] + ",\n    title={" + document.title + "},\n    url=\"\\url{" + window.location +"}\",\n    note=\"[Online; accessed " + d[2] + "-" + d[1] + "-" + d[3] + "]\",\n    author={" +(document.getElementsByName('author')[0] || {content:'TODO put in author'}).content + "}\n"  + "}")})()

Tested in Safari 4.

Units is out

August 17, 2009

Units is a new library for making Python programs safer. It’s also kind of fun.

For more, check out

Academic Hacker News hits Reddit frontpage, top of Google

March 9, 2009

Academic Hacker News ( hit the front page of reddit today and is the top Google result for “Academic Hacker News.”

A redditor pointed out that the lack of comments is a threat to its life. I’m not sure how to get people commenting— suggestions welcome.

The site is up to 162 registered users. I want to encourage more people here to use it, somehow, but Zak believes that the growth will make it less valuable as a research tool.

March 7, 2009

The launch

The other day I put up Academic Hacker News. People at school were too busy to check it out so I submitted it on the real Hacker News yesterday. You can see the discussion.

The response has been rewarding. People seem enthused with the idea. I got a few suggestions about my terrible taste in colours and the difficulty in finding the RSS feed. Over 100 people have registered user accounts and there have been over twenty submissions so far. Not bad for < 24 hours.

I want this to succeed and become a good tool for practitioners in our area. Now I need ideas on how to build momentum and get the word out. Help!

A lesson and thanks

This wouldn’t have happened if the department didn’t have infrastructure that let us easily set up servers. It also wouldn’t have worked if I didn’t have Alan‘s help and approval along the way. Thanks, Alan.

An open question: What other infrastructure could we have that would enable lightweight independent student projects?

Now, off to check out all the submitted links…

Academic Hacker News: A social news site for academic papers in CS

March 5, 2009

Check out Academic Hacker News. Sign up. Participate. Let’s make something good.

Hack Session 4

March 1, 2009

Andrew Louis, Andrew Trusty and I hacked together a social news site for academic papers. Code on hyfen’s github; application on Google App Engine.

There were three of us, so our 4-hour hack session means this application has 12-person hours of work behind it. I think our next iteration will turn this thing into something really worth using—we’ve planned some neat features. Right now it’s quite rough around the edges.

Give ‘er  a spin! But we warn you… Louis calls this an “extreme beta.”

Hack Session 3

February 19, 2009

Andrew flew solo to put together a new, improved, final version of the Idea Hat.  

Our hack sessions are open to anyone interested in having some fun and writing some code.

Hack Session 2

February 8, 2009

Andrew Trusty and I sat down for another 3-hour hack session on Thursday. A new version of the idea hat is the result. Our source code is available, of course.

We’re below 12 animal-hours of work on this thing. Not bad!

Here’s the changelist:

  • You can make your own independent hats.
  • New totally awesome design!
  • Submitted ideas are now anonymous.

Known bugs:

  • Every homepage access results in a new hat. We intend to have one global, default hat.
  • The ideas list superimposes all ideas.
  • URLs are not discoverable.

SortHN: My first GreaseMonkey script

February 4, 2009

SortHN is my first GreaseMonkey script. It reorders the items on the Hacker News front page in an attempt to push better submissions to the top.

Google “Code Snippets” are Cool

February 2, 2009


Code snippets for defaultdict

Hack Session 1

January 29, 2009

Andrew Trusty and I spent three hours this afternoon in a hack session. Reynold Xin was here for a bit but left because his laptop was unusable.

We built our first Google App Engine application: Idea Hat. It’s a little rough around the edges—we have ambitious goals for it—but it works and it runs at

Available on my GitHub or Andrew’s.

Now we’re hungry. Next week we finish this thing like Paris Hilton’s career.