I don't find that it's useful to have any limits on the length of functions; I have code with functions (even main!) are hundreds of lines long.
I do, however, find that it is essential to limit the width of functions, and if my code doesn't comfortably fit into 80 columns when using 8-column tabs, there's a clear sign that it needs to be broken up.
In general, nested loops are far more of a problem than simple "do X, then do Y, then do Z" code.
Interesting! Personally I find that once a function is too long it needs refactoring either into an object or more functions although with web stuff one tends to end up with much longer functions. can we have an example of one of your long functions as to what it does?
* Connects to the server (trying multiple addresses if necessary),
* Constructs HTTP requests and sends them to the server,
* If less than one HTTP request is pending, blocks until it can finish writing a request,
* Reads HTTP response headers,
* Reads an HTTP response body and writes the data to disk,
* Prints a status line,
* if the server sent an HTTP/1.1 response, loops back to the top to continue sending more requests and reading more responses using the same TCP connection,
* and if the connection broke, closes it and loops back to the top to open a new connection and continue downloading.
I separated out the bits which could usefully be put into their own functions (e.g., constructing an HTTP request, or reading a \r\n terminated line from a socket), but there was no point dividing up the rest.
Comments
I don't find that it's useful to have any limits on the length of functions; I have code with functions (even main!) are hundreds of lines long.
I do, however, find that it is essential to limit the width of functions, and if my code doesn't comfortably fit into 80 columns when using 8-column tabs, there's a clear sign that it needs to be broken up.
In general, nested loops are far more of a problem than simple "do X, then do Y, then do Z" code.
Interesting! Personally I find that once a function is too long it needs refactoring either into an object or more functions although with web stuff one tends to end up with much longer functions. can we have an example of one of your long functions as to what it does?
can we have an example of one of your long functions as to what it does?
Sure. main() in http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.sbin/portsnap/... is over 400 lines long, and it
* Looks up a server's DNS entries,
* Connects to the server (trying multiple addresses if necessary),
* Constructs HTTP requests and sends them to the server,
* If less than one HTTP request is pending, blocks until it can finish writing a request,
* Reads HTTP response headers,
* Reads an HTTP response body and writes the data to disk,
* Prints a status line,
* if the server sent an HTTP/1.1 response, loops back to the top to continue sending more requests and reading more responses using the same TCP connection,
* and if the connection broke, closes it and loops back to the top to open a new connection and continue downloading.
I separated out the bits which could usefully be put into their own functions (e.g., constructing an HTTP request, or reading a \r\n terminated line from a socket), but there was no point dividing up the rest.
Thanks a lot.