SV: [thelist] JS: global vars...

Marcus Andersson marcan at home.se
Tue Sep 2 15:12:42 CDT 2003


Okay, I'm an object geek.

If you have a lot of stuff going on with many functions working on the
same data there is no question that you should use objects and classes
to encapsulate your data and behaviour. You get many big advantages:
- Easier to read and understand -> easier to let someone else in on the
code. It's often also easier to find things
- The code you write mostly gets more robust -> less bug fixing ->
shorter time to delivery
- It gets easier to add new functionality (often with a little
refactoring, but it's much more easy to refactor object oriented systems
than procedural)
- If you've done your encapsulation right it's much easier to change
behaviour without affecting users of the class
- You might get off with less lines of code (since it's much easier to
reuse)
- ...

All of these reasons gives code that is more maintainable, more stable
and it (probably) takes _shorter_ time to develop (if you build a system
with lots of different data and behaviour that is and you make a bit of
thinking up front). And isn't that just what our customers want: higher
quality (the stable and maintainable things) and (perhaps most
important) cheaper (due to shorter development time).

And, might I add, it's really easy to build OO system in Javascript if
you just maintain some dicipline, just don't abuse some of the features
of the languge. I'm in the middle of building a huge system of
intermingled XML/XSLT/JavaScript/DHTML and it would have been impossible
to handle if it wasn't OO.

Just my 5 cents

/Marcus

-----Ursprungligt meddelande-----
Från: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] För Tom Dell'Aringa
Skickat: den 2 september 2003 19:51
Till: thelist at lists.evolt.org
Ämne: [thelist] JS: global vars...


Greets,

I've been working on this monster of an app where I am dealing with
quite of bit of data on a page - roughly 20 kinds of data say on one
page with all different kinds of values. So you can imagine I have a
whole library of functions to deal with the data.

Quite often I have a case where one function knows about some piece of
information that another function must access, yet they cannot talk
directly - I can't send it as a parameter.

In these cases I have been using the (sometimes called evil) global
variable. That way the 2nd function can read the data it needs.

So! My question is what are the cons of this approach? The bonus is that
it makes it very easy for me to read/write the values.

Second, what are the alternatives? Should I be encapsulating such data
in objects? This of course would take a bit more development time but
might be a better solution.

Looking forward to thoughts...

Tom

=====
http://www.pixelmech.com/ :: Web Development Services
http://www.DMXzone.com/ :: Premium Content Author / JavaScript / Every
Friday! http://www.maccaws.com/ :: Group Leader [Making A Commercial
Case for Adopting Web Standards]

"That's not art, that's just annoying." -- Squidward
-- 
* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester 
and archives of thelist go to: http://lists.evolt.org 
Workers of the Web, evolt ! 




More information about the thelist mailing list