Significant Whitespace

31 Mar 2010 | By Alex Young | Tags programming style

In his homoiconic blog, Reg Braithwaite recently discussed jQuery because he heard that it “changes the way you think about programming”. In Significant Whitespace, Reg discusses how jQuery embraces the use of chained calls on separate lines to increase readability:

instance
  .call1(text)
  .call2({...})
  .lastcall('open');

He makes an interesting stylistic point:

To keep the logic clear I’ve been using the following style guideline: When I’m returning the receiver, I do not indent.

So when he returns something else from a chained call, he indents:

$(event.target)
  .parents('body > *')
    .find('.wants_close')
      .trigger(event);

He likens more complex chained and indented calls to haml and sass. JavaScript isn’t aware of the level of indentation, but he suggests that this could be useful, and relates this to jQuery’s end() method.

His last point is what really made me understand his arguments:

I have some other reasons for wanting this in the language that have to do with destructuring assignment, pattern matching, and other uses for code that looks like the data it manipulates.

Even though his hypothetical language would be interesting, understanding end() can trip up jQuery beginners working on other people’s code. I’ve had to untangle deeply nested ends before and it’s one of those things that encourages me to finish work early and hit the beer. If you’re feeling a little bit lost, the end() documentation makes it all clear once you work through the examples.


blog comments powered by Disqus