| ▲ | rhubarbtree 3 hours ago | |
Just chiming in here to say, absolutely you should keep functions small and doing one thing. Any junior reading this should go and read the pragmatic programmer. Of course a function can be refactored in a wrongheaded way as you’ve suggested, but that’s true of any coding - there is taste. The ideal of refactoring such a function you describe would be to make it more readable, not less. The whole point of modules is so you don’t have to hold in your head the detail they contain. Long functions are in general a very bad idea. They don’t fit on a single screen, so to understand them you end up scrolling up and down. It’s hard to follow the state, because more things happen and there is more state as the function needs more parameters and intermediate variables. They’re far more likely to lead to complecting (see Rich Hickey) and intertwining different processes. Most importantly, for an inexperienced dev it increases the chance of a big ball of mud, eg a huge switch statement with inline code rather than a series of higher level abstractions that can be considered in isolation. I don’t think years worked is an indicator of anything, but I’ve been coding for nearly 40 years FWIW. | ||
| ▲ | xorcist 2 hours ago | parent [-] | |
> They don’t fit on a single screen, so to understand them you end up scrolling up and down. Split them up into multiple functions and there is more scrolling, and now also jumping around because the functions could be anywhere. > It’s hard to follow the state, because more things happen It's easier to follow state, because state is encapsulated in one function, not constantly passed around to multiple. > a huge switch statement with inline code Huge switch statements are a common reason for large functions. Python has this architecture and largely won because the interpreter is surprisingly easy to understand and extend. | ||