Cargo-cult programming is what a programmer does when he or she doesn't know a particular language or paradigm well enough, and so ends up writing redundant and possibly harmful code. It rears its head quite often in the land of Java...
Cargo-cult programming is what a programmer does when he or she doesn't know a particular language or paradigm well enough, and so ends up writing redundant and possibly harmful code. It rears its head quite often in the land of JavaScript. In this article, I explore the concept of cargo-cult programming and places to watch out for it in JavaScript.Cargo-culting is sometimes defined as "the extreme adherence to the form instead of content". The form, in programming, being the syntax, paradigms, styles and patterns that we employ. The content being the abstract thing that you are seeking to represent through your code — the very substance of your program. A person with lacking understanding in an area is likely to copy the form of others without truly understanding, and thus their content — their program — can suffer.Cargo-culting is curiously common in JavaScript, probably because of the general low barrier to entry in the front-end development world. You can whip up an HTML page with a bit of JavaScript in seconds. As a result, there are many people who become sufficiently proficient in these technologies to feel comfortable creating and imposing rules on themselves and others. Eventually, other newcomers copy these rules. Dogmatic rules surface and spread, until they are considered the norm:Always use strict equality operatorsNever use evalAlways use a single var declaration per scopeAlways use an IIFE – it “protects” youA rule continues to spread until a programmer is only using a given technique because of its popularity, instead of considering each specific use-case independently.JavaScript Abuzz with Semi-colonsIf you've had the opportunity to witness the witty banter and rhetoric of the software developer over the years, you will have spotted a tendency to discuss seemingly tiny things at great lengths. Things like the semi-colon, the comma, white-space or the curly brace.Syntax like semi-colons or white-space may seem to purely be elements of form, not of content. But many of these subtle syntax rules can have significant effects in JavaScript. If you don't understand the 'form' then you cannot begin to understand the 'content'.So in this article, we will identify what areas of form in JavaScript are frequently cargo-culted off of — that is, copied without understanding. How JavaScript can seem…, an image from Angus Croll’s "The Politics Of JavaScript" presentation UndefinedAngus Croll, in a recent presentation, titled "The Politics Of JavaScript", highlighted one of the most common pieces of JS dogma that people cargo-cult off of:if (typeof myObject.foo === 'undefined') {...}
Most of the time, doing such a long-winded check for undefined is pointless. The technique became common because people were copying other people, not because of it's actual value.Of course, there are times when:
typeof x === 'undefined'
… is preferable to:x === undefined
But, equally, there are times when the latter is preferred. A quick overview of the options:
// Determine if `x` is undefined:
x === undefined
typeof x == 'undefined'
typeof x === 'undefined'
x === void 0
// Determine if `x` is undefined OR null:
x == null
x == undefined
People started using the typeof approach because they were protecting themselves against:A potentially undeclared variable (non-typeof approaches would throw TypeErrors)Someone overwrote undefined globally or in a parent scope. Some environments allow you to overwrite undefined to something like true. You have to ask yourself: “Is it likely that someone overwrote undefined, and should my script have to pander to such silliness?“But most of the time they're protecting themselves from having to worry. It's a catch-all avoidance of having to know the details. Knowing the details can help you though. Every character of your code should exist with a purpose in mind.The only time that you sh