Should I create functions/methods for packages/libraries that allow optional parameters to accept null?

In this example below, I set the 3rd and 4th parameter as null which will act as the default value.

myLibrary.myFunction(1, 7, null, null, true);

Or is this not a good way to go about creating functions for a package and therefore should not accept null as a parameter value.

myLibrary.myFunction(1, 7, false, 4, true);
  • MajorHavoc@programming.dev
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    18 days ago

    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.