archives

An Eloquent Book

I recently came across an on-line JavaScript book titled "Eloquent JavaScript," with the subtitle of "An opinionated guide to programming," by Marijn Haverbeke.

I was interested in learning about the language to improve some node.js programs I had written. I was hoping to find a book which teaches the language as a whole, rather than as a collection of scripts a designer might use to make his or her web page look fancy.

I was disappointed to read this book described by the author as targeted for the "beginning programmer." Usually this means a long paragraphs with tedious motivations leading to the inevitable "hello world" program. From there books continue to introduce basic arithmetic, variables, for/while loops, input/output, etc. All of this is necessary for beginners, but usually skipped by experienced programmers who just need to know how this language differs from the languages they already know. Luckily, "Eloquent JavaScript" is one of the few books that teaches beginners the various constructs they need to assemble their programs, but also teaches them (and reminds experts of) how to harness the immense, almost magical, power of programs to turn ideas abstract thoughts in our head to objects which can be shared and executed with perfect fidelity.

In a few introductory paragraphs, Marijn describes the evolution of programming languages as such:

In the beginning, at the birth of computing, there were no programming languages. Programs looked something like this:

00110001 00000000 00000000
00110001 00000001 00000001
00110011 00000001 00000010
01010001 00001011 00000010
00100010 00000010 00001000
01000011 00000001 00000000
01000001 00000001 00000001
00010000 00000010 00000000
01100010 00000000 00000000

That is a program to add the numbers from one to ten together, and print out the result (1 + 2 + ... + 10 = 55).

...

It might help to use names instead of numbers for the instructions and memory locations:

 Set 'total' to 0
 Set 'count' to 1
[loop]
 Set 'compare' to 'count'
 Subtract 11 from 'compare'
 If 'compare' is zero, continue at [end]
 Add 'count' to 'total'
 Add 1 to 'count'
 Continue at [loop]
[end]
 Output 'total'

At this point it is not too hard to see how the program works.

...

Here is the same program in JavaScript:

var total = 0, count = 1;
while (count lt 10) { //edit: replaced "less than or equal to "sign with 'lt' for formatting
  total += count;
  count += 1;
}
print(total);

...

Finally, here is what the program could look like if we happened to have the convenient operations range and sum available, which respectively create a collection of numbers within a range and compute the sum of a collection of numbers:

print(sum(range(1, 10)));

The moral of this story, then, is that the same program can be expressed in long and short, unreadable and readable ways.

...

A good programming language helps the programmer by providing a more abstract way to express himself. It hides uninteresting details, provides convenient building blocks (such as the while construct), and, most of the time, allows the programmer to add building blocks himself (such as the sum and range operations).

The chapter on functional programming uses a metaphor of recipes to describes how a naive attempt to write a non-trivial program can turn into a "grandmother's tale," when what is actually required is a concise, terse description. The chapter on "searching" shows a maps and challenges readers to describes how to find the shortest path between two locations. It should be no surprise to any of us how important such descriptions are to introduce some fundamental algorithms to students (many whom will not realize they are now wandering into computer science, not just software engineering).

LTUers are well aware of Structure and Interpretation of Computer Programs, series of books which include The Little Schemer, and How to Design Programs. I believe I have mentioned how well Scala By Example[PDF] is written.

Elequent JavaScript is among the books that should not only be recommended to new programmers, but also referenced by those who are planning on writing books of their own.