Don’t Repeat Yourself – Functions and Scriptblocks
In this chapter, we’re going to look at one of the basic principles of software development and learn how to apply it to save ourselves effort and make our code easier to maintain: Don’t Repeat Yourself, also known as the DRY principle. In The Pragmatic Programmer, by Andy Hunt and Dave Thomas, the principle is stated as “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.” Some programmers will take this principle to its logical conclusion and have no repeating code anywhere. We’re writing scripts, and so we will apply the principle just as much as is convenient. We will talk about the concept of functions within code. By the end of the chapter, we will have seen how to write functions in our scripts to replace repeating code and learned how doing this makes our scripts easier to adapt and repair. We will also learn about another type...