Re: [fpc-pascal] Legitimate use of for and break

2023-07-01 Thread Hairy Pixels via fpc-pascal



> On Jul 2, 2023, at 3:25 AM, Steve Litt via fpc-pascal 
>  wrote:
> 
> I tend to put continue statements at or near the top of the block, to
> summarily rule out some obvious irrelevant iterations without all sorts
> of if/then/else nesting. As a practical matter it's more readable and
> more maintainable.

Swift has decided to build this pattern into the language. I kind of like it 
but it's also kind of messy looking and a continue at the top of the block 
often is easier to read for me. In pascal it would look like this:

for item in list where item.value > 10 do
  begin
  end;

Which would be the same as:

for item in list do
  begin
if item.value <= 10 then
  continue;
  end;

or with traditional for loops but this really looks bad.

for i := 0 to list.Count - 1 where list[i] > 10 do
  begin
  end;

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Legitimate use of for and break

2023-07-01 Thread Steve Litt via fpc-pascal
Santi via fpc-pascal said on Sat, 1 Jul 2023 10:22:48 +0200

>El 16/06/2023 a las 16:09, Mattias Gaertner via fpc-pascal escribió:
>> On Fri, 16 Jun 2023 20:51:42 +0700
>> Hairy Pixels via fpc-pascal  wrote:
>>  
 On Jun 16, 2023, at 6:23 PM, Thomas Kurz via fpc-pascal
  wrote:

>> 20 years ago there were some programmers, claiming a loop condition
>> must only be at start or end, but not in the middle.  

>I mostly agree with that programmers. That's called structured 
>programming.  "Break" and "continue" are in fact, a subset of GOTO

I agree, although they're less damaging than goto, which I haven't used
since 1982 using 6800 hex machine language, where I learned how much
less damaging jump to subroutine and return from subroutine are than
goto.

>When you see a structure, a block, you know at the beginning (or end
>of the block) the exit conditions. So you can skip the block and you
>know the conditions after the block. It is very useful when you are
>skimming the code or debugging. You don't have the investigate the
>inner loop to see if there are hidden GOTOs.

I agree, although there are some practical concerns, which you go on to
mention...

>
>But, as any other golden rule, you must know when it makes sense to 
>ignore it.
>I use the break, but only at the beginning of the loop. (or exit in 
>function/procedure)
>And sometimes with deep nested loops, but, when I commit such crime, I 
>highlight it with neon lights in the comments.

I tend to put continue statements at or near the top of the block, to
summarily rule out some obvious irrelevant iterations without all sorts
of if/then/else nesting. As a practical matter it's more readable and
more maintainable.

Also, when I'm in a rush, I'll do while(True) and then just put one or
more breaks in the block, with the full intention of going back and
doing it the right way, but then forget to. I apologize to the
maintenance programmer who follows me, because this is bad practice.

Sometimes I use breaks when the loop test condition would be so hairy
as to be difficult to understand. This happens a lot when things get
very stateful. But your post got me to thinking that maybe this is a
symptom that I made a poor design.


>
>I really hate having the read the full code to guess whats happening. 
>Structured programming  is your friend.

Xactly!

SteveT

Steve Litt 
Autumn 2022 featured book: Thriving in Tough Times
http://www.troubleshooters.com/bookstore/thrive.htm
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Legitimate use of for and break

2023-07-01 Thread Santi via fpc-pascal

El 16/06/2023 a las 16:09, Mattias Gaertner via fpc-pascal escribió:

On Fri, 16 Jun 2023 20:51:42 +0700
Hairy Pixels via fpc-pascal  wrote:


On Jun 16, 2023, at 6:23 PM, Thomas Kurz via fpc-pascal
 wrote:

Whether it's elegant is a different question. In my opinion YES
because it often gives better readable code than nested "if"
statements inside the loop. But I've also read that using "break"
is discouraged because it shows a bad choice of the loop range.

This is highly suspect. Doing an early break in loops is the essence
of how to do linear searching. No idea who thinks that's a bad idea.

20 years ago there were some programmers, claiming a loop condition
must only be at start or end, but not in the middle.
I mostly agree with that programmers. That's called structured 
programming.  "Break" and "continue" are in fact, a subset of GOTO


When you see a structure, a block, you know at the beginning (or end of 
the block) the exit conditions. So you can skip the block and you know 
the conditions after the block. It is very useful when you are skimming 
the code or debugging. You don't have the investigate the inner loop to 
see if there are hidden GOTOs.


But, as any other golden rule, you must know when it makes sense to 
ignore it.
I use the break, but only at the beginning of the loop. (or exit in 
function/procedure)
And sometimes with deep nested loops, but, when I commit such crime, I 
highlight it with neon lights in the comments.


I really hate having the read the full code to guess whats happening. 
Structured programming  is your friend.




Gladfully, most programmers came to their senses.

Mattias
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal



--
Saludos
Santi

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal