PHP: Named arguments are your friends
It’s the developers who are not
I stumbled upon an article in Stitcher.io that, in a few words, declare that developers should be aware that Named Arguments will break things. That’s quite weird when you consider they fix more than what they “break”.
The reasoning behind calling Named Arguments as “breaking” is because one single name change makes the function or method unusable, only if the developer is using named parameter.
There is no sin if you are using the coding language as intended, but is one if you go against it. Everyone is welcome to make their own language like Facebook did when they branched onto HHVM an Hack, but that’s one extreme.
Since they will be widely available starting this winter with PHP 8.1, there is no reason to not use them. The thing is, trying to point the blame on the developer is entirely wrong:
(…) 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.
Such statements is what makes the PHP scene a clown car for external developers. Let me rephrase the above in other words:
I reserve the right to break your app and I couldn’t care less.
From now on, changing the name of the argument in your package becomes a major version increase. It’s the difference between going from 2.0.3
to 3.0.0
because the argument $mandatory
is now $required
. Things like SemVer is what protect us from such stupid ass decisions like going from 2.0.3
to 2.0.4
, and breaking a project.
There are ramifications for that kind of nonsense. Developers must stop working and go fix whatever is broken, but that’s the tip of the iceberg. Some apps work with very important people on very important places, and in some countries a simple “Sorry” is met with a compensation or a lawsuit. I just pray it’s not Friday.
Going back to the point, there is little reason on changing parameters names on function and methods. Naming the variable that will hold a given value is not rocket science if you consider it has to be descriptive, like “date”, “user”, “password” or “amounts”. You can even use variadic arguments if you need some kind of flexibility.
You lost the election, so move on
There are things in time that are life changing, and Named Parameters were one that happened for PHP more than a year ago. It’s seems too late to whine now. PHP is not a democracy, but everyone is welcome to have the power to vote by becoming an active maintainer. We, normal developers, are the ones that are on the other end of the stick. We are not evangelists, but mercenaries.
So, now that Named Arguments are months away to be widely supported for all platforms, as PHP 7.4 will be deprecated at the end of the year, it’s time to assume the responsibility as a developer and take special care on the parameters names in function and methods. Taking care of breaking changes is part of your job.
If you’re a package author, or framework maintainer, start to take into account that developers using your code will use Named Arguments at some point. Now you have to be sure that these will play nice with your code, so making a fuss about them its being one and a half year late to the party.
So? Use Named Arguments. If there is an issue, call the developer. If is not willing to fix it and puts the blame on you, fork and forget him.
You’ll never be wrong for using the language as intended.