Strategies
If your function exposes multiple implementation strategies, make those explicit through a single argument that takes an enumeration. This makes it clear how to control the operation of your function and extends gracefully if you discover new strategies in the future.
This part of the book goes into some of the details of and variations on this pattern:
- You should consider this pattern even if there are only two variations and you’re tempted to use
TRUE
andFALSE
instead. Chapter 13 discusses why. - Sometimes, different strategies need different arguments and Chapter 14 shows a useful pattern to achieve this.
- Other time, the need for different arguments might suggest that you actually need different functions. That’s the topic of Chapter 15, and then we dive into a big case study of that problem by looking at
rep()
in Chapter 16.
See also
- The original strategy pattern defined in Design Patterns. This pattern has a rather different implementation in a classic OOP language.