develooper Front page | perl.perl5.porters | Postings from February 2022

Re: trim vs trimmed revisited

Thread Previous | Thread Next
From:
Paul "LeoNerd" Evans
Date:
February 25, 2022 15:09
Subject:
Re: trim vs trimmed revisited
Message ID:
20220225150858.188fad61@shy.leonerd.org.uk
On Fri, 25 Feb 2022 01:24:00 -0600
David Nicol <davidnicol@gmail.com> wrote:

> IMO trim should operate in place in void context and return the
> substring in nonvoid context. Optimizing

Functions that do materially different things depending on context have
been a constant annoyance to newbies and experts alike, ever since the
language was first created. They are commonly agreed to be one of the
worst mis-features of the entire language.

What does this do?

  sub shortname {
     my $self = shift;
     return trim( $self->{fullname} );
  }

Does it mutate the object instance, or not? Perhaps it depends on the
caller.

Maybe far away in a script somewhere many files away:

  printf "My shortname is %s\n", $user->shortname;

  $user->shortname;
  printf "Fullname is now %s\n", $user->{fullname};

Oops. We just broke the object.

This sort of "spooky action-at-a-distance" makes it hard to build
robust code and reason about its behaviour in small modular pieces.

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About