in reply to Re^9: Thread on Joel on software forum : "I hate Perl programmers."
in thread Thread on Joel on software forum : "I hate Perl programmers."

FWIW, coming in such a long time after the party:

It has become my “standard conversion” for where I want to type

Of course, in this particular case, the correct, optimal solution is trivial:

my @grid = ( '01' .. '12' ) x 12;

In the more general case, I prefer another way, because it maintains symmetry:

my @grid = map @$_, ( [ '01' .. '12' ] ) x 12;

And to this:

Beyond the thinko, the problem I have with the for/push method is that the main objective – that of initialising the array @grid – gets lost in the noise of the construction.

I say bingo. That’s the reason I use Perl to begin with, rather than C. (“If I had a nickel for every time I wrote for( i = 0; i < LEN ; ++i ), …”)

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^11: Thread on Joel on software forum : "I hate Perl programmers."
by BrowserUk (Patriarch) on Nov 30, 2005 at 17:53 UTC
    Of course, in this particular case, the correct, optimal solution is trivial: my @grid = ( '01' .. '12' ) x 12;

    Actually, no. That builds a single dimension array with 144 elements, not a 2 dimension array of 12 elements with 12 elements each.

    In the more general case, I prefer another way, because it maintains symmetry: my @grid = map @$_, ( [ '01' .. '12' ] ) x 12;

    Let's see.

    1. You build an anonymous array of twelve elements;
    2. replicate the reference to that 12 times into a list;
    3. Pass that list to map(*), where you dereference that reference 12 times to produce a list of 144 scalars which get assigned to the array you are initialising.

    And after all that, for the sake of "symmetry", you still end up with the wrong thing?

    I just read that back and it sounds sarcastic, but it is not intended to be. I relish the opportunity to explain and defend my choices of coding style that so many people apparently find...shall we say eccentric :)

    We may never agree on this, but what you are trying to avoid  1 .. 12; in favour of  x 12;. Ie. You would like to avoid the generation of a list that doesn't get used except for the side-effect it produces.

    But, in your code, you are producing a list of 12 references that only get used for the side-effect it produces! What is the difference between a list of 12 integers and a list of 12 references? Besides the memory consumed.

    Whilst I agree that if/when the language supportes an x-like operator that executes it's left argument 12 times rather than executing it once and replicating it twelve times, I would use that in preference.

    But until that operator becomes available, all those shinnanagins to avoid generating a list of integers that is only used for the side-effect of the iterations it causes, to me, introduces more obfusication, mis-direction and potential for misunderstanding and errors, than using the list for it's side-effects.

    (*)Using the non-block form which even TheDamian agrees with me (in PBP) should be avoided.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Sorry, I misread the code. I suppose it could be rewritten like so:

      my @grid = map [ @$_ ], ( [ '01' .. '12' ] ) x 12;

      but yeah, that’s past the tipping point up to which it might make more sense than

      my @grip = map [ '01' .. '12' ], 1 .. 12;

      What is the difference between a list of 12 integers and a list of 12 references? Besides the memory consumed.

      The difference is that the value that comes from the list is used by the map expression. With your approach, only the list length matters, while the list elements are demoted to dummy constants, so 1 .. 12 is arbitrary and could just as well be ( undef ) x 12 or split //, ' ' x 12 or whatever. It basically offends my sensibilities to have something in the code which amounts to nothing but an ornament. Not enough that I’d take the silly approach outlined in the first snippet above, though. I just try to avoid it; but the line is blurry.

      Makeshifts last the longest.