On 16/02/11 22:40, Angus Croll wrote:
> What worries me is the unwanted side effects. 
> 
> console.log(d.foo.bar.bam) and maybe you just created 3 new objects

That's a legitimate concern. There is at least one language (that I know
of) which has this feature, and in my experience it's proven to be more
of a hindrance than a help. Perl will automatically create all the
intermediary objects (hashes, actually) in an assignment like this:

  my $foo = {hey => {ho => {lets => "go"}}};

This is called "autovivification" in Perl-speak, and it's intended to
make life easier by reducing the amount of typing (same thing we're
talking about here). Unfortunately, it will also do this if you just try
to read a value:

  my $bar = {};
  if ($bar->{hey}{ho}{lets}{go}) ...

- and bam!, $bar is now -

  {
    'hey' => {
      'ho' => {
        'lets' => {}
      }
    }
  };

IMHO, this behavior might make things easier when you're going to assign
values, but on the flip side, it can and often will create unnecessary
objects (hashes) when you only wanted to read a value. This can easily
happen in situations where a module maps a tree (like XML) to a hash of
hashes, or similar structures.

To avoid autovivification in Perl, you have to use

  if (defined $bar->{hey} && defined $bar->{hey}{ho} ... etc

Wikipedia lists autovivification as a "distinguishing feature" of Perl,
so maybe there aren't that many other languages which do this. Python's
"defaultdict" is also mentioned, but it's not quite the same thing.


regards,
stefan



PS. I still love Perl, weird as it is :)

-- 
LOAD"Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn!",8,1
RUN!

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to