From: Justin F. <je...@ey...> - 2001-07-08 01:50:57
|
Alex/Guys: Look, I don't mind exposing my ignorance, but I am floundering around trying to get "experience" in bolting modules together and to establish some standards for our company's coding. What I am sort of wrestling with, now, is the tradeoff between: 1. Writing code "correctly" 2. Trying not to clutter up global scope with potential danger of name clash 3. Increase performance, or the corrollary, not inadvertantly decrease performance. I will give you an example, here, which you may not think typical. I have bolted together an "application" with a module that allows you to UPDATE/INSERT/DELETE, find in any field, sort on any field, jump to any record, browse all records for all databases in my system. It works quite well, and is a general module that I can bolt into other "applications". Since it is general, it makes one humungous number of SELECT's in the constructor when you must fetch/SELECT - All database names - All tables in each database, meaning, additionally * field names * field types * field lengths * field options which I collect and put into a large data structure. This module/Class is called EditTable. OK, this is a big hit on MySQL, all done in the constructor. You can even see the page generation is slow, even on a development machine with your "private" database(s). You can imagine what it would be if you have 50 databases with 10 tables each, each table having, say 10 fields, and 50 clients using it. Now, being a good boy, in other classes, when I need this class, or something from it, I instantiatiate it with a new EditTable inside another module instead of having the EditTable module put the data structure in global scope one time, and have other modules go check on the existence of that data structure, and if not there, do a new EditTable. It is interesting to watch MySQL query logging each time EditTable gets instantiated. But, I am going to use this data structure in a Report Generator, where you need the same kind of data to be able to generate reports dynamically, and in other modules. So, I have a tension between going global, meaning, putting the data structure global, so I don't have to instantiate a new instance of EditTable, and taking the big MySQL hit over and over, technically unnecessary. THIS WAS NOT THE CASE when the modules used Init(), as, in theory Init() was only called once when loaded by BC, no matter how many times you instantiated the class inside another module. Before, you had to specifically call Init() again (if you needed to, with options/different args). Thus, the switch to constructors has caused a "penalty" when you have a whole bunch of database work in the constructor. What would you guys do? Is it even worthwhile to consider asking Alex for some kind of registration facility for things/(big things) that indeed have a good reason for being global? Yes, I do respect Alex's "law" that he wants globals to be held to a minimum. What is the sweet compromise here???? _jef -- Justin Farnsworth Eye Integrated Communications 321 South Evans - Suite 203 Greenville, NC 27858 | Tel: (252) 353-0722 |