in reply to Re^3: I hate nested loops
in thread I hate nested loops

I prefer the nested loops, because that makes it easier to change the value that is assigned. For instance:
for my $eth (@eths) { for my $net (@nets) { $netifaces{$eth}{$net} = func($eth, $net); } }
The structure remains the same, regardless whether the RHS of the assignment is a literal, or a more complex expression.

Or it can be easily extended to initialize more structures:

for my $eth (@eths) { for my $net (@nets) { $netifaces{$eth}{$net} = func($eth, $net); $something_else{$eth} += $net; } }

Replies are listed 'Best First'.
Re^5: I hate nested loops
by BrowserUk (Patriarch) on Aug 11, 2011 at 06:15 UTC
    I prefer the nested loops, because that makes it easier to change the value that is assigned.

    That is only a benefit if the initialisation value is likely to change to a per item initialisation value. But it isn't. Indeed, it is very, very rare that is required at all. And much rarer still that an algorithm will suddenly change from requiring bulk initialisation to per-item initialisation.

    And the effort involved in the change on those extremely rare occasions, is so trivial, that expending any effort -- even thought -- to trying to optimise it, is the very definition of premature optimisation; the woe in O'Woe; the pointless in contrary counter-argument.

    It's like carrying a gas mask with you everywhere you go. One day it might save your life -- but the odds are stacked so high against it, nobody does.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      No, it means I need only one idiom, regardless whether the RHS is a literal, a more complex expression, or whether I initialize one or more structures. Using one idiom instead of more improves readability, IMO.

      It's like having just one type of umbrella in the boot of your car instead of a whole bunch, each of them suitable as protection against a single type of rain.

        only one idiom

        But that is the absolute antithesis of what Perl (and all VHL languages) are all about.

        We could (and for a while did) program using just a single flow control idiom: goto. If reducing the number of idioms we used was a virtue, we'd still perform the initialisation like this:

        my @e = 'a'..'z'; my @n = 1..10; my $href; my $ie = 0; OUTER: my $e = $e[ $ie ]; my $in = 0; INNER: my $n = $n[ $in ]; $href->{ $e }{ $n } = 1; $in = $in + 1; if( $in < @n ) { goto INNER; } $ie = $ie + 1; if( $ie < @e ) { goto OUTER; }

        We don't any more for very good reasons.

        You've pointed out many times that the C-style for loop is far more flexible than the perlish for loop, but we have both and most people prefer the latter for most uses. (Albeit that some use it when it is less appropriate.)

        Using one idiom instead of more improves readability, IMO

        IMO, if you think about it a little more, you'll change your mind.

        By definition, the more general an idiom, the more situations it is applicable in. Which, also by definition, means the more closely you need to inspect it to understand which of those situations it is being used for in this case.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.