My recommendation for APIs that have optional parameters is to have an optional last options
parameter, every property of which can be left unset. Makes it far clearer to the caller what those properties do.
this post was submitted on 15 Oct 2024
6 points (100.0% liked)
JavaScript
1954 readers
24 users here now
founded 1 year ago
MODERATORS
When null can be handled rationally, a library should accept it and do something reasonable.
When null makes no sense whatsoever, a library should error out if it receives null.
So it's up to your use case which is most appropriate.
The core principle is the program should stop executing the moment we 100% know that we're not going to accomplish what the user needs. In many cases, being passed 'null' tells us that.
In any other case, the program should pick a reasonable (default) option for the user and continue running.