Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Output of HTML tree built with TreeBuilder

by dash2 (Hermit)
on Jun 20, 2003 at 11:36 UTC ( #267522=note: print w/replies, xml ) Need Help??

in reply to Output of HTML tree built with TreeBuilder

It depends how much futureproofness you want. I assume the code works as it is, but is becoming hard to make more changes. I suggest that you write out a table of all the possible inputs into the decision as to how to write the node out.
has parent || is marked comment || node tag || etc...
On the right of the table, put all the possible results:
indent with tabs || newline before || etc.
Then, write out all the variations on the rows:
has parent is marked comment node tag indent with tabs newline before ...
Once you've done this, you should be able to see what the main factors are which decide the differences in output, and refactor accordingly. (You can also use this technique to model what your future changes will do.)

For example, if you find out that there are only 3 main output styles, then you can rewrite the subroutine to look at the inputs, and then call one of 3 subroutines (you could put them in a dispatch table in case you need more).

Or, if you think the decision is more complex, you might want to create objects to decide how to output the code. For example, you could create NodeWriter::HasParent to write out nodes with parents. Maybe table cell nodes are handled slightly different, so NodeWriter::HasParent::Td could inherit but override some methods. Then you can decide which object to create:

sub prepareOutput { my $self = shift; my ($node) = @_; my $writer = $self->create_nodewriter($node); $self->[OUTPUT] .= $writer->write_output($node); } sub create_nodewriter { my $self = shift; my ($node) = @_; $subtype = $node->parent? 'HasParent':'NoParent'; $tagtype = ucfirst $node->tag; $class = "NodeWriter::$subtype" . "::$tagtype"; return $class->new(); }

In short what I am suggesting is: "separate policy from mechanism".

A massive flamewar beneath your chosen depth has not been shown here

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://267522]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2018-04-25 13:08 GMT
Find Nodes?
    Voting Booth?