Don’t Repeat Yourself (DRY)

The DRY concept comes up a lot in programming (you already saw it in database design—don’t repeat data in multiple tables), and it is especially important in organizing your code. If you have two different functions that do almost the same thing, you will end up writing the same instructions twice, which is a waste of time. What’s worse, though, is that when you find a bug in one of your functions (and, believe me, you will find a bug), you will have to fix it in both functions. Even worse still is when you fix the bug in one function but forget to fix it in the other function, and you end up thinking, “What is going on? I already fixed this bug.”

The first big project I worked on was a somewhat complex website for my university. One of the most important features of the website was a typeahead (see Figure 8.2—I didn’t know that these were called typeaheads when I was building the website). In the process of building the website, I probably made 15 typeaheads, and I had to repeat a whole bunch of code for each one. I did a lot of copy-and-pasting, and every time I found a bug, I had to fix it in 15 different places. Every time I found a better way of handling the typeahead, I had to update 15 different functions. Don’t repeat yourself—because you are just creating more work for yourself.

Image

Figure 8.2 Typeaheads are so useful—and even better when you have to write the code only once.

Let’s get back to the counting to 10 example. If we do a lot of counting from 1 to 10, we might still want a countToTen function. But instead of repeating a bunch of code, we can just use the countrFromXtoY function inside of countToTen (see Listing 8.15). No repeated code. Awesome!

Listing 8.15 countToTen Using countFromXtoY


function countToTen() {
  countFromXtoY(1, 10);
}



Anonymous Functions and Callbacks

Most functions have names. Almost every function you have seen in the code examples in this book has had a name. You execute a function by calling it by name. But some functions are anonymous; they have no name. How can you execute a function if you don’t know the name to call it? Why would anyone ever want an anonymous function? That’s crazy talk. Well, think back to the forEach example from the last chapter. forEach is a special function for accessing each item in an array, and forEach accepts an anonymous function as an argument. See Listings 8.16 and 8.17.

Listing 8.16 forEach Revisited


> var lettersArray = ['a', 'b', 'c'];
  ["a", "b", "c"]
> lettersArray.forEach( function(letter) {
    console.log(letter);
  } );
  "a"
  "b"
  "c"


Listing 8.17 forEach’s Anonymous Function


function(letter) {
  console.log(letter);
}


That function has no name. A function that is passed as an argument to another function is often called a callback. A callback is a block of code to be executed by the other function. The callback function is just a container of instructions, so it doesn’t need a name. Now that is some crazy stuff.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.5.86