Refactoring code in functions

Creating functions is similar to creating mini factories that are independent and self-sufficient. The good functions do not require global variables to use, and are extremely modular and easy to debug.

Let’s take the guessing numbers game that we made previously. If we had to turn the random number generator into a function, how would we do it?

If we wanted to write a function that could take in two numbers as parameters and return a random value between the two numbers, here’s how we would go about doing it:

function getRandomNumber (lower, upper) {

var randomNumber = Math.floor(Math.random() * (upper – lower + 1)) + lower;

return randomNumber;

}

In fact, we can make this code even more compact:

function getRandomNumber (lower, upper) {

return Math.floor(Math.random() * (upper – lower + 1)) + lower;

}

We can even put safeguards on this function so that it checks to see if the parameters are actually numbers. You wouldn’t want someone trying to put in strings or other characters.

function getRandomNumber (lower, upper) {

if (isNaN(lower) || isNaN(upper)) {

throw new Error(“Both arguments must be numbers!”);

} else {

return Math.floor(Math.random() * (upper – lower + 1)) + lower;

}

}

Now that we have this function, we can use it wherever in our code to calculate any form on random numbers by simply calling it:

randomNumber(1, 20);

To show the modular power of functions, we can create callbacks – or calling a function within a function. The logic is quite easy once we grasp it. What if we wanted to created a function that checks whether a random number is greater than or equal to a given number? We could reuse our getRandomNumber function above, and call it in our new function. Here’s how we would go about doing it:

function compareRandomNumberSizeTo (number) {

var randomNumber = randomNumber(1, 20);

if (randomNumber < number) {

return “The you chose is greater than the number generated”;

} else {

return “The number you chose is less than the number generated”;

}

}

Now, you can call this function in an alert.

alert(compareRandomNumberSizeTo(13));

Callbacks are a major part of most programming languages, and this is the reason why it is crucial to create clean, modular code using functional programming. Think of it as individual factories that churn out information to be used in other individual factories. If each factory is constructed with good code, and is independent of other factories, we can create modular pieces of code that are reusable and easier to debug.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s