Mail Archives: djgpp/1999/01/10/13:38:05
On Sun, 10 Jan 1999, pjotr wrote:
) When writing code, is there like a maximum size of lines per function
) you should take into account? I just wrote a 200+ lines function and I
) was just wondering if maybe it's too large for just one function.
)
) I know it wouldn't make a difference at runtime, but as a C-writing
) etiquette, what would you suggest? Note that those 200+ lines DO
) include comments.
From the LinuxCodingStyle distributed with packages of the Linux kernel
sources (available at http://ftp.n.ml.org/linux/LinuxCodingStyle if you'd
like to peruse the whole document):
Chapter 4: Functions
Functions should be short and sweet, and do just one thing. They should
fit on one or two screenfuls of text (the ISO/ANSI screen size is 80x24,
as we all know), and do one thing and do that well.
The maximum length of a function is inversely proportional to the
complexity and indentation level of that function. So, if you have a
conceptually simple function that is just one long (but simple)
case-statement, where you have to do lots of small things for a lot of
different cases, it's ok to have a longer function.
However, if you have a complex function, and you suspect that a
less-than-gifted first-year high-school student might not even
understand what the function is all about, you should adhere to the
maximum limits all the more closely. Use helper functions with
descriptive names (you can ask the compiler to in-line them if you think
it's performance-critical, and it will probably do a better job of it
that you would have done).
Another measure of the function is the number of local variables. They
shouldn't exceed 5-10, or you're doing something wrong. Re-think the
function, and split it into smaller pieces. A human brain can
generally easily keep track of about 7 different things, anything more
and it gets confused. You know you're brilliant, but maybe you'd like
to understand what you did 2 weeks from now.
--
Daniel Reed <n AT ml DOT org>
/sbin/breakfast: warning: cereal port not responding
- Raw text -