Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Unusual (and pointless?) idiom in old code

by ajmalton (Acolyte)
on Sep 22, 2011 at 11:18 UTC ( [id://927325]=perlquestion: print w/replies, xml ) Need Help??

ajmalton has asked for the wisdom of the Perl Monks concerning the following question:

Ok, so I'm maintaining some quirky old (2000-2010) perl code not written by me and I see a large number of list initializations of the form:

@foobar = splice(@foobar,@foobar)
Now as far as I know this is a pointless variation on
@foobar = ( )
Can any wise one think of _any_ reason why I shouldn't just replace all these weird splices with the literal empty list?

Replies are listed 'Best First'.
Re: Unusual (and pointless?) idiom in old code
by bart (Canon) on Sep 22, 2011 at 11:53 UTC
    The second parameter for splice, OFFSET, is called in scalar context, so @foobar just returns the number of elements. Using that count as the offset will indeed return an empty list.

    I think the original idea behind this idiom was to keep a number of elements, for example, 3, from the end of the array, and probably into a different variable, removing them from the original array:

    @trailing = splice(@foobar, @foobar-3);
    (which is the same as
    @trailing = splice(@foobar, -3);
    but that won't work for 0)
    which was later adapted to return nothing, and probably who didn't quite grasp how it worked.
Re: Unusual (and pointless?) idiom in old code
by chrestomanci (Priest) on Sep 22, 2011 at 11:52 UTC

    Do you know the author of the code you are trying to maintain? Do you have an estimate of their abilities as a perl programmer? What other languages did they code in?

    The reason I ask is because you often find strange idioms in perl scripts written by people who are used to writing in shell, C or another language, and because TIMTOWTDI and perl is tolerant of these idioms it works and the programmer leaves them in and gets used to writing their code that way. (For example, see node 927269 where the author looks to be used to writing C.)

    Perhaps your predecessor just did not know that you could clear an array with @foo = () and instead started using splice because they where familiar with it from JavaScript or whatever, and then got into the habit of doing it that way and never bothered to learn a better way. A lot of people learn just enough perl to hack something together that works, but don't bother learning anything more because their main job is something else.

Re: Unusual (and pointless?) idiom in old code
by kcott (Archbishop) on Sep 22, 2011 at 11:57 UTC

    In list context (as you have here), no elements are removed so none are returned: resulting in an empty list. This holds true for splice() - Perl v5.14 and back as far as splice() - Perl v5.8 (maybe earlier versions as well).

    While possibly considered pedantic, you show no terminating semicolon. Look closely at surrounding code: is this the entire statement or just part of a larger construct?

    Update: s/split/splice/ (2 instances) - thanks AnomalousMonk.

    -- Ken

      Thanks to all. I am happy that there is no wisely hidden reason for having done this. There is other internal evidence that probably the programmer had some other language in mind. Several old perl hands have commented to me that they never ever saw this oddness before. It makes me feel better that _I_ never have, either. I'm going to replace them all with ()'s. Then comes the hard part, getting rid of all the global variables I've inherited.
Re: Unusual (and pointless?) idiom in old code
by jwkrahn (Abbot) on Sep 22, 2011 at 11:54 UTC
    @foobar = splice(@foobar,@foobar)

    Yes, it is both unusual and pointless.    In my many years of Perl programming I have never seen anyone do that.

    If you want to use splice to empty an array then:

    splice @foobar

    Would suffice to do the job.

Re: Unusual (and pointless?) idiom in old code
by TomDLux (Vicar) on Sep 22, 2011 at 17:24 UTC

    Is this in a loop, eliminating values from the previous iteration? If not, you don't need to assign anything at all. If it IS in a loop, try moving the declaration into the loop, so that once again you don't need to initialize, because limited scope causes the variable to be created, new and empty, once each time around.

    I have similar problems, with code written 12 years ago by interns borrowed from the mainframe department and not supervised or peer-reviewed.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://927325]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found