BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:
I'm refactoring a piece of code that uses $x, $y, $z. There are 6 repetitions of a large chunk of code that are essencially identical, but for the 'ordering' of those three variables.
This is ripe for refactoring into a subroutine called 6 times. Ie:
if( $x ) { doStuff( $x, $y, $z ); doStuff( $x, $z, $y ); } if( $y ) { doStuff( $y, $x, $z ); doStuff( $y, $z, $x ); } if( $z ) { doStuff( $z, $x, $y ); doStuff( $z, $y, $x ); }
But here is a dilemma I've encountered many times before, what do you name the 3 variables inside the sub:
sub doStuff { my( $?, $?, $? ) = @_; .... }
I know it's trivial, just call'em $p, $q, $r or $u, $v, $w or $a, $b, $c I hear the collective mind screaming, but it is a large and fairly complex piece of code, and $x, $y, $z are actually placeholders for a bunch of different entities that have x, y & z components all interacting and it gets quite confusing -- here's one of the 6 blocks in the code I'm adapting:
gg = _GetGrid( this.Min.y / this.Parent.Zoom.y, this.Max.y / t +his.Parent.Zoom.y, this.Scale.y ); if( this.GridDelta.y != 0 ) gg[1] = this.GridDelta.y; var ii = 0; for( var jj = gg[2]; jj >= gg[0]; jj -= gg[1] ) { pp.y = jj*this.Parent.Zoom.y; pp.z = this.Min.z; vv = this.Parent.ScreenPos( pp ); _OAV( this.Line[4][ii], "from", parseInt( vv.x ) + "," + p +arseInt( vv.y ) ); xx = vv.x; yy = vv.y; pp.z = this.Max.z; vv = this.Parent.ScreenPos( pp ); _OAV( this.Line[4][ii], "to", parseInt( vv.x ) + "," + par +seInt( vv.y ) ); _OAV( this.Line[4][ii], "stroke", this.StrokeColor ); _OAV( this.Line[4][ii], "visibility", "visible" ); if( this.Parent.Th < 0 ) { _OAV( this.Text[4][ii],"x",Math.floor( xx+( vv.x-xx )* +1.06 )-50*uu ); _OAV( this.Text[4][ii],"y",Math.floor( yy+( vv.y-yy )* +1.06 )-7*uu ); } else { _OAV( this.Text[4][ii],"x",Math.floor( vv.x+( xx-vv.x +)*1.06 )-50*uu ); _OAV( this.Text[4][ii],"y",Math.floor( vv.y+( yy-vv.y +)*1.06 )-7*uu ); } _OAV( this.Text[4][ii],"color",this.StrokeColor ); _OAV( this.Text[4][ii],"visibility","visible" ); if( ( ii == 1 )&&( this.Label.y )) _OAV( this.Text[4][ii], +"innerText",this.Label.y ); else { if( isNaN( this.Scale.y )) { if( this.Scale.y.substr( 0,9 ) == "function " ) { ff = eval( "window."+this.Scale.y.substr( 9 )) +; if( ff ) _OAV( this.Text[4][ii],"innerText",ff +( _ScaleString( jj,gg[1] )) ); } else _OAV( this.Text[4][ii],"innerText",_ScaleStri +ng( jj,gg[1] )+this.Scale.y ); } else { if( this.Scale.y<1 ) _OAV( this.Text[4][ii],"inner +Text","" ); if( this.Scale.y == 1 ) _OAV( this.Text[4][ii],"in +nerText",_ScaleString( jj,gg[1] )); if( this.Scale.y>1 ) _OAV( this.Text[4][ii],"inner +Text",_DateFormat( jj, gg[1], this.Scale.y )); } } ii++; } if( this.Min.y<this.Max.y ) { if( this.Parent.Fi >= 180 ) { if( this.Min.y/this.Parent.Zoom.y>gg[0]-gg[1]/3 ) _OAV +( this.Text[4][ii-1],"innerText","" ); } else { if( this.Max.y/this.Parent.Zoom.y<gg[2]+gg[1]/3 ) _OAV +( this.Text[4][0],"innerText","" ); } } while( ii<11 ) { _OAV( this.Line[4][ii],"visibility","hidden" ); _OAV( this.Text[4][ii],"visibility","hidden" ); ii++; }
So my question is, is there some technique or pattern or something that has been devised to deal with this? Have you arrived at some way of dealing with this situation that works for you?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Refactoring technique?
by hdb (Monsignor) on Apr 24, 2015 at 12:54 UTC | |
by BrowserUk (Patriarch) on Apr 24, 2015 at 13:56 UTC | |
by hdb (Monsignor) on Apr 24, 2015 at 16:18 UTC | |
by BrowserUk (Patriarch) on Apr 24, 2015 at 16:42 UTC | |
by jeffa (Bishop) on Apr 24, 2015 at 22:04 UTC | |
| |
by hdb (Monsignor) on Apr 24, 2015 at 21:31 UTC | |
| |
|
Re: Refactoring technique?
by flexvault (Monsignor) on Apr 24, 2015 at 15:31 UTC | |
by BrowserUk (Patriarch) on Apr 24, 2015 at 16:05 UTC | |
by MidLifeXis (Monsignor) on Apr 24, 2015 at 17:24 UTC | |
by BrowserUk (Patriarch) on Apr 24, 2015 at 18:12 UTC | |
by flexvault (Monsignor) on Apr 24, 2015 at 19:50 UTC | |
|
Re: Refactoring technique?
by marinersk (Priest) on Apr 24, 2015 at 12:57 UTC | |
by BrowserUk (Patriarch) on Apr 24, 2015 at 14:13 UTC | |
by marinersk (Priest) on Apr 24, 2015 at 16:04 UTC | |
by BrowserUk (Patriarch) on Apr 24, 2015 at 16:06 UTC | |
by marinersk (Priest) on Apr 25, 2015 at 00:11 UTC | |
by marinersk (Priest) on Apr 25, 2015 at 00:07 UTC | |
|
Re: Refactoring technique?
by Anonymous Monk on Apr 24, 2015 at 14:09 UTC | |
by BrowserUk (Patriarch) on Apr 24, 2015 at 14:15 UTC |