Re: Function size
| From: | Ashley Sheridan | Date: | Wed, 23 May 2012 19:49:49 +0000 |
| Subject: | Re: Function size | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-317966@lists.php.net to get a copy of this message | ||
On Wed, 2012-05-23 at 20:59 +0200, Matijn Woudt wrote:
> On Wed, May 23, 2012 at 5:49 PM, shiplu <shiplu.net@gmail.com> wrote:
> > On Wed, May 23, 2012 at 8:14 PM, Tedd Sperling <tedd@sperling.com> wrote:
> >
> >> Hi gang:
> >>
> >> On May 21, 2012, at 8:32 PM, tamouse mailing lists wrote:
> >> > A rule of thumb is no more than 50 lines per
> >> > function, most much less. Back in the day when we didn't have nifty
> >> > gui screens and an 24 line terminals (yay green on black!), if a
> >> > function exceeded one printed page, it was deemed too long and marked
> >> > for refactoring.
> >>
> >> You hit upon a theory of mine -- and that is our functions grow in size up
> >> to our ability to view them in their totality. When our functions get
> >> beyond that limit, we tend to refactor and reduce.
> >>
> >
> > When number of lines becomes the criteria of function size? Wouldn't it
> > depends on the task the function is doing? I follow this rule, *Each time I
> > end up need a code block I wrote earlier, I convert it to a function. *So
> > simple. This way you re-factor your code automatically and you dont do any
> > copy paste. Last year someone on Stackoverflow asked something like
> > this[1]. And that was my answer.
> >
> >
>
> Hi,
>
> While this could be one reason to start a new function, it should not
> (IMO) be the only reason. Sometimes you can have a large complicated
> function, with say 200 LOC. While I wouldn't need any of these lines a
> second time, I usually try to rip blocks of say 50 lines out and put
> it in a seperate function, so that the main function itself is easier
> to understand.
>
> - Matijn
>
I'm of the same mind. Generally I'll split a function if I'm reusing
more than a couple of lines of code. I only split a "large" function if
it's actually doing several things, if it happens to need 200 lines to
perform one 'step' then I'll leave it as is. While I do prefer my
functions to fit into a single 'screen', it rarely happens quite like
that, because I move from screen to screen with different resolutions,
so there's no constant limit for me.
As a rough example, on a random selection of 27 functions taken from a
controller on a site I worked on I get these general statistics:
Functions: 27
Mean lines: 22.5
Mode lines: 3
Max lines: 218
The function with 218 lines is a large switch, and it doesn't make sense
to do it any other way, because it would actually end up less readable.
--
Thanks,
Ash
http://www.ashleysheridan.co.uk