> > x:= DataModule.Table.FieldByName('X').AsInteger;
> > if (x = 2) or (x = 4) or (x = 5) then begin
> >
> > is obviously preferable to writing
> >
> > if (DataModule.Table.FieldByName('X').AsInteger = 2) or
> > (DataModule.Table.FieldByName('X').AsInteger = 4) or
> > (DataModule.Table.FieldByName('X').AsInteger = 5)
> 
> One good thing about previous sample is speed will be faster.

I'm of the school of caveats.

1. If it ain't speed critical...

if DataModule.Table.FieldByName('X').AsInteger in [2,4,5] then begin
end;

is fine...

As another speed improvement... Always specify your field orders.  Declare
these as an enumeration and use an Ord(EnumValue) indexing into Fields
rather than the string search into Fields using FieldByName...

if DataModule.Table.Fields[Ord(fldX)] .AsInteger in [2,4,5] then begin
end;

Also, in all cases where 2,4 & 5  have higher level meanings... Declare an
Enumeration or set of constants and document the values via comments.

var
  x :Valuetypes;
begin
  x := ValueType(DataModule.Table.Fields[Ord(fldX)] .AsInteger);
  if x in [vtAValue,vtAnotherValue,vtYetAnotherValue] then begin
  end;
end;

the aim would be to add readability, performance and compiler enforced
fault protection.  More ideal is to wrap all DB access into an object layer...

--
Talden@home


---------------------------------------------------------------------------
  New Zealand Delphi Users group - Offtopic List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to