It's early days but I'm making progress! https://github.com/ebanner/pynt
https://news.ycombinator.com/item?id=16064412
I think the important thing is that modularity (breaking up longer functions into shorter ones) helps, but only if not done blindly. One has to spend some time doing it, since there are many possible ways it can be done, and some of them will be better than others. So it can pay off in the long term, but only if one is willing to put in the effort and time, and be ready to throw away some not-so-good attempts. I've done this sometimes and seen that it does help.
I've seen 1k LoCs functions that were incredibly easy to understand, and 10 liners that were complete gibberish messes.
I do however agree that declaring new variables every 10 lines over hundreds of lines would be hell to follow.
For some cases that results in more confusing code.
Usually I try to weight the overhead of having more function prototypes to track, indirections in the main function and whatnot versus the overhead of having it all inlined.
A lot of things become more evident as you inline one-use functions, because the whole context is now local.
Interesting, your comment reminded me of inline functions in C++ and C99; not the same thing (or at least goal) as what we were discussing, but closely related:
Still, I think the GP has a point - clear naming is very important, regardless of one's attitude towards function size. ;-)
let lines = {
let ret = vec![];
// Open file
// Read into ret
ret
};
let processed_data = {
// Open another file
// Use lines
// Construct ret
ret
};
....
You get the best of both worlds this way: scoped, meaningful names like with sub-functions and the continuity of a long one.Everything is an expression is an underrated feature of Rust.
List<String> lines;
{
int whatever;
lines = new ArrayList<>();
// modify lines
}
List<Object> processed_data;
{
int whatever;
processed_data = new ArrayList<>();
// modify processed_data, using lines
} if err := func() error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
// work with f
return nil
}(); err != nil {
return err
}Also, if we're talking about OOP, small methods are better. With small methods it's easier to change behavior by subclassing. In several occasions, I've been forced to make a subclass with a copy of a big method with only one line changed.
If the subroutines are sensibly named, you shouldn't need to. I'd argue they're also easier to follow than blocks of code within a long function as well-written small functions will depend only on their inputs, whereas code in a large function could depend on any of the params to that large function.
Splitting functions is basically adding indirections and spreading the context all over the place.
For reusable tasks that works wonders, but for single-use functions that usually obfuscates the intent more than anything.
I don't want to follow fifteen 10-lines one-use functions when I could read one 150-line function (which would most likely be a lot less due to less boilerplate).
A potentially contrarian view: a long-ish straight linear function without duplicated code is often much easier to understand than a smaller function broken into subroutine callouts (because you don't have to chase down the implementation of the subroutines to figure out what's going on).