https://www.joelonsoftware.com/2006/08/01/can-your-programming-language-do-this/ Skip to content * View menu * View sidebar Joel on Software Your host [Pong] I'm Joel Spolsky, a software developer in New York City. More about me. [ ] [SUBMIT]Search [newdesign-]Read the archives in dead-tree format! Many of these articles have been collected into four books, available at your favorite bookstore. It's an excellent way to read the site in the bath, or throw it at your boss. Careers [jobs] Ready to level up? Stack Overflow Jobs is the job site that puts the needs of developers first. Whether you want to take control of your search or let employers discover you, we're on a mission to help every developer find a job they love. Looking to hire smart programmers who get things done? Stack Overflow Talent is a fully-customized sourcing solution that helps you understand, reach, and attract developers on the platform they trust most. Find the right candidates for your jobs. Learn more. [so-logo] For my day job, I'm the co-founder and CEO of Stack Overflow, the largest online community for programmers to learn, share their knowledge, and level up. Each month, more than 40 million professional and aspiring programmers visit Stack Overflow to ask and answer questions and find better jobs. Stack Overflow is also the flagship site of the Stack Exchange network, 160+ question and answer sites dedicated to all kinds of topics from cooking to gaming. According to Quantcast, Stack Overflow is the 30th largest web property in the United States and in the top 100 in the world. profile for Joel Spolsky on Stack Exchange, a network of free, community-driven Q&A sites [fc-logo] I also founded Fog Creek Software, one of the most influential small tech companies in the world. As an independent, privately-owned company, we've been making customers happy since the turn of the century. We share what we've learned about how to make great software, both by writing about our ideas and by creating products, like FogBugz, Trello and Gomix, that help others make great technology. As a result, Fog Creek's impact on the world of developers rivals companies a thousand times our size. Twitter! Twitter! My Tweets August 1, 2006January 20, 2017 by Joel Spolsky Can Your Programming Language Do This? * Rock star developer, News 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 Moose." // A trivial example: alert("I'd like some Spaghetti!"); alert("I'd like some Chocolate Moose!"); 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 Moose"); A picture of the Swedish Chef OK, it's a trivial example, but you can imagine a more substantial example. This is better code for many reasons, all of which you've heard a million times. Maintainability, Readability, Abstraction = Good! Now you notice two other blocks of code which look almost the same, except that one of them keeps calling this function called BoomBoom and the other one keeps calling this function called PutInPot. Other than that, the code is pretty much the same. alert("get the lobster"); PutInPot("lobster"); PutInPot("water"); alert("get the chicken"); BoomBoom("chicken"); BoomBoom("coconut"); Now you need a way to pass an argument to the function which itself is a function. This is an important capability, because it increases the chances that you'll be able to find common code that can be stashed away in a function. function Cook( i1, i2, f ) { alert("get the " + i1); f(i1); f(i2); } Cook( "lobster", "water", PutInPot ); Cook( "chicken", "coconut", BoomBoom ); Look! We're passing in a function as an argument. Can your language do this? Wait... suppose you haven't already defined the functions PutInPot or BoomBoom. Wouldn't it be nice if you could just write them inline instead of declaring them elsewhere? Cook("lobster", "water", function(x) { alert("pot " + x); } ); Cook("chicken", "coconut", function(x) { alert("boom " + x); } ); Jeez, that is handy. Notice that I'm creating a function there on the fly, not even bothering to name it, just picking it up by its ears and tossing it into a function. As soon as you start thinking in terms of anonymous functions as arguments, you might notice code all over the place that, say, does something to every element of an array. var a = [1,2,3]; for (i=0; i