O'Reilly logo

MORE JOEL ON SOFTWARE: Further Thoughts on Diverse and Occasionally Related Matters That Will Prove of Interest to Software Developers, Designers, and Managers, and to Those Who, Whether by Good Fortune or Ill Luck, Work with Them in Some Capacity by Joel Spolsky

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

twenty-twoCAN YOUR PROGRAMMINGLANGUAGE DO THIS?

TUESDAY, AUGUST 1, 2006

One day, you're browsing through your code, and you notice two big blocks that look almost exactly the same. In fact, they're exactly the same, except that one block refers to "Spaghetti" and one block refers to "Chocolate Mousse."

// A trivial example: alert("I'd like some Spaghetti!"); alert("I'd like some Chocolate Mousse!");

These examples happen to be in JavaScript, but even if you don't know JavaScript, you should be able to follow along.

The repeated code looks wrong, of course, so you create a function:

function SwedishChef( food ) {     alert("I'd like some " + food + "!"); } SwedishChef("Spaghetti"); SwedishChef("Chocolate Mousse");

OK, it's a trivial example, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required