> On 11 Aug 2022, at 18:10, Rowan Tommins <rowan.coll...@gmail.com> wrote:
>
> On 11/08/2022 10:03, Alex Wells wrote:
>> You could argue the problem is that all of these are single-liners, so here
>> are the same examples, but multiline formatted:
>
>
> When people talk about avoiding one-liners, they're not just talking about
> whitespace, but other refactoring. For example, introducing intermediate
> variables:
>
> $fileNameComponents = explode('_', $file->getName() );
> $slicedComponents = array_slice($fileNameComponents, 4);
> // (most likely there's a better variable name, which would explain *why*
> we're ignoring those components)
> $className = Str::studly( implode('_', $slicedComponents) );
>
> Or adding additional helper functions:
>
> $fileNameComponents = explode('_', $file->getName() );
> $slicedComponents = array_slice($fileNameComponents, 4);
> $className = Str::studlyFromArray( $slicedComponents );
>
> Or even, since "Str::studly" probably calls explode() internally anyway:
>
> $className = Str::studlyWithSlice( $file->getName(), 4 );
>
I agree that this is a solution for many cases. However, the goal of the
comparison was to show reduced cognitive complexity, which I don’t believe is
what was achieved by splitting a simple expression into mulitple lines with
multiple variables. It gets progressively harder to “scan through” if you’re
just trying to get a global picture of what’s happening and not specifically
understand why these lines exist and what they’re doing. Sometimes you just
need a neat one-liner and that’s enough :)
Besides, variables are statements, making these a statement list, not an
expression, meaning they can’t be used as one in an arrow function or a throw
expression.
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php