View Single Post
Old Nov 9th, 2006, 5:21 PM   #8
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
There is actually a term for the act of cleaning these kind of things up, it's called "Refactoring. As previously stated, a lot of it is just identifying what code could go into a function. Don't be afraid of having functions with long signatures. A common refactoring process is also creating large arrays with all the necessary information, sometimes even including function names and parameters.
example: if your program looks like this
function bla
{
    if a then 1
    if b then 2
    if c then 3
    if d then 4
}
you could make an array
{
( a, 1 )
( b, 2 )
( b, 3 )
( c, 4)
}
and replace the top code with something like this
foreach $i in $array {
  if $i[0] then $i[1]
}
where $i[1] is actually an entire conditional and $i[2] is actually the name of a subroutine that can be parsed with some regex and used.

the string "subrouting:a:b:c"
could be parsed with
/^subroutine:(.*):(.*):(.*)/
and called run as
subroutine(a, b, c)
you can match the name of the subroutine to call the correct one in the event you need to call more than one.

now in some languages this is not as easy as in others (PERL is very good at things like this for example).
Excuse me for my rant on Refactoring, but it's a good skill to have, and I use it everyday for my job and on my personal projects. Eventually one can get good at it that the code written on the first try does not have to be refactored anymore, but don't be afraid to write it out the long way and look back afterwards.

Good Luck,

-Dizz
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote