The Great void of JavaScript

In JavaScript, you often see code like this:

// 1

(function() {
    a bunch of code;

The code in the function runs immediately and allows you to avoid namespace collisions with other code. If the code includes (inner) functions, those functions are essentially private. In fact, they will go away if there are no references to them when the code completes.

People seem to prefer the above syntax, though the code is equivalent to:

// 2

(function () {
    a bunch of code;

When I first felt the need to do this sort of thing, I tried the more intuitive syntax:

// 3

function () {
    a bunch of code;

That is what one would expect to work. It makes intuitive and syntactic sense. It clearly creates an anonymous function and then invokes it. However, it causes a syntax error because, according to here, a statement that begins with the function token must be a function declaration (as opposed to an invokable function expression).

Noting first that the following works without complaint:

// 4

var discard = function () {
    a bunch of code;

I then arrived at this:

// 5

void function () {
    a bunch of code;

Note that void is a unary operator in JavaScript that discards its operand and evaluates to undefined. It is neither a function, as has been misreported, nor a data type, nor the absence of a data type, as it is in Java.

To me, #5 is more readable than #1 and #2. What I don’t like about #5 is that it’s still a bit cryptic. It may be cryptic, but I think it’s less syntactically disconcerting than #1 and #2, where I really don’t like the mysterious outer parentheses.

In case you missed it

Cases #1, #2, and #5 are equivalent. To me, #5 looks the simplest and the least prone to errors. I do wonder if there any insidious differences deep under the hood of any browsers.

Encapsulation? Or clean air?

This is a technique in JavaScript commonly used to avoid namespace pollution, and it’s often referred to as encapsulation, even though it’s really not. Booch defines encapsulation as “serving to separate the contractual interface of an abstraction and its implementation.” To this end, in Java and ActionScript, we have the interface. In JavaScript, we don’t.


  • 2012-11-20. Douglas Crockford refers to case #1 as “dog balls” here and prefers case #2.

By Any Other Descriptor

I really have not been paying attention. Apparently there are various editions of Shakespeare’s works, with vastly different text among them, with no definitive versions. In fact, the earliest versions seem to be considered the least reliable. This is the opposite of how original publications of musical works are generally treated, where urtext (original text) editions are thought of as bringing us closest to the composer’s original intentions.

I grew up thinking I knew the following lines from Romeo and Juliet:

What’s in a name? That which we call a rose
By any other name would smell as sweet.

I heard this on the radio this past weekend as:

What’s in a name? That which we call a rose
By any other word would smell as sweet.

Wanting to verify my sanity, I quickly checked my bookmarked link to the Complete Works of William Shakespeare at MIT. By any other name. Sanity confirmed? Perhaps not. Google has this to say:

By any other name would smell as sweet (about 535,000 results)

By any other word would smell as sweet (about 124,000 results)

The ratio is about 4:1, which doesn’t spell a staggering defeat of one over the other. What was driving me crazy was that I couldn’t find any mention of this disparity.

Here’s another view from Google. Both phrases have coexisted for hundreds of years, with the name form always being more popular.

Beethoven vs. Shakespeare

At least in music, disparities like this get some attention. Beethoven marked the first movement of his fourteenth piano sonata (the Moonlight Sonata) to be played senza sordino. This brings up two very nonintuitive Italian markings in piano music:

Senza sordino: don’t use (or stop using) the soft pedal. The meaning is something like without [the] mute and instructs you to not mute the sound, which nowadays means don’t use (or stop using) the una corda pedal, where the una corda pedal is also known as the soft pedal or left pedal.

Senza sordini: do use the damper pedal. The meaning is something like without mutes (or dampers) and instructs you to play without dampers on the strings, which effectively means to depress the damper pedal, which is also known as the right pedal. This term rarely occurs in music in favor of using a fancily scripted Ped. marking instead.

Note: Schubert used sordini (i.e., short for con sordini, meaning with dampers) to mean don’t use the damper pedal. All these double negatives really make you stop and think! Brahms used una corda (one string) and tre corde (three strings), which to me is shrouded in less obscurity.

Some argue that Beethoven misspelled (or perhaps miswrote) sordini as sordino (plural vs. singular). In fact, some Beethoven editions make the according “correction.” The Harvard Dictionary of Music concludes (assumes) that Beethoven misspelled the word.

These pedal markings usually apply only to a few measures at a time within a piece. Beethoven’s marking has perhaps never been used at the beginning of another piece, where it seemingly refers to the whole piece. Some argue that Beethoven meant that the damper pedal should be held throughout the entire movement and would not have used a marking such as this (misspelled or otherwise) unless he meant something special, such as holding the pedal throughout the entire movement.

There are many levels to this controversy! They will be discussed forever and never resolved.

Words and grammar don’t really matter

Trying to find more information on the Shakespeare side of things, I found this website:

The site has photocopies of various early editions, excerpted as follows:

First quarto (1597)
Second quarto (1599)
First folio (1623)
Second folio (1632)
Third folio (1664)
Fourth folio (1685)

Wow! The amount of sloppiness and bad grammar boggles the mind. The same site has the only mention of this budding rose controversy I could find so far:

This page comes out and says, simply, that the first quarto (by any other name) is bad and the second quarto (by any other word) is good. Alas, the futility.