Recent

Author Topic: How many lines is too many lines ?  (Read 6034 times)

440bx

  • Hero Member
  • *****
  • Posts: 6329
Re: How many lines is too many lines ?
« Reply #15 on: March 06, 2026, 07:08:19 pm »
You can do bad stuff with any coding paradigm. That isn't a flaw of the paradigm.
I didn't say anything about any paradigm.

And, in case you say some are more likely to be gotten wrong. Well, some may require more study how to do them. Doesn't make the paradigm less good. It may even be better. (just not for someone who does their first hello world with it).
How about we don't bring paradigms into the subject of how many lines are too many in a function/procedure and since you brought the paradigm thing into the subject, I'll now add... method.   That way, it's clear that the discussion is fully paradigm independent as it includes them all equally.

The first reads a bit like a "trick question"...
No.  No trick question.

1. What's easier ?  a) to read 1000 lines of code in a single block or b) read 20 out of line functions/procedures that implement that one "macro" function ?

2. Isn't the fact that the programmer's attention has to jump 20 times (or at least to 20 different places) to the "sub-functions" a form of spaghetti code ?

Please answer the questions individually with specifics to provide a solid foundation for your answers.

you didn't answer them because no matter how well named a function or procedure may be, it doesn't tell you anything about how it is implemented which is just as important to know as how the function that _uses them_ is implemented.

If you find code that says
Code: Pascal  [Select][+][-]
  1. MemberList.SortByKey(skLastName);
Do you really need to jump there and read that code?
Yes, I do.  Eventually I will read the code to ensure the method/function/procedure does what its name says and not only that, I'll read it to find out _how_ it does it because that can also be extremely important.

That means that if you have 20 such functions, now the programmer - presuming a responsible programmer that gets acquainted with the code he/she is maintaining - has to jump back and forth among 20 pieces of code to assemble all this stuff in order to figure out how the whole hangs together.

And, well, if there is a bug somewhere, and I don't know in which routine? (Ignoring test cases, and test-ability improvements by having some code extracted), I can on the first run step over each function and check the result, until I find the routine that returns wrong.
You are thereby acknowledging that your attention has to jump around looking for the bug.  Wouldn't it be a lot easier if the code was simply linear and your attention didn't have to jump around ?

Not to mention that you wouldn't have to wonder if that piece of code is used in more than one place (which is very important to know when you're maintaining code.)

So, having stuff in functions is much easier to handle.
That's a nice claim but where is the support for it ?

why should the statements be outside the context that uses it (the main/macro function) ? after all, if the function is 30 statements, the statements could simply be prefaced with a comment stating "the following sort - whatever it is that it sorts - by - whatever key it uses" instead of being "somewhere" and the programmer wouldn't have to search the source code nor wonder if that code is also used someplace else.  Those are two very specific reasons for the code NOT to be in a separate function.  The fact that the statements fulfill a subtask/subfunction can simply be noted with a comment.

And I don't see this in any way fulfilling the criteria for Spaghetti code. If anything it adds structure, hence reduces the spaghetti factor. It is also likely to reduce nested conditions, loops and sets clear bounds for conditional code flow, which also reduces the spaghetti factor.
It definitely fulfills the criteria for spaghetti code... you're reading code... you see a function... you need to know how the function performs its task... your focus must be redirected to where the function is located (goto function)... read the function's statement... go back to where the function was called (hopefully you remember that)... resume reading and repeat for every function... what sauce would you like with that spaghetti ?

And just naming a block of code, and expressing that it does a task that is distinct enough to qualify for being separated, is a good reason too. It gives very valuable info to the reader.
Doesn't a comment accomplish that much without having to divert the programmer's attention to some other place and then having the programmer's attention come back to the original spot to continue reading ? ... not only that makes me think of spaghetti code, it also sounds like ping-pong programming.


And in case of 1, its just a what it is, if the reader thinks its called from other places too, then there is no harm at all. None, nada, zero.
Not true at all.  The maintainer bears the burden of having to determine that the code is not executed by any other code to ensure any changes to it do not affect any other part of the program.  If the code was not in a separate function/procedure/method, the programmer would automatically know that code isn't executed anyplace else because it is NOT in a function/procedure/method that can be executed by another caller.

I will make a categorical statement: no code should be in a named function/procedure/method UNLESS it is executed by more than one caller.  if there is only ONE caller then that code should have been inline.   

The majority of programmers (myself included) often create a function or procedure simply to assign a name which exposes the logical task performed by a group of statements.  THAT is a very unfortunate habit that is inherently incorrect. 

How does having to read copious amount of extra lines every time you need the bigger picture solve that. Keeping the code inlined just increases the likelihood to overlook parts of it.
They are not extra lines, they are the statements that implement the function and they are less "copious" if they are kept in one place than broken into a bunch of subtasks.

Also again, they should be appropriately named. They should just do one thing and that is in the name and hence no way to forget it.
I agree with that.  It's nice to have the jigsaw puzzle piece reasonably well named, that hopefully saves a little time and lessens the headache of putting the pieces together.

Another question...

What do you think gave someone the idea of creating inline/anonymous functions/procedures ?  Why would anyone come up with those ?  What problem(s) do they solve ?




If it does more than one thing and it does make sense to logically compartmentalize them. E.g. if you have a function that should load a file, parse the contents and initialize a GUI based on that contents, it should be at least two functions (file handling, and UI handling), maybe even three (file reading, deserialization, UI handling).
I will admit that I would also break that code into logical functions in spite of the fact that they shouldn't be.  I have done that countless times but, as a result of the language not providing the features I'd really like to have.  The breakage is a "workaround" for those missing features.

A good indicator if a function is too long is actually if you have "headline comments", e.g. you have a code block:
Code: Pascal  [Select][+][-]
  1. // read file
  2. ...
  3. // parse file
  4. ...
  5. // update ui
  6. ...
If you have dedicated sections in your functions that you can give headlines to, then just take those code pieces and put them in a function with your headline as name. This is why I really love nested functions, because sometimes you just want a "named block" and not some code fragment visible to the rest of the file.
Essentially, you are making a function/procedure based on "major" logical boundaries.  I'm also guilty of doing that, mostly because as previously stated, there are some missing "grouping" features in the language.

The second condition when you may want to break up a function, if you do the same thing multiple times.
That's the only _valid_ reason to have a separate function or procedure (allowing for "same" to include sameness as a result of parameters.)

Everything below 10 lines of code if it's not a placeholder, no-op or mathematical formula is generally to short for me to care.
I'd say that any function/procedure that does _not_ fulfill an obvious and complete _logical_ task is not only too short but extremely bad programming.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

creaothceann

  • Sr. Member
  • ****
  • Posts: 316
Re: How many lines is too many lines ?
« Reply #16 on: March 06, 2026, 07:21:07 pm »
Consider it philosophical but, I believe the philosophy should rest on solid technical reasons.  Just in case, "a solid technical reason" is _not_ a limit imposed by a compiler's implementation.  IOW, that a compiler cannot handle a function/procedure that has more than X lines is _not_ a valid reason to limit the number of lines in a function or procedure (it's an excellent reason to improve the compiler.)
The advantage of limits is that they can be defined so that certain internal variables have a smaller size. (A simplified example: if identifiers have an arbitrary length they'd perhaps need a string represented by a pointer. If we were to restrict the number of identifiers to 65,536 or less, we could represent the identifier as a 16-bit integer. That's 2 bytes vs 8 bytes, which might be a significant improvement when CPU caches are involved.)

- - -

About the longest subroutine I can imagine (for now) would be something like a bytecode interpreter, where each byte of the program does several things, and each one has to be on its own line. I wouldn't want to break that up.

An example would be a SNES emulator. The CPU's core (WDC 65c816) interprets 256 opcodes. But it's actually compatible with its predecessor (MOS 6502) when a special flag in the status register is set, and there are some slight differences even in opcodes that perform the same task. So you could actually decide to implement 512 opcodes. Each opcode takes somewhere between 2 and 9 cycles, let's take 5 as the average, and each CPU cycle would be its own line. That results in at least 2,560 lines just for the cycles. There could be more depending on how the opcodes are formatted.
« Last Edit: March 06, 2026, 07:23:05 pm by creaothceann »

440bx

  • Hero Member
  • *****
  • Posts: 6329
Re: How many lines is too many lines ?
« Reply #17 on: March 06, 2026, 07:40:03 pm »
The advantage of limits is that they can be defined so that certain internal variables have a smaller size. (A simplified example: if identifiers have an arbitrary length they'd perhaps need a string represented by a pointer. If we were to restrict the number of identifiers to 65,536 or less, we could represent the identifier as a 16-bit integer. That's 2 bytes vs 8 bytes, which might be a significant improvement when CPU caches are involved.)
but limits were useful in the days of 8, 16 and 32 bit.  In the age of 64 bit and demand paged memory, the value of limits has gone down significantly.

Personally, I believe the limits now should be _very_ generous and fixed upon application start but, susceptible of being overriden if necessary (on the command line or some other mechanism.)

For instance, if a compiler allocated 1GB of memory upfront and is handed a "hello world" type program (a tiny program), on Windows, that would end up using only a very small amount of that GB because the compiler would not access memory it didn't need, therefore the only cost of allocating 1GB is a few entries in the  O/S' virtual allocation tables.  In today's machines, that's insignificant.

The problem is that many, many programs have legacy designs that are suited for older hardware of lesser capabilities.  IOW, they don't take advantage of modern architectures.

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12291
  • Debugger - SynEdit - and more
    • wiki
Re: How many lines is too many lines ?
« Reply #18 on: March 06, 2026, 08:52:05 pm »
You can do bad stuff with any coding paradigm. That isn't a flaw of the paradigm.
I didn't say anything about any paradigm.
Maybe the wrong term to describe it: the concept and application of outlining some code into their own procs. Whatever you want to call it. Because that "concept" (and I use that word only because I need some word to name it) is my answer to what limits the amount of lines.

"Limits" not by applying a limit, but by site effect of doing the outlining, less function have greater amount of lines in them.

Quote
And, in case you say some are more likely to be gotten wrong. Well, some may require more study how to do them. Doesn't make the paradigm less good. It may even be better. (just not for someone who does their first hello world with it).
How about we don't bring paradigms into the subject of how many lines are too many in a function/procedure and since you brought the paradigm thing into the subject, I'll now add... method.   That way, it's clear that the discussion is fully paradigm independent as it includes them all equally.

The first reads a bit like a "trick question"...
No.  No trick question.

1. What's easier ?  a) to read 1000 lines of code in a single block or b) read 20 out of line functions/procedures that implement that one "macro" function ?
The answer is definitely "b".

Reading 1000 lines in a single block is absolut hell.

Even the regex function (not mine, just did some contribution at some point) is such that I don't need to ever read 1000 lines in a go. Each case is stand alone, and code in the case section next to it has nothing to do with it, but is entirely unrelated code.


Quote
2. Isn't the fact that the programmer's attention has to jump 20 times (or at least to 20 different places) to the "sub-functions" a form of spaghetti code ?
No, it is not.

https://en.wikipedia.org/wiki/Spaghetti_code


Quote
Please answer the questions individually with specifics to provide a solid foundation for your answers.

As for the first question: I can only tell you what my experience is.  And I can tell you that lots of code in the wild that I have come across at least to some noticeable degree seems to follow the described goal. But that is arbitrary, as its just the code I came across.

As for 2, since you allege the "spaghetti", why don't you do the work of finding a solid foundation.

My browsers SEARCH did not find a reference to subroutines on Wikipedia. First criteria there is "contral flow" (aka condition/loops) => they don't change by outlining into a proc. Well if anything they get less a problem, because the end of the function provides a tighter boundary for conditional/loop jumps.

2nd on wikipedia is "absence of structure" but procedures are providing structure by defining bounds for the contained functionality.

So according to wikipedia, using outlining into procs has at least 2 points that reduce spaghetti

Quote
you didn't answer them because no matter how well named a function or procedure may be, it doesn't tell you anything about how it is implemented which is just as important to know as how the function that _uses them_ is implemented.
...
Quote
Yes, I do.  Eventually I will read the code to ensure the method/function/procedure does what its name says and not only that, I'll read it to find out _how_ it does it because that can also be extremely important.
...
Quote
That means that if you have 20 such functions, now the programmer - presuming a responsible programmer that gets acquainted with the code he/she is maintaining - has to jump back and forth among 20 pieces of code to assemble all this stuff in order to figure out how the whole hangs together.

So you have read every function in the RTL, LCL, components that you ever used?
Did you ever use any close source environment, where you could not check the RTL by yourself, but had to trust it does what it says?

But anyway, then yes, for you this will pose a problem. I can say (and again arbitrary selection) you are the first programmer that I met who does this.

Btw, what is your opinion on the implementation of "writeln"? SCNR.

But back to serious, even if... E.g. if I had to judge some code for a company that wanted my to certify it for their use. I would rather read 20 small routines, than 1 large.
I could make my notes on each of them, and be done with each of them.

Of course, I am talking about a scenario were they have been properly chosen. Where they don't have unpredictable side effects. So that once I did read them, and put them an me "ok list", I do not need to read them again.

If there are all in one large block, then when I read any of those blocks (assuming that it is well marked as a block) I still have to check that it doesn't expect any side effect in any of the local vars that it uses (because the locals are all in one scope). With outlined routines, each has its own locals, that alone can reduce the work I have. And reduce it by way more than the few codetool jumps that I have to do.

Mind you that, if (what should not happen in first) I need to see other code, then I have the same problem in case of a single large function. The other code will most likely not fit into the editor window. Though in both cases I can solve that by opening several windows. But point is, a single large block of code would not help with this at all. (not for me)





Quote
And, well, if there is a bug somewhere, and I don't know in which routine? (Ignoring test cases, and test-ability improvements by having some code extracted), I can on the first run step over each function and check the result, until I find the routine that returns wrong.
You are thereby acknowledging that your attention has to jump around looking for the bug.  Wouldn't it be a lot easier if the code was simply linear and your attention didn't have to jump around ?

No, there is no attention jumping. In fact there is less attention needed.

I can narrow down on the location of the bug by stepping over all the outlined code, and find the one that returns wrong. That is less work, than continuously looking for the end of the next logical block, and have the debugger run to that end....

And way less than stepping over every statement individually (unless I get lucky and the bug is right at the top).

And once I have to step in (into one single method) both debugger and codetools do the work for me. Since at this point I verified that the params passed in a correct, any code before does not matter when I debug the called function.

But in a rare case were the bug is an interaction (e.g. a dangling pointer in one place causes cross  function side effects), then I need 2 editors to see both function.
Only, if all was in a single large block, the chances that the other part was scrolled out are reasonably high. So I need the same 2 editors too.

So far having the separate function has always paid out for me.

Quote
Not to mention that you wouldn't have to wonder if that piece of code is used in more than one place (which is very important to know when you're maintaining code.)
In case it was scoped for global access.... Which would mean the outlining was done badly. Not a fault of outlining, a fault of whoever did this particular case.

Otherwise, with a smaller scope, the IDE has plenty of tools to tell me in a few seconds. Yes, it will take a few seconds. Those I can take out of the thousands of seconds that I have saved by outlining.

Sorry, but I have to say, this above quote of yours looks like you a constructing cases where someone has done "not what I advertised", but a completely wrong way of outlining. So that what you describe is a problem that doesn't exist (in what I advertised).
Otherwise lets take single large block and find ways for someone to write them in the worst possible way....


Again, all I have is my experience with this.
O well, and from several decades ago, when I was a newby, the (bad) experience of not doing this.


Quote
So, having stuff in functions is much easier to handle.
That's a nice claim but where is the support for it ?

Well I haven't seen any evidence that "single large" is better. Only your word. So at this point I can only say for you (and potentially only you, and a few people you know (may be hundreds, still few)) this is the perceived case... And that even assumes that you tried otherwise.

I have (involuntary) tried otherwise (your single large way), when I was a beginner.



Quote
why should the statements be outside the context that uses it (the main/macro function) ? after all, if the function is 30 statements, the statements could simply be prefaced with a comment stating "the following sort - whatever it is that it sorts - by - whatever key it uses" instead of being "somewhere" and the programmer wouldn't have to search the source code nor wonder if that code is also used someplace else.  Those are two very specific reasons for the code NOT to be in a separate function.  The fact that the statements fulfill a subtask/subfunction can simply be noted with a comment.
I did several comment about commenting blocks. In this and in my previous post. Simply not the same.



Skipping several repeats on spaghetti and comments.


Quote
And in case of 1, its just a what it is, if the reader thinks its called from other places too, then there is no harm at all. None, nada, zero.
Not true at all.  The maintainer bears the burden of having to determine that the code is not executed by any other code to ensure any changes to it do not affect any other part of the program.  If the code was not in a separate function/procedure/method, the programmer would automatically know that code isn't executed anyplace else because it is NOT in a function/procedure/method that can be executed by another caller.

I will make a categorical statement: no code should be in a named function/procedure/method UNLESS it is executed by more than one caller.  if there is only ONE caller then that code should have been inline.   
So you copy/paste any code from components, if you call it from only one place?

Changing code is usually less frequent than reading it. That follows that before a change, one would usually read it.

As I wrote above, the check were it is called takes but a few seconds. Of which plenty have been saved by having it outlined. (Again, I can only tell what it does for me, and those I know)


Quote
The majority of programmers (myself included) often create a function or procedure simply to assign a name which exposes the logical task performed by a group of statements.  THAT is a very unfortunate habit that is inherently incorrect. 
I seem to recall that you earlier stated such a function would be a total surprise for the reader. Now you state the majority create them themself?

For reference, point 3: https://forum.lazarus.freepascal.org/index.php/topic,73608.msg577862.html#msg577862


Quote
How does having to read copious amount of extra lines every time you need the bigger picture solve that. Keeping the code inlined just increases the likelihood to overlook parts of it.
They are not extra lines, they are the statements that implement the function and they are less "copious" if they are kept in one place than broken into a bunch of subtasks.
Parsing a text into tokens by some rule, and then sorting those tokens => that are 2 functions. I will not put them into a single function.
When I have to read one of them, I see no need to have the other presence and wonder if it has any reason to be there.

If you want to put the together suit yourself.

Again, I can only give you my opinion. There may be more info on it (I am certain there is, I am certain to have read it in the past), but
I don't want to spent time on searching it. You want to know, you can do the work (sorry)


Quote
Also again, they should be appropriately named. They should just do one thing and that is in the name and hence no way to forget it.
I agree with that.  It's nice to have the jigsaw puzzle piece reasonably well named, that hopefully saves a little time and lessens the headache of putting the pieces together.

Another question...

What do you think gave someone the idea of creating inline/anonymous functions/procedures ?  Why would anyone come up with those ?  What problem(s) do they solve ?

Indeed other question. I don't actually like them much in Pascal. There is some point for "closures" but they could have been done without anonymous.

The same goes for: I prefer "s:= inttostr(1);" and not "s:= 1.toStr;" or similar.

There are languages that are differently designed, and in which method chainging is great. It isn't in Pascal. Languages come with a style. Keeping to that keeps the readable between different users. Copying styles from elsewhere.... But very off topic now.

440bx

  • Hero Member
  • *****
  • Posts: 6329
Re: How many lines is too many lines ?
« Reply #19 on: March 06, 2026, 09:56:47 pm »
I see that the jumping back and forth is well ingrained in your mind.  Unfortunately, you're in good company.

It has been clinically proven that linear causality is the easiest one for the human brain to understand.   I found some sites that support the point but none that are bona-fide research centers.  For this reason, I didn't provide any links but, I believe that it should be fairly evident that structures that simply sequential are easier to understand than those that are not.  I have the research material somewhere, if I ever find it, I'll post it but, there are credible sites that support the point which can be found using google.

That alone proves that having to jump from one "function name" to "function implementation" and back to the spot where "function name" occurred takes greater mental effort than simply reading sequential code.  This is particularly true as the nesting level increases.   It's also the reason why Lazarus itself includes a feature to jump to the implementation and back to the origin (it saves the human brain from having to remember this worthless but necessary stuff.)

Also demonstrated is that most humans have a tendency to lose their ability to keep track of where a logical fact resides when the nesting level reaches 7 or more.

I fully understand the strong desire programmers have to group statements and identify the group with a name that declares the purpose of the statement group.  That's what - erroneously - leads to define functions or procedures even when those groups of statements are not used anywhere else.  (Even I am guilty of that.)

Now I am going to _prove_ that a single block of a 1000 lines of code is more desirable than 20 (or so) individual blocks.  Here:

{%region /fold SortByWhateverKey long comment can follow}
{%endregion}
{%region /fold EliminateDuplicates comment}
{%endregion}
{%region /fold PackTable comment}
{%endregion}
{%region /fold DoMoreStuff comment}
{%endregion}
...
...
{%region /fold Step20TheLastStep comment}
{%endregion}

Now, the beauty of the above is that the programmer can logically group statements and the statements take no space.  This is what the creation of separate functions/procedures accomplishes but, the above

1.) does NOT force the programmer to jump around to find the code
2.) makes it crystal clear that the code is not used anywhere else
3.) if convenient, you can see multiple blocks at once (which are not necessarily contiguous.)

In this particular case the construction relies on something provided by the IDE instead of the compiler.  That's very unfortunate because it is the compiler that should offer a way of grouping statements sequentially instead of forcing the programmer to bundle them in some other location (forcing the programmer's attention to jump to that location whenever he/she wants to find out what the thing does (or how it does it.))  It's a deficiency in the language.


FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12291
  • Debugger - SynEdit - and more
    • wiki
Re: How many lines is too many lines ?
« Reply #20 on: March 06, 2026, 10:29:57 pm »
I see that the jumping back and forth is well ingrained in your mind.  Unfortunately, you're in good company.

It has been clinically proven that linear causality is the easiest one for the human brain to understand.   I found some sites that support the point but none that are bona-fide research centers.  For this reason, I didn't provide any links but, I believe that it should be fairly evident that structures that simply sequential are easier to understand than those that are not.  I have the research material somewhere, if I ever find it, I'll post it but, there are credible sites that support the point which can be found using google.

That alone proves that having to jump from one "function name" to "function implementation" and back to the spot where "function name" occurred takes greater mental effort than simply reading sequential code.  This is particularly true as the nesting level increases.   It's also the reason why Lazarus itself includes a feature to jump to the implementation and back to the origin (it saves the human brain from having to remember this worthless but necessary stuff.)
Oh, if I had time, that should be interesting.

I wonder how they explain how millions cope with their native language. Look at how different language "build" numbers (and well, it seems to me, what matters is not the order, but what one grew up with).

94 =>
English: Ninety four
German: Four and ninety
French: Four twenties and fourteen
well then again 87 in American English: Four score and seven

And other stuff, e.g. where in the sentence a "not" goes, and what it negates....




Quote
Also demonstrated is that most humans have a tendency to lose their ability to keep track of where a logical fact resides when the nesting level reaches 7 or more.
Afaik the amount of thinks your short term mem can hold.

But on the whole, when looking at the code, you don't have to remember if you got there by
- page up
- alt up (declaration jump)

In either case your brain (should) care about the 2 code fragments, and the execution order of them.
Where they are located in the editor, and which keypress did get you there is not important and does not need to be remembered.

And also, never mind how where they are located in the editor, you take the same path of discovery, and therefore have the same order of reading.

People have read books for a long time, and I have not encountered someone yet who told me, I am fine reading a page, but every time I turn it, I completely forgot what was on the previous page. But if you are connecting many items then even if they occur sequentially, they will at some point not be on the same page. But on a computer you can always bring both (or more) pages on the screen.
So there is no difference to reading order or ability to look at different parts easily.
There is only a difference in having certain places, clearly marked as logical borders. Which reduces the amount of "points" you need to keep in your short time memory, and frees up some of those 7 slots, if you use outlined functions.

Quote





I fully understand the strong desire programmers have to group statements and identify the group with a name that declares the purpose of the statement group.  That's what - erroneously - leads to define functions or procedures even when those groups of statements are not used anywhere else.  (Even I am guilty of that.)

Now I am going to _prove_ that a single block of a 1000 lines of code is more desirable than 20 (or so) individual blocks.  Here:

{%region /fold SortByWhateverKey long comment can follow}

Now, the beauty of the above is that the programmer can logically group statements and the statements take no space.  This is what the creation of separate functions/procedures accomplishes but, the above
Except the debugger doesn't step over a folded block, if I want that....

But, yes. I have done that for code where there was not a clear enough "this is separate functionality", and therefore a real cut wasn't possible.

It all depends what the code inside is. I have stated from the beginning that the outline code must have its one standing.
I have never said take arbitrary blocks of n lines....

Of course the comments you added, indicate that those are likely separate functions. So then I would outline the code.

Quote
In this particular case the construction relies on something provided by the IDE instead of the compiler.  That's very unfortunate because it is the compiler that should offer a way of grouping statements sequentially instead of forcing the programmer to bundle them in some other location (forcing the programmer's attention to jump to that location whenever he/she wants to find out what the thing does (or how it does it.))  It's a deficiency in the language.

Well you could have the code in several functions  (need to add some frwrd declares)
Code: Pascal  [Select][+][-]
  1. procedure Bar;
  2. begin
  3. ....
  4. Foo;
  5. end;
  6. procedure Foo;
  7. begin
  8. ....
  9. Abc;
  10. end;
  11. procedure Abc
  12. begin
  13. ....
  14. end;
  15.  

Afaik the compiler can optimize the tail call into a jump. So that should be perfect for you.

440bx

  • Hero Member
  • *****
  • Posts: 6329
Re: How many lines is too many lines ?
« Reply #21 on: March 06, 2026, 10:47:03 pm »
People have read books for a long time, and I have not encountered someone yet who told me, I am fine reading a page, but every time I turn it, I completely forgot what was on the previous page.
That makes my point.  Usually, there won't be anyone claiming they completely forgot what was in the previous page because the previous page is in a linear sequence with the next page.  Now, ask someone what was 10 pages before the one they are reading and most readers won't be able to answer, that's what having 20 separate functions is like, it's no longer sequential.

That forces having to physically go where the information is located, refresh one's memory, then go back to the original page to resume reading.  Way more work which is unnecessary in a linear construction (but the linear construction yields a single block which some programmers don't seem to like much.)
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

TBMan

  • Sr. Member
  • ****
  • Posts: 352
Re: How many lines is too many lines ?
« Reply #22 on: March 06, 2026, 10:53:00 pm »
My old fashioned 2 cents is this.

I use a function as a way to get a result.   For instance, in my solitaire games I would use something like
Code: Pascal  [Select][+][-]
  1.   function CardCanPlayonTable(Stack:TableStackType;Card:CardType):boolean;
  2.  

to tell me if a card can be moved onto another card according to the rules of the game.  Doing this helps when I
am writing multiple games (which I do). I just have to change the guts of the above function to reuse code
(cut and paste) from another game.

A procedure contains a series of tasks to achieve a goal. This goal can be running an entire app or just drawing
a playing card on the screen.   I like to keep my procedures relatively short though, but it really depends on what the
procedure is doing. In objected orientated programming, the method that is used to run an app can be long as it would have a lot of responses to actions by the user. Each response to the action though should be a separate procedure.
Barry

Newest game (clone),
Missile Commander:
https://www.youtube.com/watch?v=tgKz0cxog-k

Lauriet

  • New Member
  • *
  • Posts: 45
Re: How many lines is too many lines ?
« Reply #23 on: March 07, 2026, 01:54:16 am »
If you need a comment to explain the code, then it should be in a procedure with a good descriptive name.
My philosophy is that your grandmother should be able to read it and know whats going on. A program should almost read like prose.
There are a few good books about good programming style...get one and read it.
If I had to work on a procedure that long, I'd quite my job. It shows an illogical mind.

e.g.

for x := 0 to 56 do  {-- sum all x values }
  j := j + Z
  • ;


Now are x, j, z local or global ? Why 56 ? We know its creating a sum, but off what?

this should be something like:

function SumAllData(var StudentHeight : StdHArrayType) : Integer;
var
  Counter ,
  Result : Integer;
begin
  Result := 0;
  for Counter := 0 to LengthOfArray do
     Result := Result + StudentHeight[Counter];
  SumAllData := Result;
end.

Now we have a heading that explains what the function does and what its input/output are. No comments needed.
We have variable names that explain what they do.
Now we can forget about the contents of the function and know what it does.

In a 1000+ lines of code there MUST be lots of places that can be abstracted away.
You will be doing yourself a favour, and more importantly anybody else that has to read it.

mas steindorff

  • Hero Member
  • *****
  • Posts: 568
Re: How many lines is too many lines ?
« Reply #24 on: March 07, 2026, 03:04:40 am »
My units tend to top out a 2K line or less now days. This is do to two things.
1: I like to be able to quickly find procedure in the list generated when I use [Alt][G]. Too many functions or procedures and I have to remember some subpart of the procedure's name. a task made harder when I was not the one who named it. otherwise I need to dig down to one of the places where it was used to track it down. 
2: unit testing: any module with 3K+ lines more and this becomes an almost imposable task. most programmers I know will not even try to setup any external testing code unless they can see a target summery of the result what each function/procedure is to do. That's fine for hobbits but skipping unit testing is not professional. 

The nice thing about "object" pascal is re-useable code. how much work of your 50K unit can be used in your next project?
MAS 
 
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

440bx

  • Hero Member
  • *****
  • Posts: 6329
Re: How many lines is too many lines ?
« Reply #25 on: March 07, 2026, 03:38:03 am »
If you need a comment to explain the code, then it should be in a procedure with a good descriptive name.
using your way of thinking... if you need a procedure with a good descriptive name  then you can simply have an even better comment for the group of statements and not force the programmer to divert his/her attention away in order to view/read the statements.



The nice thing about "object" pascal is re-useable code. how much work of your 50K unit can be used in your next project?

I don't really want to bring OOP into the discussion as it really doesn't apply but, in the case of reuse it is quite obvious by the unjustified size of the executables, that there is way, way too much "reuse" taking place.  Executables that would only be a few Ks in procedural programming commonly take over a megabyte in "object" Pascal.  No doubt, lots and lots of "reuse" not to mention lots of very low line count methods that do _not_ accomplish a logical function, they exist only because the code they inherited from was either incomplete or needed to be "customized" by overriding some trivial characteristic (such as color or some other trivial thing.)

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Lauriet

  • New Member
  • *
  • Posts: 45
Re: How many lines is too many lines ?
« Reply #26 on: March 07, 2026, 04:49:55 am »
Well, you see, that's where you have missed the point. With a good procedure/function name you do NOT have to divert your attention anywhere, you automatically know what the bit of code does, 'cause it says so and tells you what data structure it uses.

You asked the question, why argue with any answers

440bx

  • Hero Member
  • *****
  • Posts: 6329
Re: How many lines is too many lines ?
« Reply #27 on: March 07, 2026, 05:27:57 am »
Well, you see, that's where you have missed the point. With a good procedure/function name you do NOT have to divert your attention anywhere, you automatically know what the bit of code does, 'cause it says so and tells you what data structure it uses.

You asked the question, why argue with any answers
Apparently you're the one who missed the point.  No matter how well named a function or procedure may be, that doesn't give all the details as to how it is implemented.

Maybe you are one of those people who simply does not care how it's implemented and as long as the thing is called "sort" you'll use it and believe it is doing a great job in spite of being a bogosort (and that's hoping it is actually a sort procedure and... it is bug free... that's what programming is all about... use stuff you don't check nor understand... way to go!.)



FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1225
Re: How many lines is too many lines ?
« Reply #28 on: March 07, 2026, 06:01:06 am »
This whole conversation is a well educative topic.

creaothceann

  • Sr. Member
  • ****
  • Posts: 316
Re: How many lines is too many lines ?
« Reply #29 on: March 07, 2026, 06:19:42 am »
If you need a comment to explain the code, then it should be in a procedure with a good descriptive name.

Comments can still be used to explain - when necessary - why a certain implementation was chosen, what alternative implementation(s) were used before and why they were replaced, and what the limits of the current implementation are.

 

TinyPortal © 2005-2018