3 ms·
"You are adamantly opposed to function/methods over 20 lines of code." There are (at least) 2 problems with long funtions/methods: 1) They typically lead to mo
by at 19y ago
"You are adamantly opposed to function/methods over 20 lines of code."
There are (at least) 2 problems with long funtions/methods:
1) They typically lead to more duplicated (and less reusable) code, e.g. repeated blocks of code between functions that could have been replaced with to-the-point short functions. A typical sign of where to extract a function is when you see a long block with a comment above it inside an even longer function, then replace the block with a function with a name inspired by the comment.
2) The methods becomes harder to test, and possibly even more important, the test code becomes harder to maintain (and untested code of some complexity usually doesn't work), e.g. what happens to your existing tests if you add some new conditions at the top of a long method?
- Tichy 19y agoA good argument might be chunking: the (average) human brain can keep only about six or seven chunks of information in short term memory. A method with 20 lines of codes is roughly 20 chunks. Move some parts of those 20 lines into other functions, and you have created more effective chunks. Presumably, the brain has a much easier time understanding things it can actually keep in memory. One idea is to create functions instead of comments, that is instead of writing "compute the rank" followed by some code, create and call a method computeRank().