Function size...

Discussion in 'C++' started by lombrozo, Jul 24, 2003.

  1. lombrozo

    lombrozo Guest

    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.
     
    lombrozo, Jul 24, 2003
    #1
    1. Advertising

  2. "Agent Mulder" <> 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
     
    Victor Bazarov, Jul 24, 2003
    #2
    1. Advertising

  3. lombrozo

    Agent Mulder Guest

    > 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
     
    Agent Mulder, Jul 24, 2003
    #3
  4. lombrozo

    John Dibling Guest

    On 24 Jul 2003 11:55:29 -0700, (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
     
    John Dibling, Jul 24, 2003
    #4
  5. 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'.
     
    E. Robert Tisdale, Jul 24, 2003
    #5
  6. 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.
     
    Alexander Terekhov, Jul 24, 2003
    #6
  7. 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.
     
    Alexander Terekhov, Jul 24, 2003
    #7
  8. On Thu, 24 Jul 2003 23:03:10 +0200, Alexander Terekhov <> 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. ;-)
     
    Alf P. Steinbach, Jul 24, 2003
    #8
  9. On 24 Jul 2003 11:55:29 -0700, (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.
     
    Thore B. Karlsen, Jul 24, 2003
    #9
  10. lombrozo

    Mickey Mouse Guest

    I would definitely break this down into separate functions. Add methods
    like:

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

    etc...


    This will be MUCH more maintainable...

    "lombrozo" <> wrote in message
    news:...
    > (lombrozo) wrote in message

    news:<>...
    > > 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.

    >
     
    Mickey Mouse, Aug 6, 2003
    #10
  11. (lombrozo) wrote in message news:<>...
    > 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.


    Function call overhead is never a reason not to use multiple
    functions. Any decent compiler will do inlining (should be automatic
    for single-use functions; if not, you can use the "inline" keyword).

    Sam
     
    Samuel Barber, Aug 6, 2003
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Madhanmohan S
    Replies:
    5
    Views:
    7,793
    Madhanmohan S
    Sep 3, 2004
  2. Andreas Klemt
    Replies:
    6
    Views:
    11,238
    Peter Theill
    Nov 28, 2004
  3. =?Utf-8?B?QmlkYXJrb3Rh?=

    How to set the DataGrid Row Size to a fixed size.

    =?Utf-8?B?QmlkYXJrb3Rh?=, May 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    840
    =?Utf-8?B?QmlkYXJrb3Rh?=
    May 19, 2005
  4. Neil Zanella
    Replies:
    4
    Views:
    496
    Gianni Mariani
    Oct 9, 2003
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,661
    Michael Jung
    May 25, 2008
Loading...

Share This Page