Thread: average function length...

  1. #1
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537

    average function length...

    How long is the average function? I remember being told to keep the program modular and the length of functions relatively short. This is usually ok but I still find myself sometimes writing functions whcih are nearly 100 lines long. So I guess the question is how long is too long?
    Couldn't think of anything interesting, cool or funny - sorry.

  2. #2
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    That depends on what the function is supposed to do!

  3. #3
    kuphryn
    Guest
    Function length, like program length, is a matter of program design. You can have a 1000 lines function or a hundred 10 lines functions.

    Kuphryn

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    The material you have probably read was talking about keeping your code readable and modular. I agree with that completely. Keep them as small as can be easily read and understood.

    If the question of callstack speed comes up...

    Yes, calling more functions can slow things down a tad. But not if they are inline. Pick out repetitive functions that are in areas where you should optimize and add the inline keyword. This will allow the readability of a modular function without the speed loss.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    In a 100 line function, I think it's pretty likely there is some common functionality that can be factored out into smaller functions which are called more than once. Instead of one 100 line function, perhaps it's possible to break it into a 50 line function and a 30 line function that is called twice.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    Stolen from Code Complete -

    ...if you think it makes sense to make a certain routine 100, 150, or 200 lines long, it's probably all right to do so. Current evidence says that routines of such length are no more error prone than shorter routines and are likely to be cheaper to develop.

    If you want to write routines longer than about 200 lines, be careful.

    ...In a study of the code for IBM's OS/360 operating system and other systems, the most error prone routines were larger than 500 lines of code. Beyond 500 lines, the error rate tended to be proportional to the size of the routine.

    etc
    However, as others have pointed out, you may have more reasons for breaking up a program than preventing errors.

  7. #7
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    The reason I have this function is that it initialises a class object from the user input. The reason its about 180 lines is because there are about 7 variables to initialise and some conversions to enumerations that I'm using switch statments for.

    Time for stripping it down a bit into smaller functions, theres lots of yes/no questions which could be changed

    Thanks for all the advice
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM