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

Re: trim vs trimmed revisited

Thread Previous | Thread Next
From:
Dan Book
Date:
February 25, 2022 16:07
Subject:
Re: trim vs trimmed revisited
Message ID:
CABMkAVUN4bop1HcW8FQLXGSs9pceKjfN00fViLpO_Xrg9qWZDg@mail.gmail.com
On Fri, Feb 25, 2022 at 11:01 AM demerphq <demerphq@gmail.com> wrote:

> On Fri, 25 Feb 2022 at 16:09, Paul "LeoNerd" Evans <leonerd@leonerd.org.uk>
> wrote:
>
>> 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.
>>
>
> One of them for sure.
>
>
>>
>> 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.
>>
>
> I agree with all of this. Various context related issues with sort
> especially come to mind.
>
> But i'd like to note that in previous discussion it has not been clearly
> stated as  "*context dependent* in place edits are bad" it has simply been
> presented that "in place edits are bad". At least I don't recall *that*
> component of the objection being made clear at all.
>

My view is that in place edits are not bad, but not a sufficient
implementation of this function - like chomp, it would then be surprisingly
inconsistent with other parts of the language, and awkward to use in
functional program flow (e.g. map, grep, assignment chains, function
calls). Context dependence was an alternative to that which is bad for the
other reasons discussed here. The peephole optimization for $str = trim
$str isn't perfect but seems sufficient as an alternative.

-Dan

>

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