Eloquent JavaScript

Once upon a time, every PC had a BASIC interpreter on it and an entire generation of programmers was spawned due to kids playing around with BASIC. My first experience with programming was in high school and it was with BASIC too! In the school library I discovered books written by David H. Ahl and others which had listings of simple BASIC games. Since those books were not lent out, I would write down the code listings onto paper and then type them in during computer class. The code I copied would inevitably have errors and it took a while to find and fix them. Much joy was had when the code executed and a simple game materialized on the screen.

Marijn Haverbeke, the author of Eloquent JavaScript fervently believes that JavaScript is the BASIC of today. It is indeed true, since every computing device today has a browser with a JavaScript interpreter cranking beneath it. With this motivation, Marijn has written this as a book for anyone who wants to learn programming.

I picked up this book to see what lay behind this language which is all pervasive and yet highly misunderstood and frowned upon. JavaScript was pushed out through the backdoor into the Netscape Navigator browser at the same time Sun Microsystems launched Java with much fanfare onto the world. Other than the 4 letters they both share, there is absolutely nothing in common between these languages! The book reveals that JavaScript is a badly designed, but highly powerful dynamically typed functional programming language hiding in C-like syntax! 😀

Writing some JavaScript code is easy, but writing good code in it is hard. This is not entirely the programmer’s fault, since the language has some seriously confusing features. For example, there are no classes in JavaScript, instead objects are created from constructors which have an underlying prototype. Properties and methods can be appended at will to both the objects and their prototypes. The book tries quite well to demystify such difficult concepts, but I still came away quite confused.

Functions are first class types in JavaScript, I was totally shocked to realize that we have a functional programming language throbbing underneath all our browsers! It was refreshing to see this introductory book treat functional programming as de riguer in JavaScript. All code examples in the book create and pass around functions whenever appropriate. I am still not familiar with functional programming, but this book gave me the most gentle introduction yet to this paradigm.

Programming the browser is not touched until the last 4 chapters. These chapters run through quickly showing examples of how to use JavaScript to read and modify the various web page elements. These are accessible through the Document Object Model (DOM) that the browser exposes to JavaScript. This too was quite an eye opener since I have not done any web programming other than some Perl and CGI in a former life.

Eloquent JavaScript is a good introduction to the language that can be easily finished over a weekend. The book is also available online here, where all its code examples can be edited and executed right inside the browser. I found most code examples useful, except for the grand Game of Life (Terrarium) example which is used throughout the chapter on Object-Oriented Programming. That example was just too big and was not instructive.

The book does enough to stoke the reader’s interest to program in JavaScript. I am looking around the JavaScript tool landscape and am pretty disappointed by the lack of standalone JavaScript interpreters or compilers, editors with code completion and IDEs with debugging capability. Easy availability of such tools and the ability to run standalone JavaScript programs would do wonders for this language! 🙂

CUDA by Example

With single processor speeds having hit a wall, there is a lot of interest in heterogeneous computing today. One of the popular ways to speed up applications is to rewrite them as massively parallel applications that execute on the NVIDIA CUDA architecture. It is quite hard to think of parallel solutions to existing problems and writing CUDA programs can be a minefield. These factors have made learning to swim in the choppy waters of CUDA difficult for beginners. Despite an abundance of CUDA information on the web, there has been no introductory material that is both simple and of good quality. The new book CUDA by Example: An Introduction to General-Purpose GPU Programming written by Jason Sanders and Edward Kandrot (both NVIDIA employees) aims to be such an introductory book for CUDA programming.

The only prerequisite expected of this book’s reader is knowledge of C. Spread over 12 quick chapters, the book uses example CUDA C programs all through to introduce concepts and explain their usage. Every example program is thoroughly broken down and the authors explain every stage of the process. It is quite heartening to see this detailed hand-holding extend all the way through to the complex concepts and last chapters. Chapters 1-5 are essential reading and the reader should be able to write simple CUDA programs after this point. The rest of the chapters acquaint concepts which are useful to further optimize the CUDA solution to take advantage of the problem domain or the CUDA architecture or both.

The book is strictly introductory, thankfully, and does not explain the CUDA architecture and its inner workings. I cannot commend the authors enough for taking this hard-line and making the jump into CUDA as simple and painless as they have done here. It would be natural to read the CUDA Programming Guide after this and keep it around as a reference for CUDA programming. This book is perfect for any inquisitive programmer wanting a taste of CUDA to see if it is worth his time. The avid reader can finish this book, having worked the examples and understood the major concepts, easily over a weekend.

Example code and errata of the book can be found here.

.Net Book Zero

Rating: 4/4 (The perfect introductory C# book for C and C++ programmers!)

I am most comfortable with C and C++, though it is mostly a love-hate relationship. C# is a modern programming language for the .Net platform and I have wanted to try it for sometime. I looked around for some good books and found most C# books to be horribly written. I mean, anyone can teach the keywords of the language and syntax. What one wants from a book is a deeper introduction. I finally found my match in .Net Book Zero by Charles Petzold. Petzold is a technical author who is legendary for his Programming Windows series of books. .Net Book Zero is his book for C and C++ programmers who want to learn C# and .Net.

Call me old fashioned, but I love it when the author says “Close your Visual Studio kid! Let me show you how to write and compile a C# program at the command-line using the C# compiler (csc.exe). Now, let us disassemble the program using the IL Disassembler (ildasm.exe) and examine the IL code the compiler produced for our program.”

I used version 1.1 of this book, which is updated only up to C# 2.0. I did not mind this, so the book was perfect for my needs! Petzold quickly glosses over features which work the same in C# as in C and C++. Instead he focuses on features that are new or work in subtly different ways. I especially loved the depth with which value types (allocated on the stack) and reference types (allocated on the managed heap) are covered all throughout the book. It is this kind of writing that enables a C/C++ programmer to come through this 250+ page book with an in-depth knowledge of how the gears crank underneath C#. The one thing that is missing in this book is a good index. I cannot think of an introductory C# book that is better suited for the C/C++ audience. Highly recommended. Petzold made a mistake sharing the book for free 😉

The book has some spelling mistakes, I have compiled the errata here.

Effective STL

Rating: 3/4 (Recommended for the bookshelf of any STL user)

I find myself using STL a lot in my C++ code. I also found that I was continuously rewriting the STL code, every time I discovered subtle bugs in the way I was using it or when I found a more elegant solution to the same problem. Looking around for wisdom on STL, I picked up Effective STL by Scott Meyers. Scott has authored two other books: Effective C++ and More Effective C++, both of which I have found to be approachable and useful.

Like the other books in the Effective series, this one too has 5x chapters or items, as they are called. The topics they deal with include containers, iterators, algorithms and functors. The whole area of extending STL has been skipped and left for other books. Each item in the book is self contained with a title, introduction, examples, explanations, guidelines and sometimes even trivia. I found the book very useful, showing up mistakes in my code and also sharing a lot of idiomatic methods to write what I wanted. While reading the book, I found myself compelled to go back to my code and start polishing it immediately! Much like the other Effective books by Meyers, Effective STL too is really easy to read, understand and apply. Recommended for the bookshelf of everyone who uses STL.

Related: My notes from this book can be found here.