Search

f(x)

“The ?rst rule of functions is that they should be small. The second rule of functions is that they should be smaller than that.” (page 34)  When I was first learning programming, it was my understanding that functions should be used whenever you have a piece of code that you will reuse.  If you aren’t going to reuse it, it doesn’t need to be a function.  However, this overlooks an important feature of functions: their names.  Even if a function is only used once, even if doesn’t return anything, even if it doesn’t take parameters, it’s still useful to take a block of code and name it according to its intent.  Does this lead to a lot of functions?  Maybe.  So what?  It’s easier to read and maintain.

Functions shouldn’t have a lot of parameters.  A bunch of ‘out’ or ‘ref’ parameters is usually a clear sign that maybe another class should be created.  A function with one parameter is called a monad.  A function with two parameters is called a dyad.  A function with three parameters is called a triad.  A function with four or more parameters is called epic fail.

This chapter is a very good one, with lots of great guidelines for writing functions, and a lot of the stuff in this chapter can also be applied to objects & systems (as I’ve read in later chapters).

Leave a Reply