Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:
The first is the obvious choice to change Block2 into a subroutine. This doesn't feel right to me in this situation for some reason. There are quite a few variables that would need to be passed and keeping track of what is going on is getting difficult. It is obvious to me that refactoring is the right approach but the only alternative to a sub that I can think of is:if ( $condition1 ) { if ( $condition2 ) { # Block1 } else { # Block2 } } else { # Block2 repeated again }
{ if ( $condition1 ) { if ( $condition2 ) { # Block1 last; } } # Block2 }
I do not like either of these choices which means I need to back up my refactoring to a higher level. With that said, I am still interested if anyone can think of a different way to eliminate the duplication. I am not going to provide the actual code because I am looking for generic alternatives. I know that given specific conditions there are clear winning options but that's not what I am after for now.
Cheers - L~R
Update: I am an idiot. I actually started out with RazorbladeBidet's solution below, but changed it because it didn't work as evaluation of the conditions has side effects. When I ended up with duplicated code I tried removing the duplicated code instead of the side effects. That's why a subroutine didn't feel right and it is also why refactoring at a higher level is the right thing to do.
Thanks for helping me see the forest through the trees.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Better solution for code reduction?
by RazorbladeBidet (Friar) on Mar 10, 2005 at 17:00 UTC | |
|
Re: Better solution for code reduction?
by Keystroke (Scribe) on Mar 10, 2005 at 17:03 UTC | |
|
Re: Better solution for code reduction?
by 5mi11er (Deacon) on Mar 10, 2005 at 17:01 UTC | |
|
Re: Better solution for code reduction?
by wfsp (Abbot) on Mar 10, 2005 at 17:09 UTC | |
by 5mi11er (Deacon) on Mar 10, 2005 at 20:01 UTC | |
by wfsp (Abbot) on Mar 11, 2005 at 07:28 UTC | |
by Anonymous Monk on Mar 11, 2005 at 09:32 UTC | |
|
Re: Better solution for code reduction?
by chb (Deacon) on Mar 11, 2005 at 08:08 UTC | |
|
Re: Better solution for code reduction?
by perlfan (Parson) on Mar 10, 2005 at 18:52 UTC |