in reply to Re^2: Refactoring challenge.
in thread Refactoring challenge.

Why? Your assignment is to refactor the given code. That's a local operation, and doesn't have to depend on more context (then the assignment would be to refactor the code given, in addition to the context).
Well...for starters, I didn't see if/where $depth was used outside of the subroutine. If it isn't used, I'd refactor the code so that $depth was scoped to the sub. Given the code, who knows? It's somewhat difficult to refactor code that you don't know the usage of. Truth be told, I did have a refactoring of it that I was ready to post, but then saw that I didn't have enough context upon which to base my refactorings. However, I felt that I might make better choices with more context.

thor

Feel the white light, the light within
Be your own disciple, fan the sparks of will
For all of us waiting, your kingdom will come

Replies are listed 'Best First'.
Re^4: Refactoring challenge.
by dragonchild (Archbishop) on Mar 07, 2005 at 14:37 UTC
    The challenge was to rewrite the code presented so that it will perform identically (modulus I/O) without knowing the context. :-)

    As for $depth, it is scoped to the sub. As in only pp() can access or modify $depth. The reason it's not within the sub is that $depth (and $indent) are both what would be static variables in C/C++. They're persistent across calls to the subroutine, so must not be reinitialized every time pp() is called (which is what scoping them within the subroutine would do).

    Now, you cannot do a complete refactoring to remove all smells, which I alluded to further down the thread. However, you can improve the code as it stands, knowing that some smells will remain. For example, f1() and f2() should be hidden behind a common interface which would return back $pod and $depth_mod.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.