Re: Method Size - Best Practices

Heh. Recently a 4,500 line method caught my eye at work. I ignored
the advice to wait until it was >5,000 lines before refactoring :slight_smile:

Can you say what that method was doing ?

Best regards,

Axel

On 5/9/06, [email protected] [email protected] wrote:

Heh. Recently a 4,500 line method caught my eye at work. I ignored
the advice to wait until it was >5,000 lines before refactoring :slight_smile:

Can you say what that method was doing ?

It sounds like it was gestating … getting ready to give birth to a
brood
of little methods.


thanks,
-pate

On 9-May-06, at 3:19 PM, [email protected] wrote:

Heh. Recently a 4,500 line method caught my eye at work. I ignored
the advice to wait until it was >5,000 lines before refactoring :slight_smile:

Can you say what that method was doing ?

Hmmm. Mustn’t give too much away here.

There is a huge pool of data from multiple sources from the past 10
years or so. For each source there are one or two times per day
where the data is analysed in a particular way, and the algorithm
varies between sources, and it varies over time for a particular
source. Sometimes the only changes over time are the start & end
times of the period of interest, sometimes the algorithm changes.

Now imagine code like:

if ($source eq ‘foo’) {
if ($event == 1) {
$param1 = ‘xxx’;
$param2 = ‘yyy’;
if ($date <= …) {
$start_time = ‘xxa’;
$finish_time = ‘yyb’;
}
}
elsif ($event == 2) {

}

 # 20 lines of database connect + calculation yielding $result

}
elsif ($source eq ‘bar’ or $source eq ‘baz’) {
if ($event == 1) {
$param1 = ‘aaa’;
$param2 = ‘bbb’;
if ($date < … && $date >= … and $source = ‘bar’)
$param1 = ‘ddd’;
}

   # 20 slightly different lines of database connect + calculation
   # yielding $result
 }
 elsif ($event == 2) {
   # as above, with minor variations
 }

}

else {
die “can’t handle source “$source”\n”;
}

Now imagine that the way cases were added were by the tried & tested
cut & paste method. Each stanza was about 100 … 150 lines of code
which had been copied and modified by different people over the past
few years.

Does that give you an idea of what was going on?

My attack was to make a table of sources, date ranges, and period of
inerest which gave you the correct values ofr $param1 and $param2 and
the name of a method to call calculate the result. Now I have
reasonable sized routines to start tearing the common bits out of,
but that’s for another day.

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.