Search code examples
functionlines-of-code

how big should function length be (lines of code in a function)?


Possible Duplicate:
How many lines of code should a function/procedure/method have?

I would like to know how many lines of code should be function have? How many lines is too much.

I read this a while back, it around 10 or 20 lines but it was because the screen would only accommodate so many lines. Now as the screen size become larger, that would not hold true.

Let's assume that the no part of the function is used anywhere else i.e. disregard DRY principle.

I'd like to hear what others have to say about this.

thanks.

Note: Duplicate of When is a function too long?, could not find it when I posted.


Solution

  • This kind of question is well answered in Code Complete. Steve McConnel wrote an entire page to answer this question. His conclusion:

    Decades of evidence say that routines of such length (>100 lines) are no more error prone than shorter routines. Let issues such as the routine's cohesion, number of decision points, number of comments needed to explain the routine, and other complexity-related considerations dictate the length of the routine rather than imposing a length restriction per se. That said, if you want to write routines longer than about 200 lines, be careful.