Unfortunately I spent a few days at home last week, mainly in bed; I had a mild case of flu. Fortunately, I didn't run a high temperature, but I was very weak and my head felt like cotton wool for most of the time. But that's all over now....
After my head returned to normal service but before my body regained its strength, I spent some time refactoring the code in the OP's management suite program. As this suite has been a work in progress for more than two years, it's not surprising that certain functionality has been repeated which can now be safely factored out. It turns out that a function to calculate how many days there are in a month appeared in three different units (and at least once I ignored leap years); this was easy to fix.
About 14 months ago, I wrote about sorting data in grids and included some very simple code. When I was looking at the management suite the other day, I realised that several units contained this kind of code,
with qTest do
begin
open;
addindex ('idx0', 'num', [], '', '', 0);
addindex ('idx1', 'num', [ixDescending], '', '', 0);
addindex ('idx2', 'extra', [], '', '', 0);
addindex ('idx3', 'extra', [ixDescending], '', '', 0);
strings:= tstringlist.create;
getindexnames (strings);
strings.free;
close;
end;
After some cogitation, I realised that I could make a double saving: I could write a routine which automatically created two indices (one ascending and one descending) per field in the table, and of course I could use this routine in every module. If I remember correctly, about eight modules used this generic code; of course, now I can add it to more modules without occurring any development cost. As a side effect, the size of the executable has shrunk.
As I point out to the OP, centralising code (or refactoring) means (amongst other things) that there is only a single point of failure for each function.
Here is the new code:
Procedure BuildIndices (cds: TClientDataSet);
var
i, j: integer;
alist: tstrings;
begin
with cds do
begin
open;
for i:= 0 to FieldCount - 1 do
begin
j:= i * 2;
addindex ('idx' + inttostr (j), fieldlist.strings[i], [], '', '', 0);
addindex ('idx' + inttostr (j+1), fieldlist.strings[i], [ixDescending], '', '', 0);
end;
alist:= tstringlist.create;
getindexnames (alist);
alist.free;
close;
end;
end;
No comments:
Post a Comment