Thread: Reasons for keeping functions small

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    54

    Reasons for keeping functions small

    Does the amount of work performed to place a function on the stack change significantly as the size of the function increases? I'm interested in reasons other than appearance and organization, more related to efficiency, that make writing smaller functions a good idea.

    Example: A function containing 400 lines of code, 350 of which are contained within conditional statements. Those 350 can obviously be broken down into smaller functions, and would make an improvement, since the work inside one conditional, in this example, is independent of the work inside the others. The only extra overhead would be calling the function within the conditional. How much of a performance gain is there in putting each conditional statement's code in separate functions, if any.
    Last edited by maththeorylvr; 03-25-2006 at 09:46 PM. Reason: none

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    As long as readibility is not affected then there's no need to split up a function like that. If it gets to be 2000 or so lines then it might make sense.

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    If you have 350 conditional statements in a function, the data structures and algorthms you're using probably could use some changing.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    54
    Quote Originally Posted by cwr
    If you have 350 conditional statements in a function, the data structures and algorthms you're using probably could use some changing.
    its not 350 conditional statements, just lines, and this is a hypothetical example, to try and see what you would do in extreme cases. I guess the overhead of allocating a function on the stack isnt that big of a factor in overall performance is it?

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Usually you should go by "one job per function". And this is really because it makes the function easier to understand what it's doing at a later time, than say, having n+ jobs. Do not worry about speed, though, it's really irrelevant (especially since C is pretty low level).

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    54
    im doing it in Java, but thought it might be generalizable from C to Java, and languages in general.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by MadCow257
    As long as readibility is not affected then there's no need to split up a function like that. If it gets to be 2000 or so lines then it might make sense.
    I disagree. A function ideally should fit on a page (less than 60 lines or so). This makes the function easier to follow and maintain.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It's not the size of a function (in terms of number of lines of code) that increases the overhead of a function call. The more usual overhead is related to number and size of arguments being passed. For example, passing large data structures by value will typically involve more overhead than passing a pointer to those structures.

    Compilers can shift the balance a bit through techniques like inlining (eg placing the actual body of a function in place instead of calling it). That involves a trade-off of executable file size and performance. The trade-off isn't always obvious, for example if it is necessary to swap some code out of memory so other code can run.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to split long programe in small files
    By umeshjaviya in forum C Programming
    Replies: 11
    Last Post: 04-15-2008, 02:45 AM
  2. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  3. DLL class member functions
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 06:59 AM
  4. Borland, libraries, and template member functions
    By roktsyntst in forum C++ Programming
    Replies: 0
    Last Post: 06-01-2003, 09:52 PM
  5. Expression Manipulator v0.2 (bug fixes, functions)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-26-2003, 04:52 PM