A Programmer’s Tour Of Javascript

In the spring of 2014, I took a terrible, terrible web programming course at college. One of the many reasons it was so bad was because of the short shrift it gave to the JavaScript language.

This, unfortunately, seems to be very common among undergraduate courses dealing with any kind of web development. Most of these courses use source material that is geared towards web designers – people whose primary interest is in graphic design or UI design, and who are not themselves computer scientists. Though they are usually 300- or 400-level courses, they never seem to take into account that the students in the class are of the sort that have actually written a compiler, or coded a driver for a network card in assembly language, or who know what P vs. NP means. Instead, the course material is little more than beginning tutorials for people who have never touched a line of C++ in their lives.

This is a tragedy, because JavaScript (and web development in general) is a deep subject. After taking the course (and easily getting an A), I revisited JavaScript to (re-)learn all of the things the class didn’t teach. And now that I know more about different programming languages, I was impressed with how powerful and beautiful JavaScript really is.

So, I’m writing this article to combat the shallow view of the JavaScript language that is fostered among programmers. My intended audience is programmers who know C++, Java, and possibly a functional language like Scheme, and who want to actually learn JavaScript from a programmer’s perspective. It may also be helpful to intermediate JavaScript programmers who want to learn other languages, but will be tripped up by the many differences between JavaScript and other OOP languages.

But it is not meant for web designers. I will not cover HTML or CSS at all. I will mainly talk about core JavaScript, and only those parts that differ from other C-style languages. I will only briefly cover client-side JavaScript, when I talk about the JavaScript environment. There are plenty of other places to learn that stuff. I do assume that people reading this have at least a basic knowledge of HTML, because these days, who doesn’t?

Now, on to JavaScript. To start off, we should know exactly what kind of language JavaScript is.

JavaScript is a dynamic prototyping language. This means it is interpreted, dynamically typed, handles object inheritance using prototypes, and has first-class functions.

It shares none of these things with C++ or Java. In those languages, code is compiled before execution; variables are strongly and statically typed; functions are not first-class citizens; and inheritance is handled by extending classes. This can make JavaScript somewhat foreign to most programmers. On the other hand, JavaScript uses a C-style syntax common to a host of other programming languages (C++, Java, PHP, Perl, etc). It is incredibly easy to pick up if you know any of these other languages; and if you don’t, JavaScript may be a good introduction to them.

This makes JavaScript an ideal candidate for programmers who want to explore high-level subjects (like closures) that would ordinarily require learning a functional programming language like Lisp, Scheme, or Haskell (and their painful syntaxes). JavaScript is also used nearly everywhere, unlike those esoteric functional languages.

Each of these differences requires some explanation, and those explanations lead naturally to JavaScript-specific features and coding patterns. This is not a trivial topic. Even though I broke up the article into multiple pages, it’s still a long read, so be forewarned.

Though everyone uses the term “JavaScript,” the language is specified by Ecma, and the standardized version is technically called ECMAScript. This is the version I will talk about. Some vendors’ impelentations of JavaScript differ from the standard, but I will cover them when the need arises.

After reading this article, you will have a “wide but shallow” understanding of JavaScript, so I also included some book recommendations if you want to learn more.

Addendum: After the article was finished, I showed this to other programmers. I’m incorporating some of their suggestions. Credit and thank-you’s are at the end of the article.

Posted in JavaScript, Programming, Uncategorized | Tagged | Leave a comment

Classes in C++

If you’ve been following this blog, then you know I just wrote an article about passing objects as arguments in C++.

This article was originally intended to be part of that one. But while writing it, I realized that you need to know about objects in general, and how they work in C++. This is not a trivial subject, and what started out as a mere introduction ballooned into the article you see before you.

This article will cover object life cycles, inheritance, polymorphism, and composition. These are deep subjects in their own right, so I’ll only cover how C++ handles them. Even so, it is not an article for people who are completely new to object-oriented programming. It will require lot of thought, but in my opinion, it’s worth it. Aside from being a better C++ programmer, you will also gain a deeper understanding of how other languages operate “behind the scenes.”

Continue reading

Posted in c++, Programming | Leave a comment

Like Bigger Stones: Argument Passing in C++, Part 2

In my previous article, I talked about how C++ handles parameter passing when the arguments are primitive types or arrays. This article will talk about passing objects in C++. Objects are more complicated than primitives or arrays, so as you can imagine, passing them is also more complicated.

Before you read this article, you should understand the three basic C++ mechanisms for parameter passing: by value, by reference, or by pointer (or “simulated reference”). You should also understand how C++ hanldes automatic local variables. If you don’t understand this already, you should read my previous article on the subject. (If you don’t understand that, then I’m not a good writer, and you shouldn’t read this article anyway.)

Continue reading

Posted in c++, Programming | Leave a comment

Like Stones: Argument Passing in C++, Part 1

For people new to C++, argument passing can be very hard to understand. Most programming languages handle argument passing “behind the scenes” somehow. Either there is really one form of argument passing (e.g. C), or your choices are determined for you (e.g. Java). Not so with C++. Like almost everything else, C++ requires that you handle argument passing yourself.

So, for those people, I thought I’d write up a couple of posts about the various types of argument passing. But be aware: despite what you may think, this is not really an article for beginners. I wrote this mostly for people who already have some programming experience in another language, but are new to C++. It doesn’t require a lot of knowledge, and I’m not saying that beginners won’t get anything out of it. But if you are an absolute beginner, you may be overloaded with information.

With that out of the way, let’s talk about C++. The short version is that C++ has three mechanisms for argument passing: passing by value, passing by reference, or passing by pointer (or “simulated reference”). I’ll explain how each one works in turn.

Continue reading

Posted in c++, Programming | Tagged , | Leave a comment

An Evil Scheme

In the winter of 2013, I took a course at UMass Boston called CS 450: The Structure of Higher Level Languages. I had no choice; it is a required course for getting a CS degree. I hated, hated, hated that course. And I am hardly alone; of all my classmates that I talked to, not one liked it. The best one could say is that others hated it less than me.

The main reason for my hatred is because it is a Scheme course. I know that there are many programmers who love Scheme, but I have no idea why. I also know that most programmers have never written a single line of Scheme code in their entire lives. But quite a few of them will be forced to eventually; UMass is not the only university that has a Scheme requirement.

If you’re one of those programmers, then this article is for you. By reading this article, you will hopefully be a little more prepared for your inevitable pain. On the flip side, if you read this article and say, “Hey, that actually sounds kinda cool” – well, then, Scheme is the language for you. Good for you; you’ll save lots of money on therapy.

Continue reading

Posted in Programming, Scheme | Tagged , | 1 Comment

Efficient prime number testing

In nearly every lower-level CS class, students are invariably asked to create a function that can recognize a prime number. There are very good reasons for this. For one thing, the study of prime numbers is one of the longest-running fields in mathematics; the ancient Greeks were the first to study them in depth, but even the ancient Egyptians distinguished between prime numbers and composites. For another, the study of prime numbers has applications in a wide variety of fields. The one that will probably interest CS students the most is cryptography.

But the reason I’m going to talk about them is because recognizing prime numbers is a good starting point for talking about algorithms. By starting with the most basic, inefficient algorithm, we can learn some techniques to improve efficiency and eliminate redundancy. These same techniques can be applied to almost any algorithm, and will be useful (I hope) throughout our entire careers.
Continue reading

Posted in Programming | Tagged , , | Leave a comment

The Double Baggins

(This post originally appeared on my Digication site.)

As a (non-gradable) challenge for BHCC’s Data Structures course, Prof. Richmond challenged us to come up with a new sorting algorithm. This is the one I came up with.
Continue reading

Posted in Programming | Tagged , , | Leave a comment