Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
In this chapter we are going to look at one of the most essential parts of any language, the function. Functions allow us to encapsulate reusable and discrete code blocks. Without functions our code would be one long, unreadable, and unmaintainable mess.
I wanted to give you an example of what JavaScript would look like if we were not able to use or write functions, but I was unable to. Even the simplest example of taking a string and making it lowercase requires the use of functions in JavaScript.
Because I can’t show you an example devoid of functions, I’ll show you an example of some CoffeeScript code that could use the help of a function or two, so you can see how important functions are to helping you keep your code manageable.
Example: (source: no_functions_example.coffee)
tax_rate = 0.0625
val = 100
console.log "What is the total of $#{val} worth of shopping?"
tax = val * tax_rate
total = val + tax
console.log "The total is #{total}"
val = 200
console.log "What is the total of $#{val} worth of shopping?"
tax = val * tax_rate
total = val + tax
console.log "The total is #{total}"
Example: (source: no_functions_example.js)
(function() {
var tax, tax_rate, total, val;
tax_rate = 0.0625;
val = 100;
console.log("What is the total of $" + val + " worth of shopping?");
tax = val * tax_rate;
total = val + tax;
console.log("The total is " + total);
val = 200;
console.log("What is the total of $" + val + " worth of shopping?");
tax = val * tax_rate;
total = val + tax;
console.log("The total is " + total);
}).call(this);
Output: (source: no_functions_example.coffee)
What is the total of $100 worth of shopping?
The total is 106.25
What is the total of $200 worth of shopping?
The total is 212.5
In our example, we are calculating the total value of goods purchased in-state with certain sales tax. Apart from the banality of the example, you can see that we are repeating our code to calculate the total value with tax several times.
Let’s refactor our code a bit, add some functions, and try to clean it up.
Example: (source: with_functions_example.coffee)
default_tax_rate = 0.0625
calculateTotal = (sub_total, rate = default_tax_rate) ->
tax = sub_total * rate
sub_total + tax
val = 100
console.log "What is the total of $#{val} worth of shopping?"
console.log "The total is #{calculateTotal(val)}"
val = 200
console.log "What is the total of $#{val} worth of shopping?"
console.log "The total is #{calculateTotal(val)}"
Example: (source: with_functions_example.js)
(function() {
var calculateTotal, default_tax_rate, val;
default_tax_rate = 0.0625;
calculateTotal = function(sub_total, rate) {
var tax;
if (rate == null) rate = default_tax_rate;
tax = sub_total * rate;
return sub_total + tax;
};
val = 100;
console.log("What is the total of $" + val + " worth of shopping?");
console.log("The total is " + (calculateTotal(val)));
val = 200;
console.log("What is the total of $" + val + " worth of shopping?");
console.log("The total is " + (calculateTotal(val)));
}).call(this);
Output: (source: with_functions_example.coffee)
What is the total of $100 worth of shopping?
The total is 106.25
What is the total of $200 worth of shopping?
The total is 212.5
You probably don’t understand everything we just did there, but don’t worry, that’s what this chapter is for. However, even without knowing the specifics of how functions are defined, and work, in CoffeeScript, you can see how much cleaner our code is between the two examples. In the refactored code, we are even able to pass in a different tax rate, should we need to. This also helps us keep our code DRY1: Don’t Repeat Yourself. Not repeating your code makes for an easier-to-manage code base with, hopefully, fewer bugs.