Nerdy Code Examples

Usu­ally when you’re trying to learn some pro­gram­ming lan­guage, you end up suf­fer­ing through some pretty lame exam­ples 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 exam­ples of this in dif­fer­ent books and tutorials.

A couple of weeks ago I was read­ing a chap­ter from some book about Javascript objects. I was really struck by how cool the code exam­ple that they used for the chap­ter was. The chap­ter was about objects in Javascript (which, coin­ci­den­tally 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 exam­ple1. It’s so incred­i­bly nerdy and per­fect. I mean, usu­ally with these object exam­ples, the code is com­pletely arbi­trary. It doesn’t actu­ally do any­thing, it’s just there illus­trate a point. The more visual the exam­ple, the better. I’ve decided that all coding exam­ples that are nec­es­sar­ily arbi­trary should at least be about some­thing inter­est­ing, like video games or robots.

  1. Yes, I under­stand that the coding style of this par­tic­u­lar exam­ple is a little bit, well, ugly. I’ve just taken the exam­ple com­pletely out of con­text. I believe this par­tic­u­lar chunk was meant to illus­trate some­thing about lit­er­als. I was just so taken with the sub­ject matter of the example…

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply