Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Help me understand this idiomatic hash from array code

by sundialsvc4 (Abbot)
on Jul 11, 2017 at 20:01 UTC ( #1194862=note: print w/replies, xml ) Need Help??


in reply to Help me understand this idiomatic hash from array code

In my opinion, the first code example is unnecssarily complicated – notice that it “sent you to Camel book, to no avail.”   It all comes down to this, and could have been stated in just this way: %fields = { id => 0, name => 1, age => 2};   If I did not know how many elements there might be, I would once again use a simple-and-obvious for/foreach-loop structure to construct the array content.   The key word, to me, is “obvious,” which of course also helps future maintainability.   (If When a new element needs to be added, the modification should be minimal and easy.)

In the second code example, I probably would not use $test.   Maybe.   Maybe, I would prefer something like this:

if ( #Brian next to Dana abs($positions{Brian} - $positions{Dana}) == 1 #Dana not next to Evan && abs($positions{Dana} - $positions{Evan}) > 1 #Carlos on Dana's right && $positions{Carlos} - $positions{Dana} == 1 #one seat between Brian and Farra && abs($positions{Brian} - $positions{Farra}) == 2; ) { print "@perm\n"; }

However, I do not feel strongly about this one, and could easily be persuaded that your way is better.   (If so, I probably would use a more-descriptive variable name.)   For instance, if we can anticipate someday that the final decision might one day need to be more complicated, or multi-stage or what have you, then I would reverse my position and agree that the use of a separate variable to hold the result of the decision is better.   There is no technical concern here either way.   It’s a question of anticipating future maintenance changes that might one day need to occur in this same area.

I appreciate that you have written the test with associated comments which describe what each otherwise-confusing test is meant to do, and it does indeed appear that the code matches them.   (Note that, when you do this sort of thing, you must check for correctness v-e-r-y carefully, because future bug-sleuths will naturally tend to read the human comment and not verify that the code in fact matches it.)

Replies are listed 'Best First'.
Re^2: Help me understand this idiomatic hash from array code
by 1nickt (Prior) on Jul 11, 2017 at 20:33 UTC

    "could have been stated in just this way: %fields = { id => 0, name => 1, age => 2};"

    Assign a key/value pair to a hash:

    %hash = ( key => "value" );

    Assign a hashref as a hash key with no value:

    %hash = { key => "value" };

    perl -MData::Dumper -wE 'my %fields = { id => 0, name => 1, age => 2}; + say Dumper \%fields;' Reference found where even-sized list expected at -e line 1. $VAR1 = { 'HASH(0x7f9d5a804850)' => undef };
    Just sayin'.


    The way forward always starts with a minimal test.
Re^2: Help me understand this idiomatic hash from array code
by shmem (Chancellor) on Jul 12, 2017 at 08:47 UTC
    could have been stated in just this way: %fields = { id => 0, name => 1, age => 2};
    ...
    you must check for correctness
    v-e-r-y carefully

    People are constantly downvoting your posts? That's why.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re^2: Help me understand this idiomatic hash from array code
by Anonymous Monk on Jul 11, 2017 at 20:26 UTC

    In my opinion, the first code example is unnecssarily complicated – notice that it “sent you to Camel book, to no avail.” It all comes down to this, and could have been stated in just this way: %fields = { id => 0, name => 1, age => 2};

    It all comes down to, the information the OP already provided?

    How can you be confused about what the OP knows when you're actively copy/pasting what he knows?

    Calling a hash slice "unnecssarily complicated" is very silly and very telling

    because future bug-sleuths will naturally tend to read the human comment and not verify that the code in fact matches it.)

    Thats what non-programmers naturally do because they can't read code

    Programmers would ignore the comments and read the code

Re^2: Help me understand this idiomatic hash from array code
by adhrain (Acolyte) on Jul 12, 2017 at 08:09 UTC

    I was going to use a foreach loop, but the idiomatic code seemed (and now that I understand it, IS) better for maintainability, given that it hids indexing and is very concise

    Obviously I needed to understand its workings before employing it en masse; now that it is clear to me I can say that these are the kind of things that make me love perl and I have a new nifty piece of syntax in my arsenal.

    As for the $test variable, I agree that I could've chosen a better name (maybe $constraints like in the python original code). And given that it is just a one-off script made for fun, it is overkill to use a variable to hold the boolean, but I was influenced by habit (keep complex boolean conditions outside of the if) and by the over complex python code, where they actually create a function for every constraint and then put them in a tuple to map them to each permutation with all().

    Thank you for your observations.

      Dunno ... I still like to use loops, and usually consider them to be more maintainable, maybe because they are not so “concise.”   If all that I need to do is to, say, add another key to a hash that I am inserting into an array, I like to be able to just add a new line of source, to see at a glance(!) that it is correct and that it won’t break anything else.   Likewise, if “indexing” is part of it, I don’t want to hide that – I want to make it very, very obvious.   However, these are just matters of opinion.

      What I especially want to avoid, is precisely the thing that has bitten me (or members of my teams) in the asterisk ... or that has turned-out to be the bug in someone else’s old routine:   someone, sometime in the past, made an innocent-seeming change to a piece of “clever” code (which they of course did not write), failed to see that they had not been quite “clever” enough ... they failed to update another “clever” thing that was nearby, and which was broken by their change, and failed to notice it when they committed their change and closed the ticket.   Always remember that the next person to modify your code likely-as-not will not be you.   (“Too bad about that bread truck, pal.   I have no idea what it was doing on the sidewalk.”)   I don’t need to economize on source-code lines, especially not at the expense of “stupid-obvious clarity.”   Computer time is free.   Human time is not, and errors are as expensive as ever.

      Another thing that scares me, and which is directly related to “terse cleverness,” is when a new git commit, which is intended only to make one small change to a routine, necessarily(!) tears the block-of-code apart and rebuilds it.   Looking at the patch in code-review, I see that the change really is the minimal code-change necessary to do the job.   But now, we have to write a test to re-test everything that it does (whether related to this change or not) in order to prove that it has not regressed.   (And sometimes, we discover that it had.)   Which, too me, is just too much business risk.   That is usually when I direct the team member, “Look, let’s just rewrite this little bit of code now ... let’s just replace it with an easy foreach loop.   So we won’t have to write a massive new test the next time we have to ‘just tweak’ it.”

Re^2: Help me understand this idiomatic hash from array code
by Anonymous Monk on Jul 11, 2017 at 21:06 UTC
    > correctness v-e-r-y carefully,

    Almost everything you say is incorrect. You're full of shit

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2017-09-25 17:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (286 votes). Check out past polls.

    Notices?