design icon indicating copy to clipboard operation
design copied to clipboard

When you need more options

Open hadley opened this issue 2 years ago • 2 comments

You've used https://design.tidyverse.org/argument-clutter.html and you just keep adding more and more arguments to that function until it becomes overwhelming. What should you do? It's good to stick with the explicit options object, but instead of setting all the options in one function, provide one function to create the options object and a bunch of functions to modify it.

Examples include ggplot2 objects, httr2 (which is a good opportunity for a discussion of why httr is a less good approach), tidymodels.

hadley avatar Aug 04 '23 18:08 hadley

I like this. I have a related suggestion for the book - an alternative way to implement the advice perhaps.

I followed this advice when developing my package, and included the options function as a value for a parameter in my "main" functions. So they have the parameter opts = myfun_opts().

I have just been thinking that it might have been better for me to design this as a wrapper function instead (probably using withr::with_options()), like this:

myfun() |>
  myfun_opts(opt1 = "blue") |>
  ...

What do you think? Is this wrapper approach worth suggesting within the book?

I really like httr2's approach to building things up with different helper functions, so I think that points the way forward.

francisbarton avatar Feb 18 '25 18:02 francisbarton

Yes, once you have an object that encapsulates a bunch of state, it makes sense to provide functions to operate on it.

hadley avatar Feb 18 '25 18:02 hadley