This is probably very subjective, but I'm just trying to get another view/thoughts on this example:
when adding simple records (not a lot of lines of code), I used to always use 1st example, DataSave1: use boolean flag and add new if record not exsits, but lately I try using Exit, like in 2nd example, DataSave2: - update record and Exit, flow never gets to adding new record:
procedure SaveData1(aID: Integer; const aName: string);
var
i: Integer;
vExists: boolean;
vNewData: TDataRec;
begin
vExists := false;
for i := Low(Data) to High(Data) do
if Data[i].DataID = aID then
begin
Data[i].DataName := aName;
vExists := True;
Break;
end;
// Save new Data rec, if ID not found
if Not vExists then
begin
vNewData := Default(TDataRec);
vNewData.DataID := aID;
vNewData.DataName := aName;
Data := Data + [vNewData];
end;
end;
or without boolean flag:
procedure SaveData2(aID: Integer; const aName: string);
var
i: Integer;
vNewData: TDataRec;
begin
for i := Low(Data) to High(Data) do
if Data[i].DataID = aID then
begin
Data[i].DataName := aName;
Exit;
end;
// Save new Data rec, if ID not found
vNewData := Default(TDataRec);
vNewData.DataID := aID;
vNewData.DataName := aName;
Data := Data + [vNewData];
end;
I like 2nd example because I don't need vExists flag. Am I setting myself up to some unknown issue that I can't see?
These are examples for small Data arrays.
type
TDataRec = record
DataID: integer;
DataName: string;
end;
var
Data: TArray<TDataRec>;