in reply to Re: Refactoring challenge.
in thread Refactoring challenge.
Thanks Hugo, that helps a bit more :)
A small point: that s/// is pretty inefficient,
Unfortunately, $string =~ s[^.{$pos}\s*][]; doesn't just remove that part of the string that has been printed, it also removes any leading whitespace from the remainder of the string that otherwise would mess up the indenting.
I've been trying to remove the need for that, by having f1() and f2() advance the pointer(s) beyond the whitespace after they locate their relevant positions, so that it gets output on the end of the previous line of output where it does no harm, but so far, I've failed.
However, incorporating it into the _print/_consume sub is a good idea.
And passing the $depth through simplifies things a bit more.
Unfortunately, I cannot flatten the nesting in quite the way you have as the $pos ($length as you have it) will also be undefined if $first is undefined. I realise that this was implicit rather than explicit in the OP.
It's a shame, and something I will attempt to correct if possible, because as you have it, the parser becomes a state machine which would be very nice.
For now, the magic problem doesn't arise as $str is just a string (generated wholey within the outer layers of my code and will not have any magic attached), but it is something that I wasn't aware of and worth noting for the future.
As it stands, the combined refactoring, in-situ of it's surrounding context, has introduced a minor edge case bug that wasn't there before, but the reduction in clutter should make it easier to resolve.
The point I had been at for over a week, was that I had arrived at code that worked, by hacking at my initial attempt, but that it was so fragile that every time I tried to clean it up, it broke badly. By isolating this part from the rest and getting other eyes to look at it, it has simplified the overall thing to the point where it is much less fragile.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Refactoring challenge.
by hv (Prior) on Mar 06, 2005 at 13:28 UTC | |
by BrowserUk (Patriarch) on Mar 06, 2005 at 14:08 UTC |