[fpc-pascal] with statement using mulltiple objects
Jürgen Hestermann
juergen.hestermann at gmx.de
Sun Sep 14 11:58:22 CEST 2014
Am 2014-09-13 22:31, schrieb Marius:
> Mattias Gaertner wrote:
> All seasoned programmers know that and try to avoid the with statement.
Realy? I love it. Consider these two varianst of code from one of my programs:
---------------------------------------------------------
with TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^ do
begin
DirLogged := true;
DirHatFocus := false;
SubDirsExpanded := true;
NonLoggedDirsInBranch := 0;
LastSubDirIndex := -1;
ErrorString := '';
fillchar(StatistikOfFiles,sizeof(StatistikOfFiles),0);
fillchar(StatistikOfDirs ,sizeof(StatistikOfDirs) ,0);
Verzeichnis := Form1.Verzeichnisbaum.RootNode;
DirName := DirectorySeparator;
DirNameLowerCase := LowerCase(DirName);
TRightMostPeriod := 0;
DirSize := 0;
TFileAttributes := 0;
fillchar(TDateWritten,sizeof(TDateWritten),0);
TFlagSet := [];
SubDirs := nil;
Files := nil;
end;
---------------------------------------------------------
and
---------------------------------------------------------
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.DirLogged := true;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.DirHatFocus := false;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.SubDirsExpanded := true;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.NonLoggedDirsInBranch := 0;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.LastSubDirIndex := -1;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.ErrorString := '';
fillchar(TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.StatistikOfFiles,sizeof(TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.StatistikOfFiles),0);
fillchar(TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.StatistikOfDirs ,sizeof(TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.StatistikOfDirs) ,0);
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.Verzeichnis := Form1.Verzeichnisbaum.RootNode;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.DirName := DirectorySeparator;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.DirNameLowerCase := LowerCase(DirName);
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.TRightMostPeriod := 0;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.DirSize := 0;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.TFileAttributes := 0;
fillchar(TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.TDateWritten,sizeof(TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.TDateWritten),0);
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.TFlagSet := [];
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.SubDirs := nil;
TreeRoot.SubDirs[Low(TreeRoot.SubDirs)]^.Files := nil;
---------------------------------------------------------
Which one would you prefer?
The first one is *much* less error prone because it is easier to read and makes it therefore clearer than the second example.
Also, the calculation of the pointer is done multiple times in the second example which makes the code slower.
I love Pascal also because of the WITH statement.
The risks are the same as when using multiple units which define identical named identifiers.
More information about the fpc-pascal
mailing list