Length of main()?

This is a discussion on Length of main()? within the C++ Programming forums, part of the General Programming Boards category; How long should main() be in a program? I mean, it obviously depends on what program it is, but generally, ...

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    Length of main()?

    How long should main() be in a program? I mean, it obviously depends on what program it is, but generally, should main() contain more than like, 3 pages of code, or is there really no limit to what would be considered acceptable?

    I'm asking because i feel that i COULD put a large piece of code i have written in a function of it's own, but i feel it would reduce readability and make the program unnecesarily (How do you spell that :S?) complex. But on the other hand, my main is around 100 lines at the moment, and would probably be pushing 200 after i write the last code, is that too long? Would it be better coding etiquette (Another tricky one?) to make the main() as short as possible?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Depends on your program, but main() and every other function should generally be small. Anywhere from two or three lines to 100 is acceptable, but it all depends on what you are doing.

    Having extra functions rarely makes code more complex, especially if you break the code into parts that do separate tasks. Your main will be much easier to read and understand at a high level if it calls three or four functions and that's it. If those functions have well-defined responsibilities and good names, then you can see the flow of the program just from those three or four lines. Then if you look at each function, you should find the same thing to be true (although obviously they won't all be only a few lines long).

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    As a rough rule of thumb, if you can't see the whole function on screen / on paper at the same time (ie, you have to continually scroll or flip pages), then it is probably too long.

    Simple switch/cases with many cases would be an obvious exception, but anything implementing a complex algorithm should be kept to a manageable size.

    Also to add to Daved's list, smaller functions with a well defined purpose are easier to test.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Functions should be reusable, but they don't have to be. Programs should be organized and readable, but they don't have to be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  3. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  4. differences between int main and void
    By louis_mine in forum C Programming
    Replies: 6
    Last Post: 09-25-2004, 06:49 AM
  5. what to do with the main window
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 01-28-2003, 07:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21