Nerdy Code Examples

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.

  1. Yes, I understand that the coding style of this particular example is a little bit, well, ugly. I’ve just taken the example completely out of context. I believe this particular chunk was meant to illustrate something about literals. I was just so taken with the subject matter of the example…

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.



appointive
appointive
appointive
appointive