4 ms·
> In this case I hope nobody is proposing a single 1000-line god function. I’ll take well-structured 1000-lines function over bad spaghetti of hundreds small f
by wiseowise 3y ago
> In this case I hope nobody is proposing a single 1000-line god function.
I’ll take well-structured 1000-lines function over bad spaghetti of hundreds small functions any day.
- leoedin 3y agoHave you ever seen a well structured 1000 line function? I'm sure they exist - maybe some sort of exceedingly complicated data transform or something. But in almost every situation I've seen, a 1000 line function has countless side effects, probably sets a few globals, takes loads of poorly named arguments, each of which is a nested data structure which it reaches deeply into and often has the same for loop copied and pasted 10 times with one character changed. Often a 1000 line function is actually 5 or 6 20 line functions. I'm sure there are legitimate exceptions, but I've never seen them.
- deelly 3y agoI`m not OP, but yes, I saw. Thats my personal opinion, and nothing more: Something like complex one time financial/workflow/maintenance operation that includes calling dozens of different smaller functions, but very well structured. It does not make sense to separate it more into different functions, because execution is generally linear and having to deal with tree of calls where some calls is depends on state of the previous is become cumbersome and makes reading and making changes more complex. Again, thats my personal feeling, and nothing more.
- deleted 3y ago[deleted]
- Thorrez 3y agohttps://github.com/github/putty/blob/master/terminal.c#L3281 https://github.com/github/putty/blob/master/terminal.c#L3281 This function is 1830 lines long. It's reasonably well structured I think. Although the #if 0 are maybe not so good.
- mattlondon 3y agoA lot of those if-/case-blocks are precisely where I'd put functions :) If you changed a bunch of those to separate, pure (i.e. side-effect-free) functions it would if nothing else make unit testing a breeze, and then you'd be free to fix bugs in the logic without fear. As it is, if I had a bug in that huge function I'd be really worried about breaking some edge-condition or implied-state 500 lines up etc.
- fingerlocks 3y agoThey can’t be side effect free, that’s the point. The switch statements are mutating the input.
- Turskarama 3y agoThere's a lot that can be, take this for instance (line 3336): if (c == '\033') term->print_state = 1; else if (c == (unsigned char)'\233') term->print_state = 2; else if (c == '[' && term->print_state == 1) term->print_state = 2; else if (c == '4' && term->print_state == 2) term->print_state = 3; else if (c == 'i' && term->print_state == 3) term->print_state = 4; else term->print_state = 0; This could be turned into a pure function that takes c and print_state as input, and returns a new print_state which the outer function assigns. That's 12 lines turned into 1. term->print_state = newState(c, term->print_state); (I am not a C developer so the sytax could be wrong). Just because the outer function is impure doesn't mean it can't in turn call pure functions.
- BenFrantzDale 3y agoI totally agree. And you can always write mutating “functions” (what get called “algorithms” in the C++ world), like C++’s `std::ranges::sort(range)`.
- couchand 3y agoIt's a Mealy state machine, it should be encoded as a state machine! Why some folks will absolutely insist that a series of manually-written case statements in a single thousand line function is the epitome of style, when there's a pure state machine model inside wishing you would free it from the shackles of if else if else if else....
- npteljes 3y agoGoing further, I'll take a 1000-line shitty code, over split-to-small-functions shitty code. In the long code, all I have to think about is the code. With the functions, I have to pay attention to what calls what, also also because the code is shitty, surely the function names also are, adding two things at the same time to the confusion mix.
- Gibbon1 3y agoEven better instead of a interrupt driven state machine implemented as a switch statement that linearly progresses. Do it using 20-25 small chained callbacks spread over a couple of files. Bonus: Uncle Bob teaches us not to use comments.
- andrewprock 3y agoThis sounds like a nightmare. One that I've lived through.