If two data structures are related, make that relationship OBVIOUS.
I agree with that.
Parallel data structures are not obviously related.
It seems obvious to me that if you see them assigned
together, they're related. I did say it was a matter of
style, however, and I expected some people to have a
strong preference for the nested structures. I do use
the nested structures in some cases, when what I want to
do is a little more complex, or if there are multiple
levels of nesting, or some other good reason. And I gave
the example of using nested structures first. Don't read
more into my statement about parallel structures than is
there.
In fact, it's a maintenance nightmare.
Let's set up a thought experiment.
Thought experiments can lead you to conclude that
a heavier object will always fall faster than a
lighter one. (They can also be useful, but you
have to take them cum grano salis.)
I am your maintenance programmer.
Oooh, oooh, can I imagine that I named all my
variables with single characters and used recursive
nested evals wherever possible? ;-)
I come along and are
told there is a bug in the fubar() function and I need
to fix it in 24 hrs. I go and realize that I need this
value to make it right. I don't know that the value is
in this fourth data structure. But, I need to fix
fubar() right now. So, I add some crazy structure to
get that fourth value into fubar(). The code is now
worse.
The code will always be worse when someone who is not
familiar with the code attempts to fix something
right now without understanding how it works. No
amount of wonderful data structure will change that.
(This is not an argument for bad data structures;
I'm merely pointing out that no data structure can
prevent the scenerio you describe.)
Furthermore, unless I'm missing something, there's
nothing magic about the syntax of nesting that will
alert the unfamiliar programmer to the existence of
more data than is being used in the piece of code
he's viewing. A simplistic example...
sub foobar {
my ($object, $result);
foreach $object (@_) {
$result .= "Title:\t" . $$object{title} ."\n"
. "Author:\t" . $$object{author} ."\n"
. "-------------------------------\n";
}
return $result;
}
Will the programmer know to look in $$object{ISBN}
for the piece of data he needs to fulfill the change
request? Maybe, but if so it's not any more obvious
than (with parallel structures) looking in $isbn{$key}.
If he reads through the well-commented code, he'll
find it either way.
Of course, if the code is more complex and has a
larger number of fields, then the nested structure
can be traversed more efficiently, avoiding the bug
in the first place...
sub foobar {
my ($object, $result, $f);
foreach $object (@_) {
foreach $f (sort @fields) {
$result .= "$f:\t" . $$object{$f} ."\n";
}
$result .= "-------------------------------\n";
}
return $result;
}
But the original poster is talking about what is
currently a single hash storing a single value for
each key, and I was suggesting also storing the
unstringified reference used to create the hash
key. That's a total of two fields: not complex
enough to really need the nested structure, IMO.
Yes, the nested structure will solve the problem
nicely, but the parallel structure will also work.
Note that I'm not saying that parallel structures are
better, or even that they're as good in every case;
I only said that which you use is a matter of style.
The program will get the same result either way.
sub H{$_=shift;while($_){$c=0;while(s/^2//){$c++;}s/^4//;$
v.=(' ','|','_',"\n",'\\','/')[$c]}$v}sub A{$_=shift;while
($_){$d=hex chop;for(1..4){$pl.=($d%2)?4:2;$d>>=1}}$pl}$H=
"16f6da116f6db14b4b0906c4f324";print H(A($H)) # -- jonadab
|