http://qs321.pair.com?node_id=362715


in reply to Re^2: Style or Clarity?
in thread Style or Clarity?

A dispatch table is a data structure whose values are subroutine references. In C, this would generally be an array of funcp's. In Perl, it's generally a hash (though it can be an array).

An intelligent class, in this context, is more of a group of classes. It's best explained with an example. Let's use the one from above - a bunch of menu items that may be displayed, depending on some set of external conditions.

The immediately obvious solution would be to create some massive if-else structure to handle every obvious possibility. After about 3-4 distinct options, this becomes unmaintainable.

The next solution most people arrive at is to create some massive data structure and some control function that will "walk the tree", so to speak. This is more manageable, but starts to become problematic around 10-20 distinct options. It also has issues if there is more than one set of external conditions.

Intelligent classes is a variant of the latter solution. You still have the control function, but it's much simpler. It generally would look something like:

my @list_of_classes = ( ... ); foreach my $class (@list_of_classes) { next unless $class->should_render( \%conditions1, \%conditions2, ... ); $class->render; }

You then have a bunch of classes that all provide the should_render() and render() methods. They can be related through inheritance, or not. But, what they all do is determine for themselves whether or not they should render. This means that the conditions are broken out into the units that are smaller and self-contained, thus easier to maintain. When I use this method, I get up to 50 or 60 distinct options with 2-3 sets of external conditions and have no problem.

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

I shouldn't have to say this, but any code, unless otherwise stated, is untested