6

Named arguments and open source projects

 3 years ago
source link: https://www.stitcher.io/blog/named-arguments-and-variadic-functions
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
« back — written by Brent on August 26, 2021

Named arguments and open source projects

There are a few well known open source maintainers within the PHP community against the use of named arguments, citing maintenance overhead and backwards compatibility problems as reasons not wanting to use them.

I want to nuance those arguments a little bit.

# What's causing backwards compatibility problems?

The main fear is that supporting named arguments in, for example, a framework or open source package will increase the risk of breaking changes.

Imagine a package or framework exposing this class:

class QueryBuilder
{
    public function join(
        string $table, 
        string $leftColumn, 
        string $rightColumn, 
        string $type
    ) { /* … */ }
}

The problem with named arguments is that if users call this function with them, the framework now needs to treat parameter name changes as possible breaking ones:

$query->join(
    type: 'left',
    table: 'table_b',
    leftColumn: 'table_a.id',
    rightColumn: 'table_b.table_a_id',
)

If the framework wants to rename leftColumn and rightColumn to simply left and right, the above code — userland code — would break.

Here's the thing: no framework or package can prevent users from using named arguments, there simply isn't a way to disallow them. So either, as an open source maintainer, you:

  • treat argument name changes as breaking as soon as you support PHP 8;
  • ask users not to use named arguments; or
  • let users deal with those breaking changes themselves, and don't worry about it.

Being an open source maintainer myself: I choose option three. First of all: argument name changes only rarely happen; and second: I trust my users to be professional developers and know the consequences of using named arguments. They are smart grown ups, it's their responsibility.

So in summary for this first part: there's nothing the framework can do to prevent this kind of backwards compatibility issues besides making a note in the README on how they deal with argument name changes. Be consistent with whatever policy you choose, and you're fine.

I've just launched a 10-day popup newsletter called The Road to PHP 8.1. For the next 10 days, you'll receive one email covering a new and exiting feature of PHP 8.1; afterwards you'll be automatically unsubscribed, so no spam or followup. Subscribe now!

# Named arguments as a cleaner syntax to deal with array data

The second way named arguments can be used, is in combination with variadic functions, essentially becoming a — in my opinion cleaner — shorthand for passing arrays of data:

$user = User::create(
    name: 'Brent',
    email: '[email protected]',
    company_id: 1,  
);

This is possible thanks to named arguments playing well together with variadic functions:

class User
{
    public function create(...$props) { /* … */ }
}

Passing a named argument list into this variadic create function will result in an array like this:

[
    'name' => 'Brent',
    'email' => '[email protected]',
    'company_id' => 1,  
]

Rewriting the above example without named arguments but arrays instead, would look like this:

$user = User::create([
    'name' => 'Brent',
    'email' => '[email protected]',
    'company_id' => 1,  
]);

I know which one of these two approaches I prefer. Disclaimer: it's the one that has better syntax highlighting and is shorter to write.

Here's the kicker: there isn't any possibility for breaking changes, because there aren't any hard coded argument names to begin with!


We really need to be more thoughtful about claiming that we cannot support named arguments in our open source packages because of backwards compatibility issues. In the first case there's nothing you can do either way, and the second case doesn't pose any danger of breaking changes.

Don't you agree? Send me an email or tweet and we can further discuss it. I'm open to be proven wrong.

Noticed a tpyo? You can submit a PR to fix it.

If you want to stay up to date about what's happening on this blog, you can follow me on Twitter or subscribe to my newsletter:

Email


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK