I you need to fold code, there is deeper problem. I'm a personal fan of Linux's 'the length of a function should be inversely proportional to it's complexity' rule:
There a a lot of overly complex functions in Angband with duplicated snippets of code all over the place.
The entire Linux kernel, nearly 17 million lines of code, is (mostly) less than 80 columns using 8 space tabs. Angband has a hard time using 4 space tabs.
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
than you would have done).
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
than you would have done).
- Complex functions being longer that a screen long are difficult for mere mortals to process - Anything longer than 40 lines (be it a paragraph of text, a C function, a part of mathematical proof) pushes the limits of human memory and comprehension
- Long, complex functions are daunting for new programs and create a barrier for entry for new coders that want to get involved. Keeping the barrier to entry for new coders low is 'A Good Thing'(tm)
- Duplicated code snippets are prone to introduce bugs if changes are made that are not captured in ALL versions of the snippet
- The code ends up having insane levels of indent, which pushes the 80 column limit to the max
The entire Linux kernel, nearly 17 million lines of code, is (mostly) less than 80 columns using 8 space tabs. Angband has a hard time using 4 space tabs.
Comment