Eloquent Javascript: A Modern Introduction To Programming By Marijn Haverbeke

As the title of the book indicates, Eloquent Javascript by Marijn Haverbeke is not so much about Javascript as it is about computer programming in the context of Javascript. And furthermore, the Javascript used in the book is completely outside the browser and the DOM (Document Object Model); aside from two short chapters at the end, the book focuses primarily on programming technique, syntax, and algorithms and almost not at all on DOM manipulation or client-server communication. This approach, perhaps suspect to those in the web-development world, ends up creating a very informative, very insightful exploration of the Javascript language and of programming theory.

Once upon a time, I used to be a TA (Teaching Assistant) for a web development class at University. When I met with my students in the computer lab, I quickly found that there were two kinds of computer science students: those that could understand the concept of a "variable" and those that could not. No matter how I tried to explain variables, it seemed that some students simply could not wrap their minds around it.

I always felt that their failure of understanding was my failure as a teacher. As such, I was tremendously excited when I read Haverbeke's explanation of variables in programming (pg 16):

You should imagine variables as tentacles, rather than boxes. They do not contain values; they grasp them - two variables can refer to the same value. Only the values that the program still has a hold on can be accessed by it. When you need to remember something, you grow a tentacle to hold on to it, or you reattach one of your existing tentacles to a new value.

This visual, sensory-rich description just felt right! Upon reading this, I suddenly wanted to go back in time to bring this kind of insight to the computer lab at University.

More than just variables, however, Haverbeke consistently demonstrates a mastery of both Javascript and of teaching throughout the book. While his examples can get quite complex, he typically manages to break them down into meaningful pieces. Furthermore, his examples at the beginning of the book tend to go through many rounds of refactoring, which I believe helps the reader to truly understand the programming thought-process and not just the final product.

Whether it was his use of "soaking peas" to explain "Abstraction" or his exploration of Functional and Object Oriented programming, I consistently found myself thinking about programming theory and not about Javascript itself. As I was working my way through this Terrarium example, it didn't occur to me that this book was making me better at Javascript - it occurred to me that this book was making me a better programmer.

Oh, and did I mention that he has a whole chapter on Regular Expressions! Heck yeah!

To end this review, I wanted to present the demo text that Haverbeke used in one of text-parsing examples. Though not important to techniques demonstrated in the chapter, I found the following text to be rather beautiful in its own light (pg 79-80):

% The Book of Programming

%% The Two Aspects

Below the surface of the machine, the program moves. Without effort, it expands and contracts. In great harmony, electrons scatter and regroup. The forms on the monitor are but ripples on the water. The essence stays invisible below.

When the creators built the machine, they put in the processor and the memory. From these arise the two aspects of the program.

The aspect of the processor is the active substance. It is called Control. The aspect of the memory is the passive substance. It is called Data.

Data is made of merely bits, yet it takes complex forms. Control consists only of simple instructions, yet it performs difficult tasks. From the small and trivial, the large and complex arise.

The program source is Data. Control arises from it. The Control proceeds to create new Data. The one is born from the other, the other is useless without the one. This is the harmonious cycle of Data and Control.

Of themselves, Data and Control are without structure. The programmers of old molded their programs out of this raw substance. Over time, the amorphous Data has crystallized into data types, and the chaotic Control was wrung into control structures and functions.

%% Short Sayings

When a student asked Fu-Tzu about the nature of the cycle of Data and Control, Fu-Tzu replied 'Think of a compiler, compiling itself.'

A student asked, 'The programmers of old used only simple machines and no programming language, yet they made beautiful programs. Why do we use complicated machines and programming languages?' Fu-Tzu replied 'The builders of old used only sticks and clay, yet they made beautiful huts.'

A hermit spent 10 years writing a program. 'My program can compute the motion of the stars on a 286-computer running MS DOS,' he proudly announced. 'Nobody owns a 286-computer or uses MS DOS anymore,' Fu-Tzu responded.

Fu-Tzu had written a small program that was full of global state and dubious shortcuts. Reading it, a student asked 'You warned us against these techniques, yet I find them in your program. How can this be?' Fu-Tzu said, 'There is no need to fetch a water hose when the house is not on fire.' {This is not to be read as an encouragement of sloppy programming, but rather as a warning against neurotic adherence to rules of thumb.}

%% Wisdom

A student was complaining about digital numbers. 'When I take the root of two and then square it again, the result is already inaccurate!' Overhearing him, Fu-Tzu laughed. 'Here is a sheet of paper. Write down the precise value of the square root of two for me.'

Fu-Tzu said, 'When you cut against the grain of the wood, much strength is needed. When you program against the grain of a problem, much code is needed.'

Tzu-li and Tzu-ssu were boasting about the size of their latest programs. 'Two-hundred thousand lines,' said Tzu-li, 'not counting comments!' Tzu-ssu responded, 'Psah, mine is almost a *million* lines already.' Fu-Tzu said, 'My best program has five hundred lines.' Hearing this, Tzu-li and Tzu-ssu were enlightened.

A student had been sitting motionless behind his computer for hours, frowning darkly. He was trying to write a beautiful solution to a difficult problem but could not find the right approach. Tu-Tzu hit him on the back of his head and shouted, '*Type something!*' The student started writing an ugly solution. After he had finished, he suddenly understood the beautiful solution.

%% Progression

A beginning programmer writes his programs like an and builds her hill, one piece at a time, without thought for the bigger structure. His programs will be like loose sand. They may stand for a while, but growing too big they fall apart {Referring to the danger of internal inconsistency and duplicated structure in unorganized code.}.

Realizing this problem, the programmer will start to spend a lot of time thinking about structure. His programs will be rigidly structured, like rock sculptures. They are solid, but when they must change, violence must be done to them {Referring to the fact that structure tends to put restrictions on the evolution of a program.}.

The master programmer knows when to apply structure and when to leave things in their simple form. His programs are like clay, solid yet malleable.

This book is a really good guide to Javascript; but even more than that, this book is a great guide to programming. If not familiar with Javascript, some of the examples can get rather complex. But, if you go through them slowly, I believe you'll get a tremendous amount of value out of this book. Definitely a recommended read.

Tweet This Titillating read by @BenNadel - Eloquent Javascript: A Modern Introduction To Programming By Marijn Haverbeke Woot woot — you rock the party that rocks the body!







