Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function size...

Reply
Thread Tools

Function size...

 
 
lombrozo
Guest
Posts: n/a
 
      07-24-2003
Hi,

I'd like to ask all of you experienced, well-structured,
Object-Oriented programmers out there about function size.

I am using Visual C++ to create an application, everything is nicely
structured, plenty of classes and inheritence yadda yadda. That part
of my code I am happy with. However, I have this one worker thread
(single function) that I am worried about. It's 1400 lines long.
Now, I remember while at Uni being told that functions should be about
one page size maximum (say 60 to 70 lines), but I don't think this is
applicable for my thread function.

So, I am worried that this function is too long. But surely,
different functions must be different sizes because of the context
they are used, and the purpose they are fulfilling? My function is
quite simple, it sits in a while loop picking of elements from a
global array. It then checks the element for various conditions and
writes any error information to a log file. The elements of this
array are quite big, and contain quite a bit of data, so it takes
quite a lot of code to process the element.

Can anyone give me any advice? Is the function too big? I could
split it into many smaller functions, but what's the point? It would
still follow a procedural execution, only now we have the overhead of
function calls.

Any help would be much appreciated!

Thanks,

Simon.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-24-2003
"Agent Mulder" <(E-Mail Removed)> wrote...
> > I have this one worker thread
> > (single function) that I am worried about. It's 1400 lines long.
> > So, I am worried that this function is too long.

>
> 1400 lines is quit long. [...]


)).. I really like this typo. "If your function is 1400
lines, it's so long that you should quit". No, Agent, I am
certainly not laughing at you, I am laughing with you.

Victor


 
Reply With Quote
 
 
 
 
Agent Mulder
Guest
Posts: n/a
 
      07-24-2003
> I have this one worker thread
> (single function) that I am worried about. It's 1400 lines long.
> So, I am worried that this function is too long.


1400 lines is quit long. You already tried to take out common
functionallity, I suppose. So if the 1400 lines are really
necessary, there is no need to split it up in, say, 28 functions
each 50 lines long. Leave it like it is and cherish it as 'the
big function'.

-X


 
Reply With Quote
 
John Dibling
Guest
Posts: n/a
 
      07-24-2003
On 24 Jul 2003 11:55:29 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (lombrozo) wrote:


>Can anyone give me any advice? Is the function too big? I could
>split it into many smaller functions, but what's the point? It would
>still follow a procedural execution, only now we have the overhead of
>function calls.


First of all, let me just start by saying that 1400 lines is probably
too big, simply for maintainability's sake. Imagine stepping through
1300 lines of code you know isn't causing a problem, just to get to
the 100 that are suspect.

That said, I don't place much stock anymore in the 1-page-long rule of
thumb. I have found that when a function is 'too long' and I try to
break it up, the resulting smaller functions are in aggregate much
more complex than the monolithic original was. This I find is
especially true when dealing with complex Windows GUI issues, such as
customized common controls. Often the case is that a function is 90
lines of boilerplate and initialization code, and 10 lines of real
work. If I try to split that code in to 2 functions, that's 180 lines
of biolerplate code, and the same 10 lines of real work.

I'm *not* saying that there is no point at which functions become too
long. I'm saying that it is silly to constrain yourself to 70 lines.
It seems completely arbitrary. Why is 70 lines ok, but 140 is not?

</dib>
John Dibling
Witty banter omitted for your protection
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      07-24-2003
Something that calls itself Agent Mulder wrote:

> 1400 lines is quit long.
> You already tried to take out common functionality, I suppose.
> So if the 1400 lines are really necessary,
> there is no need to split it up in, say, 28 functions
> each 50 lines long.
> Leave it like it is and cherish it as 'the big function'.



 
Reply With Quote
 
Alexander Terekhov
Guest
Posts: n/a
 
      07-24-2003

lombrozo wrote:
[...]
> Can anyone give me any advice? Is the function too big? I could
> split it into many smaller functions, but what's the point?


There's no point. 10-20 pages of some trivial "dispatching" code (or
something like that) is OK.

regards,
alexander.
 
Reply With Quote
 
Alexander Terekhov
Guest
Posts: n/a
 
      07-24-2003
Troll-alert, Tisdale? Invisible e-ink? Again!? Man.

"E. Robert Tisdale" wrote:
>
> Something that calls itself Agent Mulder wrote:
>
> > 1400 lines is quit long.
> > You already tried to take out common functionality, I suppose.
> > So if the 1400 lines are really necessary,
> > there is no need to split it up in, say, 28 functions
> > each 50 lines long.
> > Leave it like it is and cherish it as 'the big function'.


regards,
alexander.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-24-2003
On Thu, 24 Jul 2003 23:03:10 +0200, Alexander Terekhov <(E-Mail Removed)> wrote:

>
>lombrozo wrote:
>[...]
>> Can anyone give me any advice? Is the function too big? I could
>> split it into many smaller functions, but what's the point?

>
>There's no point. 10-20 pages of some trivial "dispatching" code (or
>something like that) is OK.


You troll you!, Alexander.

 
Reply With Quote
 
Thore B. Karlsen
Guest
Posts: n/a
 
      07-24-2003
On 24 Jul 2003 11:55:29 -0700, (E-Mail Removed) (lombrozo) wrote:

>Hi,
>
>I'd like to ask all of you experienced, well-structured,
>Object-Oriented programmers out there about function size.
>
>I am using Visual C++ to create an application, everything is nicely
>structured, plenty of classes and inheritence yadda yadda. That part
>of my code I am happy with. However, I have this one worker thread
>(single function) that I am worried about. It's 1400 lines long.
>Now, I remember while at Uni being told that functions should be about
>one page size maximum (say 60 to 70 lines), but I don't think this is
>applicable for my thread function.


I don't agree that there is a set minimum number of lines above which
you can never go, but 1400 lines is way too big. I highly doubt it truly
needs to be that long.

>So, I am worried that this function is too long. But surely,
>different functions must be different sizes because of the context
>they are used, and the purpose they are fulfilling? My function is
>quite simple, it sits in a while loop picking of elements from a
>global array. It then checks the element for various conditions and
>writes any error information to a log file. The elements of this
>array are quite big, and contain quite a bit of data, so it takes
>quite a lot of code to process the element.
>
>Can anyone give me any advice? Is the function too big? I could
>split it into many smaller functions, but what's the point? It would
>still follow a procedural execution, only now we have the overhead of
>function calls.


In my experience, it's not hard to break up very large functions into
smaller pieces. The end result is much more maintanable, expandable, and
robust. Oftentimes it also results in much _less_ code, especially when
a lot of error handling is involved.

I'd say definitely give it a good try. Refactoring isn't easy, but once
you try it you will see the benefits. I've worked on code where 1400
line functions seemed to be the norm (it sure felt like it), and after
trying to maintain such code I think such monolithic functions are
completely indefensible.

I really hope you're not doing any kind of manual resource management in
those 1400 lines...

--
Be seeing you.
 
Reply With Quote
 
Mickey Mouse
Guest
Posts: n/a
 
      08-06-2003
I would definitely break this down into separate functions. Add methods
like:

- updateDroppedFrames()
- updateVideoStandard()
- updateDigitalInput()

etc...


This will be MUCH more maintainable...

"lombrozo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> (E-Mail Removed) (lombrozo) wrote in message

news:<(E-Mail Removed). com>...
> > Hi,
> >
> > I'd like to ask all of you experienced, well-structured,
> > Object-Oriented programmers out there about function size.
> >
> > I am using Visual C++ to create an application, everything is nicely
> > structured, plenty of classes and inheritence yadda yadda. That part
> > of my code I am happy with. However, I have this one worker thread
> > (single function) that I am worried about. It's 1400 lines long.
> > Now, I remember while at Uni being told that functions should be about
> > one page size maximum (say 60 to 70 lines), but I don't think this is
> > applicable for my thread function.
> >
> > So, I am worried that this function is too long. But surely,
> > different functions must be different sizes because of the context
> > they are used, and the purpose they are fulfilling? My function is
> > quite simple, it sits in a while loop picking of elements from a
> > global array. It then checks the element for various conditions and
> > writes any error information to a log file. The elements of this
> > array are quite big, and contain quite a bit of data, so it takes
> > quite a lot of code to process the element.
> >
> > Can anyone give me any advice? Is the function too big? I could
> > split it into many smaller functions, but what's the point? It would
> > still follow a procedural execution, only now we have the overhead of
> > function calls.
> >
> > Any help would be much appreciated!
> >
> > Thanks,
> >
> > Simon.

>




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments