Recent

Author Topic: Do you number your begin/end blocks?  (Read 3085 times)

silvercoder70

  • Full Member
  • ***
  • Posts: 200
    • Tim Coates
Re: Do you number your begin/end blocks?
« Reply #30 on: October 13, 2025, 02:06:51 pm »
I will typically do something similar to what Bart said in reply #2.

The other thing I would mention is that _try_ to reduce number of levels with something like (see below). I know it was/is only an example but if I can do something and exit...

Code: Pascal  [Select][+][-]
  1. begin
  2.   if not this then Exit;
  3.  
  4.   {this is true here...}
  5.   if not otherthing then
  6.   begin
  7.     ...
  8.     Exit;
  9.   end;
  10.  
  11.   {this and otherthing are true...}
  12.   If anotherthing then
  13.   begin
  14.       ...
  15.   end;
  16. end;
  17.  
     
🔥 Pascal Isn’t Dead -> See What It Can Do: @silvercoder70 on YouTube

wp

  • Hero Member
  • *****
  • Posts: 13209
Re: Do you number your begin/end blocks?
« Reply #31 on: October 13, 2025, 02:30:14 pm »
FPC routines somethis follow strange indentation rules (in my opinion), and it is hard to identify the blocks. The "outline" feature here helps a lot to see that an "end" is missing in the FPImage code shown in the screenshot (of course, I removed the "end" for this demo, the original code is correct): The final "end" of a procedure always must be red (in the default coloring scheme) - here it is orange, and this indicates that an "end" is missing somewhere, without having to do a test compilation.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11799
  • Debugger - SynEdit - and more
    • wiki
Re: Do you number your begin/end blocks?
« Reply #32 on: October 13, 2025, 03:06:20 pm »
Further more, if you have folding enabled, then you can set a different color for the fold-gutter-line of the nearest enclosing fold. (going over any nested folds, if the caret is outside those nested)

TBMan

  • Sr. Member
  • ****
  • Posts: 277
Re: Do you number your begin/end blocks?
« Reply #33 on: October 13, 2025, 03:21:27 pm »
use the Ouline option in IDE Options: https://wiki.lazarus.freepascal.org/Lazarus_IDE_Tools#Outline

I'll try this for a while and see if I can adapt to it.
I love programming.

Some things I've done using PTCgraph:

NFL Retro Football (almost finished):
https://www.youtube.com/watch?v=78mTtsd7ppk


Solitaire games:
https://www.youtube.com/watch?v=zmtxI7FdWuQ&list=PLa4BPpFl34iVhFwX1JZwVm3vE5ay_i3R2

Warfley

  • Hero Member
  • *****
  • Posts: 2021
Re: Do you number your begin/end blocks?
« Reply #34 on: October 14, 2025, 04:24:06 pm »
I will typically do something similar to what Bart said in reply #2.

The other thing I would mention is that _try_ to reduce number of levels with something like (see below). I know it was/is only an example but if I can do something and exit...

Code: Pascal  [Select][+][-]
  1. begin
  2.   if not this then Exit;
  3.  
  4.   {this is true here...}
  5.   if not otherthing then
  6.   begin
  7.     ...
  8.     Exit;
  9.   end;
  10.  
  11.   {this and otherthing are true...}
  12.   If anotherthing then
  13.   begin
  14.       ...
  15.   end;
  16. end;
  17.  
   
But why not use an assertion for this?
Code: Pascal  [Select][+][-]
  1. begin
  2.   if not this then Exit;
  3.  
  4.   {this is true here...}
  5.   if not otherthing then
  6.   begin
  7.     ...
  8.     Exit;
  9.   end;
  10.   Assert(otherthing, 'Guard should have captured if otherthing is false');
  11.  
  12.   If anotherthing then
  13.   begin
  14.       ...
  15.   end;
  16. end;
  17.  
First it serves the same documentation purpse, as when reading the code you immediately see that from this point onwards otherthing should be true, with a textual explanation why this is the case. Additionally, when compiling in debug mode with assertions enabled, when you test your code and you reach that point it checks and if it fails (e.g. you changed your guard and forgot an exit), it will cause an error that you find. While when you compile in release mode the assertions will be thrown out, resulting in no extra code being generated.

I think people are using assertions way to rarely in pascal. In other languages I see this much more commonly used

440bx

  • Hero Member
  • *****
  • Posts: 5809
Re: Do you number your begin/end blocks?
« Reply #35 on: October 14, 2025, 04:38:28 pm »
One construct I use very often is either:
Code: Pascal  [Select][+][-]
  1. for i := i to i do  { scope - not a loop }
  2. begin
  3.   if somecondition then
  4.   begin
  5.     <statements>
  6.     break;
  7.   end;
  8.  
  9.   if othercondition then
  10.   begin
  11.     <statements>
  12.     break
  13.   end;
  14. end; { scope }
  15.  
  16. <more statements here>
  17.  
  18.  
or the same thing using a "repeat <conditions with break> until TRUE;"

That way you can have an "artificial case" statement with conditions that are not all dependent on a constant or a single variable.

It's extremely easy to understand and add conditions in the right place and there is no ifs inside ifs inside ifs which can easily obfuscate the set of conditions that lead to a specific statement to be executed.  No nesting keeps the flow linear (which is the ideal.)

using a "repeat ... until" is often better because that avoids having the compiler sometimes emit a warning about the variable "i" not having been initialized.  Small potato.

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

Zoran

  • Hero Member
  • *****
  • Posts: 1974
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Do you number your begin/end blocks?
« Reply #36 on: October 14, 2025, 06:44:26 pm »
using a "repeat ... until" is often better because that avoids having the compiler sometimes emit a warning about the variable "i" not having been initialized.  Small potato.

You could avoid the warning with "for I := 0 to 0", but "repeat ... until True" is still preferred, as you don't need to declare the unnecessary variable I (and, although I haven't checked, I would also expect a better optimisation without I).
« Last Edit: October 14, 2025, 06:47:50 pm by Zoran »
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

440bx

  • Hero Member
  • *****
  • Posts: 5809
Re: Do you number your begin/end blocks?
« Reply #37 on: October 14, 2025, 07:11:16 pm »
the one thing I like about using a "for" instead of a "repeat" is that the "for" makes it clear upfront that the statements will only be executed once whereas with a "repeat" the programmer has to visually locate the "until" to see that the condition is "until TRUE" to determine the statements will only be executed once.

That's the part about the "for" I like and I sometimes use it when the "until" isn't immediately visible (say about 30 lines or more below the "repeat".)

As far as performance, the "repeat" takes less code but, since it only gets executed once, the difference is just a handful of clock cycles,  not worth sweating.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018