Recent

Author Topic: Pros/Cons of choosing one of the these two methods  (Read 2891 times)

Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 89
Pros/Cons of choosing one of the these two methods
« on: June 07, 2024, 05:56:10 pm »
Both of the ButtonClick procedure variations below work as expected. I am just
wondering if either method has advantages over the other or is it just a matter of style?

The only difference between the two procedures are the SetLength parameters used in
Line #9 vs. Line #15

TIA

Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   A:Array of string;
  4.  
  5. implementation{$R *.lfm}{ TForm1 }
  6.  
  7. procedure TForm1.Button1Click(Sender:TObject);
  8. begin
  9. setlength(A,Length(A)+1);
  10. A[high(A)]:=(Sender as TButton).Name;
  11. end;
  12.  
  13. procedure TForm1.Button2Click(Sender:TObject);
  14. begin
  15. setLength(A,High(A)+2);
  16. A[high(A)]:=(Sender as TButton).Name;
  17. end;
  18.                    
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

Zvoni

  • Hero Member
  • *****
  • Posts: 2782
Re: Pros/Cons of choosing one of the these two methods
« Reply #1 on: June 07, 2024, 07:54:22 pm »
Intuitively, i‘d go with the first, because an unset dynamic array has length 0, while High of an unset dynamic array should be „undefined“, though i accept that it probably returns „something“
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

cdbc

  • Hero Member
  • *****
  • Posts: 1746
    • http://www.cdbc.dk
Re: Pros/Cons of choosing one of the these two methods
« Reply #2 on: June 07, 2024, 08:03:39 pm »
Hi
There seems to be some kind of new(ish) compiler-switch regarding dynamic arrays: {$ModeSwitch arrayoperators}
then you can do:
Code: Pascal  [Select][+][-]
  1. A+= [(Sender as TButton).Name];
Just a thought...
P.s.: I too, would go with the first one.
Regards Benny
« Last Edit: June 07, 2024, 08:10:28 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 89
Re: Pros/Cons of choosing one of the these two methods
« Reply #3 on: June 07, 2024, 08:31:01 pm »
Thank you both. Wow; I can't keep track of the myriad of modes and their commensurate compiler switches.
I wonder how it is determined if a new capability is made available via compiler switches as opposed to
an actual new Pascal command?

Is a new capability introduced via a compiler switch or Macro etc. considered to be NOT breaking the Pascal
Standard whereas actually adding a new command to the language would anger the Pascal purists???

I personally would prefer as many useful commands as possible be added to any language as long as it made sense
to do so. I suspect that programmers as a whole exhibit "purist" /  "OCD" tendencies in all aspects of our lives. And
maybe that is a good thing...
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

Thaddy

  • Hero Member
  • *****
  • Posts: 16300
  • Censorship about opinions does not belong here.
Re: Pros/Cons of choosing one of the these two methods
« Reply #4 on: June 07, 2024, 08:31:35 pm »
There is no real difference in normal use except the second one is over-dimensioned.
FPC string types are #0 terminated anyway, also shortstrings.
One remark about the code though:
You declare it as an array of string, but that can be different. It is actually a TComponentName.
See: https://www.freepascal.org/docs-html/rtl/classes/tcomponentname.html
That does not matter too much too, but the real string type of the array in Lazarus is UTF8String, so implicit conversion will likely take place.
If I smell bad code it usually is bad code and that includes my own code.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 573
Re: Pros/Cons of choosing one of the these two methods
« Reply #5 on: June 07, 2024, 09:42:14 pm »
I personally would prefer as many useful commands as possible be added to any language as long as it made sense
to do so. I suspect that programmers as a whole exhibit "purist" /  "OCD" tendencies in all aspects of our lives. And
maybe that is a good thing...

Not purism or OCD -- I think programmers want a durable, stable platform.

Stefan Glienke

  • New Member
  • *
  • Posts: 24
Re: Pros/Cons of choosing one of the these two methods
« Reply #6 on: June 08, 2024, 01:05:25 am »
Intuitively, i‘d go with the first, because an unset dynamic array has length 0, while High of an unset dynamic array should be „undefined“, though i accept that it probably returns „something“

High is defined as Length-1 so High of an unset dynamic array (which is equal to nil) is -1.
See doc: https://www.freepascal.org/docs-html/rtl/system/high.html

Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 89
Re: Pros/Cons of choosing one of the these two methods
« Reply #7 on: June 08, 2024, 01:14:38 am »
Thaddy,
Regarding being over dimensioned, I thought so also, but when I used
Code: Pascal  [Select][+][-]
  1. setLength(A,High(A)+1)

instead of

Code: Pascal  [Select][+][-]
  1. setLength(A,High(A)+2)

I got improper results when I used

Code: Pascal  [Select][+][-]
  1. For x:=Low(A) to High(A) Do Memo1.lines.add(A[x]);

Maybe using TComponentName would fix tha.t
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

ASerge

  • Hero Member
  • *****
  • Posts: 2347
Re: Pros/Cons of choosing one of the these two methods
« Reply #8 on: June 08, 2024, 04:16:03 am »
I am just wondering if either method has advantages over the other or is it just a matter of style?
In the first case, it is a little faster, in the second - a little smaller code size. Because the Length function is inlined, but the High function is not.
Of course, the first option looks more natural.

Zvoni

  • Hero Member
  • *****
  • Posts: 2782
Re: Pros/Cons of choosing one of the these two methods
« Reply #9 on: June 08, 2024, 11:53:06 am »
Intuitively, i‘d go with the first, because an unset dynamic array has length 0, while High of an unset dynamic array should be „undefined“, though i accept that it probably returns „something“

High is defined as Length-1 so High of an unset dynamic array (which is equal to nil) is -1.
See doc: https://www.freepascal.org/docs-html/rtl/system/high.html
I thought so.
OTOH, consider this snippet
Code: Pascal  [Select][+][-]
  1. Var
  2.    A:Array[-10..-1] of string;
  3.  
High returns -1, too.
That’s the reason why i‘d go with the first one when setting a (new) „length“ of a dynamic array
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Zvoni

  • Hero Member
  • *****
  • Posts: 2782
Re: Pros/Cons of choosing one of the these two methods
« Reply #10 on: June 08, 2024, 11:54:22 am »
Thaddy,
Regarding being over dimensioned, I thought so also, but when I used
Code: Pascal  [Select][+][-]
  1. setLength(A,High(A)+1)

instead of

Code: Pascal  [Select][+][-]
  1. setLength(A,High(A)+2)

I got improper results when I used

Code: Pascal  [Select][+][-]
  1. For x:=Low(A) to High(A) Do Memo1.lines.add(A[x]);

Maybe using TComponentName would fix tha.t
Because High(A)+1 returns 0 for an unset dynamic array
High doesn’t return the count (length) of a dynamic array
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MarkMLl

  • Hero Member
  • *****
  • Posts: 8080
Re: Pros/Cons of choosing one of the these two methods
« Reply #11 on: June 09, 2024, 12:27:30 am »
I'd suggest that if you're using SetLength(), then you should be basing it on Length(). If that doesn't work as expected then it's the RTL developers' problem.

I personally would prefer as many useful commands as possible be added to any language as long as it made sense
to do so.

Definitely not: the underlying language should be as simple as possible, with everything else in libraries loaded (or ignored) as necessary. That is something that Wirth learned between Pascal and Modula-2, although the library concept might have been introduced slightly earlier by IBM.

Having said that, it would be desirable if the language's syntax could be extended, which by and large is not possible for Pascal and its siblings.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Warfley

  • Hero Member
  • *****
  • Posts: 1827
Re: Pros/Cons of choosing one of the these two methods
« Reply #12 on: June 09, 2024, 04:45:46 pm »
Thank you both. Wow; I can't keep track of the myriad of modes and their commensurate compiler switches.
I wonder how it is determined if a new capability is made available via compiler switches as opposed to
an actual new Pascal command?
The big problem is usually backwards compatibility. With Array Operators for example, before ArrayOperators modeswitch was added, you could already overload operators for arrays:
Code: Pascal  [Select][+][-]
  1. type
  2.   TIntArray = array of Integer;
  3.  
  4. operator +(const lhs, rhs: TIntArray): TIntArray;
  5. var
  6.   i, len: Integer;
  7. begin
  8.   result:=nil;
  9.   len := Length(lhs);
  10.   if length(rhs)<len then
  11.     len:=Length(rhs);
  12.   SetLength(result, len);
  13.   for i:=0 to len-1 do
  14.     result[i]:=lhs[i]+rhs[i];
  15. end;
ArrayOperators will make this code not compile anymore. Therefore it needs to be it's own switch so it does not break existing code

Thaddy

  • Hero Member
  • *****
  • Posts: 16300
  • Censorship about opinions does not belong here.
Re: Pros/Cons of choosing one of the these two methods
« Reply #13 on: June 09, 2024, 06:20:39 pm »
You can turn arrayoperators off. {$modeswitch arrayoperators-}
And then use concat() instead. I hate that feature just for an addition.
It is useless. And arrayoperators is a misnominer: it should be arrayoperator, there is just 1 (one).
« Last Edit: June 09, 2024, 06:22:17 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5791
  • Compiler Developer
Re: Pros/Cons of choosing one of the these two methods
« Reply #14 on: June 10, 2024, 09:17:51 pm »
You can turn arrayoperators off. {$modeswitch arrayoperators-}
And then use concat() instead. I hate that feature just for an addition.
It is useless.

Complain to the Delphi developers then.

And arrayoperators is a misnominer: it should be arrayoperator, there is just 1 (one).

I had picked the plural for future proofing in case we should ever decide to overload other operators for arrays.

 

TinyPortal © 2005-2018