Michael Haufe (TNO) wrote:
> Scott Sauyet wrote:
>> I'm guessing that what Angus was suggesting with
>
>> | Michael, If you've gone that far why not just have Person have a
>> | getZip() method to encapsulate the null checking?
>
>> is the same thing I feel:  you're trying to cram a Java/C# OO idiom
>> into Javascript which more naturally deals with very dynamic data
>> structures.
>
> Organizing data is not some language dependent idiom.

However the manner in which you choose to organize it is strongly
influenced by your choice of language.

> As you or the OP
> or anyone else creates something beyond trivial code you mentally have
> some notion of types and contracts in your mind.

But the contracts I consider when programming in Java are very
different from those I use when programming in COBOL, or when
programming in LISP.  When I program in Javascript, I don't usually
think in terms of classes of objects, but in one-off objects that may
or may not have certain properties.  One of the great things about the
language is the ability to add and remove arbitrary properties of
objects.

>Its wiser to make
> this explicit in your code using the features of the language than to
> play a defensive programming game that is unnecessarily slow and
> potentially verbose. There is no need for null/undefined checking on
> the level being advertised here if the data structures were organized
> in some manner instead of being thrown in some semblance of property/
> method bags to interrogate on every use.  

You're right, if you're trying to code as though you were in a
statically-typed language.  JS certainly allows you to code in that
manner.  But IMHO that ignores the strengths of the language.

> If by the definition of
> dynamic people think it means its super 1337 cool and JavaScripty to
> say:
>
> (person2||{}).address||{}).zip || "no zip"
>
> on every single member access, I think you are quite mistaken. Common
> practice and best practice are two different things.

But unless you're quite certain that no code has done the equivalent
of

    delete person2.address,

you still need something like that to ensure your code won't fail
horribly.

The point is simply that JS is a class-free language.  You can
simulate classes to some degree, but it's never an exact match with
what's available in class-based languages.

  -- Scott

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to