Friday, August 06, 2004
Another cross against with
An ADUG member (name suppressed to protect the innocent) unleashed this code on the mailing list today. Add it to the mounting stack of evidence as to why using with is not a good idea.
n := -1; { counter which identifies which group index we are up to }
with TProjectAlias(gAliases.Items[PrjIndex]) do begin
with TNetList(PNetIndex.Items[NetIndex]) do begin
with TAppIndex(ApplIndex.Items[AppIndex]) do begin
for i := 0 to SortedLevels.Count - 1 do begin
with TLevelEntry(SortedLevels.Items[i]) do begin
if GroupAdd = StrPas(inGrpAdd) then begin
n := n + 1; { increment count }
if n = inIndex then begin { check if it is the right one }
tmpDesc := TLevelEntry(SortedLevels.Items[i]).Desc; { copy description }
StrPCopy(outDesc, PadString(tmpDesc, DESC_LENGTH));
break;
end; { if }
end; { if }
end; { with }
end; { for }
end; { with }
end; { with }
end; { with }
Granted, like most starting out Delphi developers I initially embraced the concept (wow, think of all the typing I'm saving with this great feature), but gave away the notion a few years ago due to it being harder to maintain, and the ambiguity issues it can create. I used to make an exception when creating class functions to show modal forms (as shown below), but I've even stopped doing that now.
with TSomeForm.Create(nil) do
try
Result := ShowModal;
finally
Free;
end;
Comments:
Post a Comment