I am now thinking of introducing a new self imposed rule of breaking functions up when they go above 10 lines to ensure I don't have too much happening as I often don't make small enough reusable parts and my code gets too complex. Opinions?

  • 3
    10 maybe a little more or a little less a count to stick with. Any small "logical" unit should be a function
  • 1
    Hmmm I guess it depends, what the function is supposed to do, coding style and language.

    I try to keep my functions as small as possible but my average is 13 to 16 lines, on my current project I only have two functions of around 20 lines, python makes it difficult to condense some stuff sometimes but it also makes others easier.

    It also depends, are we talking about regular non class functions or class member functions? Class functions should be short and sweet but let's say a main function/proc could take more lines obviously.

    I say just keep things clean, understandable and maintainable. I personally don't care if you write 30 lines as long as the code is as small and efficient as possible while ensuring I can understand what it does.
  • 3
    Sometimes I have to make functions 50 lines or longer because otherwise it just doesn't work.

    I usually don't think about what functions to create. I just code and then I'm like "hey this can be put in a separate function" and in the end it resolves to somewhat around 5 to 30 lines. But I don't say "this function is too long, I have to split it"
  • 1
    that's nonsense. line limit on a function is as arbitrary and nonsensical as a line limit on a paragraph.

    they're supposed to be separated by being about different idea, not by a character count.
  • 0
    I should ha e phrased this as 'if a function goes above 10 lines take a good look at it and look for logical areas to break out'
  • 0
    @craig939393 better, but 10 lines still seems like a stupidly small number. you start getting lost in the indirection instead of in the text on the screen.

    also not like i know too much about today's compiler optimization, but i shudder to imagine the stupid overhead (size, perf, complexity) of numerously more and more layers of function calls until you get to the actual functionality.
  • 0
    @irene yes, i know, but how many people use them, and then how many use them correctly?
    therefore, it seems more relevant to ask about optimizations that compiler tries to do automagically
Add Comment