Nerdy Code Examples
October 24th, 2006
Usually when you’re trying to learn some programming language, you end up suffering through some pretty lame examples of code.
function Card(name,address,work,home) {
this.name = name;
this.addres = address;
this.workphone = workphone;
this.homephone = homephone;
this.PrintCard = PrintCard;
}
Sure, it gets the point across, but man is that boring. I think I’ve seen 20 examples of this in different books and tutorials.
A couple of weeks ago I was reading a chapter from some book about Javascript objects. I was really struck by how cool the code example that they used for the chapter was. The chapter was about objects in Javascript (which, coincidentally so was the above example).
var jetpack = true;
var robot = {
name: null,
hasJetpack: jetpack,
model: "Guard",
attack: function() {alert("ZAP!");},
sidekick: {
name: "Spot",
model: "Dog",
hasJetpack: false,
attack: function() {alert("CHOMP!");}
}
};
robot.name = "Zephyr";
robot.sidekick.attack();
I just love this example1. It’s so incredibly nerdy and perfect. I mean, usually with these object examples, the code is completely arbitrary. It doesn’t actually do anything, it’s just there illustrate a point. The more visual the example, the better. I’ve decided that all coding examples that are necessarily arbitrary should at least be about something interesting, like video games or robots.
You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.