Deciding what goes into a function
A function is a way for us to package a code block and give it a name. This is a good idea for several reasons. Back in Chapter 4, Software Projects and How We Organize Our Code, we talked about software modules and that dividing our code into small parts is wise as it will give us code that is easier to read, update, and maintain. The same reason applies to functions as they, too, package our code into smaller units. Another reason we want to use functions is so we can easily reuse parts of our code.
When deciding what will go into a function, we can have one rule of thumb. A function should always do only one thing and it will be named after what reflects that. What this means is that if we have a function called
send_email_and_print_invoice, we are doing things wrong. This function does two distinct tasks and should, therefore, be two separate functions. We can rephrase this rule with a quote by Robert C. Martin, the author of an excellent...