Missing in PHP7: Named parameters

This is the second post in my Missing in PHP7 series. The previous one is about function references.

Readability of code is very important, and this is most certainly not readable:

You can make some guesses, and in a lot of cases you’ll be passing in named variables.

It’s even possible to create named variables where you would otherwise have none.

This gains you naming of the arguments, at the cost of boilerplate, and worse, the cost of introducing local state. I’d hate to see such state be introduced even if the function did nothing else, and it only gets worse as the complexity and other state of the function increases.

Another way to improve the situation a little is by making the boolean flag more readable via usage of constants.

Of course, long argument lists and boolean flags are both things you want to avoid to begin with and are rarely needed when designing your functions well. It’s however not possible to avoid all argument lists. Using the cat pictures example, the limit and offset parameters can not be removed.

You can create a value object, though this just moves the problem to the constructor of said value object, and unless you create weird function specific value objects, this is only a partial move.

An naive solution to this problem is to have a single parameter that is an associative array.

The result of this is catastrophe. You are no longer able to see which parameters are required and supported from the function signature, or what their types are. You need to look at the implementation, where you are also forced to do a lot of checks before doing the actual job of the function. So many checks that they probably deserve their own function. Yay, recursion! Furthermore, static code analysis gets thrown out of the window, making it next to impossible for tools to assist with renaming a parameter or finding its usages.

What I’d like to be able to do is naming parameters with support from the language, as you can do in Python.

To me this is not a small problem. Functions with more than 3 arguments might be rare in a well designed codebase, though even with fewer arguments readability suffers greatly. And there is an exception to the no more than 3 parameters per function: constructors. Unless you are being rather extreme and following Object Calisthenics, you’ll have plenty of constructors where the lack of named parameters gets extra annoying. This is especially true for value objects, though that is a topic for another post.

6 Comments on “Missing in PHP7: Named parameters

  1. Pingback: Missing in PHP7 – Entropy Wins

  2. Pingback: Missing in PHP7: Value objects – Entropy Wins

  3. Simply do not use triadic functions/methods or worse .. if your method needs to deal with more than 2 max 3 parameters it is doing to much anyway …

    • Hi Ilja. My post actually mentions that, so I recommend you read the entire thing to see why the lack of named parameters is still a problem.

  4. Pingback: Clean Architecture: UseCase tests – Entropy Wins

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.