Recent

Author Topic: FindAllFiles and basic weirdness  (Read 581 times)

backprop

  • Jr. Member
  • **
  • Posts: 91
FindAllFiles and basic weirdness
« on: August 02, 2024, 04:41:24 am »
https://lazarus-ccr.sourceforge.io/docs/lazutils/fileutil/findallfiles.html

Code: Pascal  [Select][+][-]
  1. uses
  2.   FileUtil;
  3. var
  4.   PascalFiles: TStringList;
  5. begin
  6.   PascalFiles := TStringList.Create;
  7.   try
  8.     FindAllFiles(PascalFiles, LazarusDirectory, '*.pas;*.pp;*.p;*.inc', true); //find e.g. all pascal sourcefiles
  9.     ShowMessage(Format('Found %d Pascal source files', [PascalFiles.Count]));
  10.   finally
  11.     PascalFiles.Free;
  12.   end;
  13. end;
  14. // or
  15. begin
  16.   //No need to create the stringlist; the function does that for you
  17.   PascalFiles := FindAllFiles(LazarusDirectory, '*.pas;*.pp;*.p;*.inc', true); //find e.g. all pascal sourcefiles
  18.   try
  19.     ShowMessage(Format('Found %d Pascal source files', [PascalFiles.Count]));
  20.   finally
  21.     PascalFiles.Free;
  22.   end;
  23. end;

Looking this example, honestly I can't understand alternative code. Why this possibility is created anyway? I would understand that in Java, but Pascal have strict policy of creation and freeing objects.

Who created this function anyway?

TRon

  • Hero Member
  • *****
  • Posts: 3263
Re: FindAllFiles and basic weirdness
« Reply #1 on: August 02, 2024, 05:03:47 am »
Looking this example, honestly I can't understand alternative code.
Pascal isn't that difficult to understand

Quote
Why this possibility is created anyway?
42

Quote
I would understand that in Java, but Pascal have strict policy of creation and freeing objects.
16 years is a little too late.

Quote
Who created this function anyway?
Nobody likes watching the history channel.
This tagline is powered by AI

backprop

  • Jr. Member
  • **
  • Posts: 91
Re: FindAllFiles and basic weirdness
« Reply #2 on: August 02, 2024, 06:48:42 am »
TRon, you trolling again.

Pascal is not Java and promoting this as "proper way" to code in Pascal is at least disaster what you do with Pascal. Exactly the reason to know who created this function and created this example code. Probably doesn't deserving to be developer here.

But the the most weird thing is that others have nothing against this way of coding... Pascal is just down to the drain now...
« Last Edit: August 02, 2024, 06:53:49 am by backprop »

Aruna

  • Sr. Member
  • ****
  • Posts: 457
Re: FindAllFiles and basic weirdness
« Reply #3 on: August 02, 2024, 06:53:42 am »
Looking this example, honestly I can't understand alternative code.
What specifically don't you understand?

Why this possibility is created anyway?
The first method manually creates a TStringList and then uses FindAllFiles to populate it.The second method assumes that FindAllFiles handles the creation of the TStringList and returns it directly. Both methods achieve the same result: counting and displaying the number of Pascal source files in the specified directory.

The main difference lies in whether the TStringList is explicitly created or handled internally by the FindAllFiles function.

Who created this function anyway?
Please have a look at the attached screenshot. This function was created by some very highly experienced people who knew exactly what they were doing  :)
Debian GNU/Linux 11 (bullseye)
https://pascal.chat/

backprop

  • Jr. Member
  • **
  • Posts: 91
Re: FindAllFiles and basic weirdness
« Reply #4 on: August 02, 2024, 06:59:03 am »
The main difference lies in whether the TStringList is explicitly created or handled internally by the FindAllFiles function.

That is not the way of creating object in Pascal and sign of lack of understanding basic concepts.

Quote
Please have a look at the attached screenshot. This function was created by some very highly experienced people who knew exactly what they were doing  :)

Bart and Juha? Now I understand all... ;)


JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4519
  • I like bugs.
Re: FindAllFiles and basic weirdness
« Reply #5 on: August 04, 2024, 07:20:03 am »
I guess Martin locked this issue. Only admins and moderators can reply. My profile says "Moderator" but I cannot lock or unlock threads. I see a "Split topic" button but it does not help. I don't know what this "Moderator" status is good for.
Martin, can you please unlock. It is OK to criticize a function here.
[Edit] OK, Martin didn't lock it. I guess technical discussion about FindAllFiles, if needed, must go to a new thread.

That is not the way of creating object in Pascal and sign of lack of understanding basic concepts.
AFAIK there is no strict rule that an object must be created and freed in the same function. You just must pay attention and remember to free it.
The only strict rule is to free it somewhere.
The same idea is used in many Codetools functions and elsewhere. Sometimes extra var parameters pass optional new lists or other objects. You can Free such an object even if the function didn't create it (Free a Nil reference is OK).

That being said, such code looks out of balance. True! I also prefer to first create and then pass an object.
Fortunately FindAllFiles has an overload version for that. Please use it.

Quote
Bart and Juha? Now I understand all... ;)
:)
FindAllFiles was originally meant to be a simple utility function. Then all kinds of corner cases turned out. Extra parameters had to be added. Now there is a struggle with symlinks AFAIK.
The function does not look simple and nice any more.
Solution: Instead of function FindAllFiles use the underlying class TFileSearcher. It provides methods and events to analyze the found files and directories. A TStringList is not needed.
TFileSearcher was originally hidden under implementation section. It was moved to interface section due to user requests.
« Last Edit: August 04, 2024, 08:25:17 pm by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

 

TinyPortal © 2005-2018