in reply to Re: The cognitive load of generated code
in thread The cognitive load of generated code

Worse, it's shortened to empty list. Returning an empty list from a sub expected to return a scalar can have very weird effects.

length $f <= $m is awful too. foo $f <= $m normally means foo( $f <= $m ), so seeing length $f <= $m raises red flags, adding to cognitive load. (It's valid, but only because length breaks operator precedence.)

Replies are listed 'Best First'.
Re^3: The cognitive load of generated code
by Anonymous Monk on May 19, 2026 at 07:13 UTC
    I see what you mean. The lack of parentheses on length and especially int is ambiguous. I remember having to test int once to see if something like int $a / 2 means int($a) / 2 or int($a / 2) and since it meant the latter I adopted that bad habit. I could probably write a book: Perl Worst Practices! Thanks for the clues.
    sub shortencentered { my $f = defined $_[0] ? shift : return ''; # be less weird :-) my $m = shift || 42; return $f if length($f) <= $m; my $a = $m - 3; my $s = int($a / 2); my $e = $a - $s; return substr($f, 0, $s) . '...' . substr($f, -$e) }