Forum: Ruby Re: Method Size - Best Practices

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
67bb4df2775f6a6b603347dce7119571?d=identicon&s=25 unknown (Guest)
on 2006-05-09 21:20
(Received via mailing list)
> 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 :-)

Can you say what that method was doing ?

Best regards,

Axel
Bb6ecee0238ef2461bef3416722b35c5?d=identicon&s=25 pat eyler (Guest)
on 2006-05-09 21:39
(Received via mailing list)
On 5/9/06, Nuralanur@aol.com <Nuralanur@aol.com> 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 :-)
>
> 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
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2006-05-09 21:55
(Received via mailing list)
On 9-May-06, at 3:19 PM, Nuralanur@aol.com 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 :-)
>
> 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 Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
This topic is locked and can not be replied to.