Pages

Wednesday, November 2, 2016

Why JavaScript

A parent recently asked me why we are using JavaScript instead of a language like Java to learn object-oriented programming in AP Computer Science Principles. The following (with a few typo corrections) is a large portion of my reply to that question. It is by no means a complete apology for using JavaScript, but it's a start.

Teaching object-oriented programming is a challenge no matter what language one is learning first. So much so that there are well known, language-independent phrases that describe options: “objects early”, “objects in the middle”, and “objects late”, for example.

JavaScript is by no means a perfect language, and when it comes to object-oriented programming it’s an especially easy language for many programmers (beginning and experienced) to misunderstand. For experienced programmers, I suspect that’s due in large part to the fact that many of us learned object-oriented programming in the context of “classic” or “traditional” object-oriented programming languages like C++, C#, Java, etc. JavaScript is different from a language like Java in many ways: it’s loosely typed, dynamic, interpreted, and it uses constructors and prototypes instead of classes as means of managing objects. Furthermore, objects in JavaScript are associative arrays, and JavaScript arrays themselves are dynamic.

This contrast between languages is nothing new in the field of computer science. In the classic “little” book More Programming Pearls: Confessions of a Coder, Jon Bentley begins “Column 2: Associative Arrays” with this very interesting, insightful, short paragraph:
Anthropologists say that language has a profound effect on world view. That observation, usually known as Whorf’s hypothesis, is summarized as “Man’s thought is shaped by his tongue.”
In the paragraphs that follow, Bentley (who taught computer science at Carnegie-Mellon University, where he received a Dean’s Award for Excellence in Undergraduate Education) compares and contrasts languages like Algol, PL/1, C and Pascal with languages like Lisp and APL. He says, “…languages challenge the way we think about computing. We are amazed by Lispers as they work magic with their S-expressions and recursion, by APL fans who model the universe as the outer product of a couple of long vectors, and by Snobol programmers who seem to turn any problem into a big string.” Bentley singles out Awk as being Algol-like but with the noteworthy twist of having associative arrays. Today, Bentley might very well have used Java and JavaScript as examples of similar but very distinct languages that challenge the way we think about computing in their own unique way.

Today JavaScript is the de facto standard programming language of the internet. It is incorporated into most if not all modern web browsers. With the press of a button (F12 on the computers I use most often) Chrome, for example, presents a very sophisticated integrated JavaScript development environment, no additional software required. Type a little or copy and paste a bunch of JavaScript code at the command prompt presented in the console and you’re immediately executing code, no compiling required. Moreover, in my own experience the characteristics of the JavaScript language itself can be very powerful when it comes to designing simple, elegant solutions to problems. It is distinctive enough that someone who has taken AP Computer Science A with me at Valley can then take AP Computer Science Principles (or vice versa), and even if we repeated the exact same programming assignments in both classes both years, the differences between the two languages would keep the material fresh and engaging (I hope) and worth re-learning (I believe). (That said, if you look very carefully at the numerous examples of Java and JavaScript code that I’ve handed students on a silver platter, you’ll quickly see how easy it is to translate back and forth between the two languages, in spite of their many fundamental and sometimes superficial differences.)