Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Basic Coding Tips: Parsimonious Parameterization

by brian_d_foy (Abbot)
on Jan 29, 2005 at 03:30 UTC ( #426174=note: print w/replies, xml ) Need Help??

in reply to Basic Coding Tips: Parsimonious Parameterization

I call this the "Use what you already know" concept. I once saw in a code review a bit of code from someone you didn't trust arrays to know how many elements were in them. It looked a lot like:

my @array = (); for( my $i = 0; $i < 10; $i++) { $count++; push @array, $i; } print "There are $count elements\n";

I've seen the same thing for hashes too, since some people don't read the perlfunc entry for keys().

I'm still waiting to see some Perl code on The Daily WTF?

brian d foy <>

Replies are listed 'Best First'.
Re^2: Basic Coding Tips: Parsimonious Parameterization
by BUU (Prior) on Jan 29, 2005 at 04:18 UTC
    Well, if you want a "WTF", I found this gem on cpan a while ago:
    #merges the elements of two or more arrays together so that the values + of one # are appended to the end of the previous one sub array_merge{ my(@array1, @array2) = @_; foreach my $element (@array2){ @array1 = (@array1, $element); }#foreach return @array1; }#array_merge

      I have to admit - he couldn't do it much faster than that and still have a subroutine...

      (Anyone else notice that @array2 will always be empty since @array1 will get all of @_? Thus, the foreach will always be skipped!)

        Yes :) But it'd works this way:
        sub array_merge(\@\@){ my($array1, $array2) = @_; foreach my $element (@$array2){ @$array1 = (@$array1, $element); }#foreach return @$array1; }#array_merge my @t=array_merge(@liste1, @liste2);
Re^2: Basic Coding Tips: Parsimonious Parameterization
by Aristotle (Chancellor) on Jan 29, 2005 at 03:49 UTC

    There have been a number of Perl submissions there, though not as many as one might expect. Check out The Guy Who Invented Arrays for an example. I guess VB has captured the majority of the crop of casual programmers who are prone to pretzel logic, where ten years ago a much larger percentage of those would be using Perl.

    Makeshifts last the longest.

      First, thanks for the link, thats an interesting site. :-) But i have to quibble a little with this particular example. Obviously the code posted in that link appears hokey. Not using pack/substr or vec for the index string strikes me as a little odd for instance. But the overall strategy represented there is not IMO that crazy. Consider that if you have large numbers of strings such a strategy can greatly reduce the overhead of storing it in perl. Each SV in perl represents a certain amount of space, for the sake of the discussion lets say 20 bytes, versus the 2 - 8 required by a representationg akin to the one mentioned. So if we are storing large numbers of strings this overhead can be quite burdensome. Also, by using such an approach you can grab a large chunk of memory at a single go instead of doing a large number of small allocations. (Its interesting (if a little obvious) to consider that if you have 100k null terminated strings you are using 100k to simply store the end of string point :-)

      Anyway i didnt think such comments were worth posting on the site you linked to, but I do think its worth mentioning here.


        Oh, I know well enough. In the case of that example though, the coder did so many other hokey things at once that it doesn't seem particularly likely that this was well-reasoned conscious choice.

        Makeshifts last the longest.

Re^2: Basic Coding Tips: Parsimonious Parameterization
by vek (Prior) on Jan 29, 2005 at 06:28 UTC

    No Perl? Oh but there is. Enjoy the hilarity as we observe a cunning ploy to avoid using length :-)

    -- vek --
Re^2: Basic Coding Tips: Parsimonious Parameterization
by greenFox (Vicar) on Jan 31, 2005 at 07:17 UTC

    Seen recently in a production script distributed around the company I work for-

    my $var=1; while ($var == 1){ # do a bunch of stuff and never touch $var # exit if (some condition); }

    while(1) must have been too difficult :)

    Do not seek to follow in the footsteps of the wise. Seek what they sought. -Basho

      Or, depending on structure, even

      do { # ... } until $condition;

      Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://426174]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-05-23 13:57 GMT
Find Nodes?
    Voting Booth?