-
Notifications
You must be signed in to change notification settings - Fork 53
/
explicit-strategies.qmd
20 lines (14 loc) · 1.26 KB
/
explicit-strategies.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Strategies {#sec-strategies-explicit}
```{r}
#| include = FALSE
source("common.R")
library(stringr)
```
If your function exposes multiple **implementation strategies**, make those explicit through a single argument that takes an [enumeration](#sec-enumerate-options).
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` and `FALSE` instead. @sec-boolean-strategies discusses why.
- Sometimes, different strategies need different arguments and @sec-strategy-objects 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 @sec-strategy-functions, and then we dive into a big case study of that problem by looking at `rep()` in @sec-cs-rep.
## See also
- The original [strategy pattern](https://en.wikipedia.org/wiki/Strategy_pattern) defined in [Design Patterns](https://en.wikipedia.org/wiki/Design_Patterns). This pattern has a rather different implementation in a classic OOP language.