4
We3D
1y

We had a dispute @ work yesterday on this topic :

aproach 1 :

$res = getSomeData( [ 'filter_1' => 'str_1', 'filter_2' => 'str_2', ... 'limit' => 10 );

with

function getSomeData( $data )
{
...
}

VS

aproach 2 :

$res = getSomeData( 'str_1', 'str_2', ... 10 );

with

function getSomeData( $filter_1 = '', $filter_2 = 'default_str', ... $limit = 20 )
{
...
}

I had used 1-st b/c IMHO it's more dynamic and easy to maintain and call ( especially when there are many params, most of which not used on each call )

and my colleague didn't liked it w/h his main arguments that one can not easily see the needed params and no default vals for them ( the latest could be easily corrected in the function body ). I'm curious to read your opinions, arguments & contra arguments for each aproach

Comments
  • 1
    PHP? You can add tags to posts..
  • 4
    Are the filters really named filter_1 or do they have descriptive names?

    If they just count up and always are filled from the beginning I would lean towards the first one.

    But if they are named, like ”name”, ”size”… I would create a class with properties so you create a filter object and pass that.

    That way you can have defaults, only need to fill out the ones you need, order can be as you want and you can have named filters preprepared.
  • 0
    @ElectroArchiver

    It is PHP in the concrete case, but the principles are applicable in many languages
  • 0
    @Voxera

    That's an really nice alternative aproach, I like it. To answer your question : no, the names of the params are descriptive I just used this for potentially faster reading =]. One of the downsides w/h ver. 2 is when you not always use all of the params and you need to fill only the n-th 1 or 2. In that case you should know/count how much false ones to put before the one you need.
  • 3
    @We3D what voxera said.

    A simple DTO can solve this easily...

    Keep it simple - do not try to mix OOP into it, a plain old PHP object with public properties is sufficient.
  • 1
    @gintko Thanks for the detailed response and for the bonus. I'll definitely switch to 3rd way in the new project ( while is young and still moldable ) and make a proposal to the to the other 2 devs to make it default way for new projects. I'm still new in this company so wish me luck.
  • 1
    @highlight

    // DTO (aka "parameter object")

    final class DtoInPhp81

    {

    /** @param array<int, string> $bravo */

    public function __construct(

    public readonly bool $alpha,

    public readonly array $bravo,

    public readonly AnotherDto $delta,

    ){}

    }

    // PHP 8.2 will be released on December 8, 2022

    final readonly class DtoInPhp82

    {

    /** @param array<int, string> $bravo */

    public function __construct(

    public bool $alpha,

    public array $bravo,

    public AnotherDto $delta,

    ){}

    }
  • 0
    What does it do, what are these filters and what does the arrow operator do?

    So far neither is readable to me
  • 1
    @netikras

    PHP-array ("hash map"):

    - ['hello'] is the same as [0 => 'hello']

    - with custom key: ['some-key' => 'some-value']

    Accessing a value:

    - $array[0] results in 'hello'

    - $array['some-key'] results in 'some-value'
  • 0
    Is this perl ?
  • 0
  • 1
    Even in c# there are cases you can pass in a parameters array I don't like that personally I like strongly typed prototypes so when something changes it breaks the build

    That's my issue with things like js

    Sometimes someone changes something and you don't see it breaks till it breaks because it's buried

    At least in vs i can try to avoid this by checking all uses but if something is composing the call dynamically I might miss it and the boom the in prog dies
Add Comment