1

I am building an API where I allow the users to passes 2 different list of custom fields. The API is basically this:

def action(type, name, date, name_custom_1, name_custom_2, name_custom_3, date_custom_1, date_custom_2, date_custom_3, date_custom_4)

So type, name date are parameter of this API and are mandatory.

name_custom_*, and date_custom_* are optionals, I could have 0, 1, 2, 3 ... I am putting a limit to 3 for name_custom and 4 to date_cutom for technical reasons, but eventually this limit can get increased (but never extremely will never be completely remove)

Now my question is, what is the best way to make this API, from a user point of view:

def action(type, name, date, name_custom_1, name_custom_2, name_custom_3, date_custom_1, date_custom_2, date_custom_3, date_custom_4)

or

def action(type, name, date, names_custom, dates_custom):

Where names_custom and dates_custom are a list which can not be bigger than X.

I am struggling between both and find value and logic in both. Any suggestions?

dizballanze
  • 1,267
  • 8
  • 18

1 Answers1

0

The list parameters give a cleaner solution, because:

  1. There are less arguments in the function signature, making the documentation easier to read for humans.
  2. It is more resilient to change. Suppose you decide to change the maximum number of custom arguments from 4 to 5. In the list approach, the change is simpler.

Even having 5 arguments in a function call is more than usual, and often considered sloppy (see How many parameters are too many?). You may want to consider introducing a class, or a few classes in here. Depending on your application, maybe it makes sense to create class that encapsulates the name and the list of custom names, and a class that encapsulates the date and the list of custom dates? And perhaps the action itself is better off being a class with a number of setter methods?

In other words, if your functions become long, or argument lists become long, it is often a sign that there are classes waiting to be discovered underneath your design.

Community
  • 1
  • 1
Adi Levin
  • 5,165
  • 1
  • 17
  • 26