Forum > FPC development

[CLOSED] ForceDirectories anomaly

(1/2) > >>

lagprogramming:
rtl/objpas/sysutils/disk.inc has function ForceDirectories(Const Dir: PathStr): Boolean;
https://gitlab.com/freepascal.org/fpc/source/-/blob/main/rtl/objpas/sysutils/disk.inc
The function might return true, false or raise an exception. Raising an exception is counterintuitive.

The stable documentation writes

--- Quote ---76.15.128
ForceDirectories
Synopsis: Create a chain of directories
Declaration: function ForceDirectories(const Dir: RawByteString) : Boolean
function ForceDirectories(const Dir: UnicodeString) : Boolean
Visibility: default
Description: ForceDirectories tries to create any missing directories in Dir till the whole path in Dir
exists. It returns True if Dir already existed or was created successfully. If it failed to create any
of the parts, False is returned.
--- End quote ---

The development documentation has been modified for this function but it avoids this anomaly.
This mix of exceptions and boolean results is prone to easily insert bugs in code.

PascalDragon:
This exception is only raised if the Dir parameter is empty which is essentially an invalid argument for the function, after all it makes no sense to ensure that no path exists. Delphi behaves the same here, so nothing will be changed there.

AlexTP:
No report in the bugtracker is needed, per PascalDragon.

lagprogramming:

--- Quote from: PascalDragon on May 10, 2023, 10:45:46 pm ---This exception is only raised if the Dir parameter is empty which is essentially an invalid argument for the function, after all it makes no sense to ensure that no path exists. Delphi behaves the same here, so nothing will be changed there.

--- End quote ---
mkdir(''); doesn't raise an exception.
directoryexists('',true); returns false, it doesn't raise an exception.
I have doubts ordinary programmers enclose forcedirectories in try...except/finally blocks. It's counterintuitive.
Anyway, because Delphi has this behavior, I have doubts Fpc and Lazarus would change the existing code.

PascalDragon:

--- Quote from: lagprogramming on May 19, 2023, 04:48:52 pm ---
--- Quote from: PascalDragon on May 10, 2023, 10:45:46 pm ---This exception is only raised if the Dir parameter is empty which is essentially an invalid argument for the function, after all it makes no sense to ensure that no path exists. Delphi behaves the same here, so nothing will be changed there.

--- End quote ---
mkdir(''); doesn't raise an exception.
--- End quote ---

ForceDirectories is not an equivalent to MkDir as that requires the parent directories to already exist.


--- Quote from: lagprogramming on May 19, 2023, 04:48:52 pm ---I have doubts ordinary programmers enclose forcedirectories in try...except/finally blocks. It's counterintuitive.
--- End quote ---

Programmers will also very likely not pass empty strings to ForceDirectories which is the only case where that function raises an exception.


--- Quote from: lagprogramming on May 19, 2023, 04:48:52 pm ---Anyway, because Delphi has this behavior, I have doubts Fpc and Lazarus would change the existing code.

--- End quote ---

Correct. This will not change.

Navigation

[0] Message Index

[#] Next page

Go to full version