[Lazarus] 'with' is evil, isn't it?

cobines cobines at gmail.com
Tue Aug 2 20:16:12 CEST 2011


2011/8/2 Jürgen Hestermann <juergen.hestermann at gmx.de>:
> And it also saves the program from
> calculating array indices and other references in these expressions multiple
> times so it speeds up the code too.

I didn't know that, but indeed it does.

> I.e., if you have an expression like
> this:
>
> -----------------------------------------------------------------------
> AusdehnungInMeter :=
> (Qarray[QuaderNr]^.Teilung[R].Feldgrenzen[length(Qarray[QuaderNr]^.Teilung[R].Einteilungen)+1]
> -
>
>  Qarray[QuaderNr]^.Teilung[R].Feldgrenzen[1]) *
>
>  EinheitAuswahl[Uebergabe^.Einheit.Koord[R]].Faktor;
> -----------------------------------------------------------------------
>
> it is much easier to read (and calculate) in a with statement:
>
> -----------------------------------------------------------------------
> with Uebergabe^, Qarray[QuaderNr]^.Teilung[R] do
>  AusdehnungInMeter := (Feldgrenzen[length(Einteilungen)+1] -
>                                      Feldgrenzen[1]) *
>
>  EinheitAuswahl[Einheit.Koord[R]].Faktor;
> -----------------------------------------------------------------------
>

In case Qarray[QuaderNr]^.Teilung[R] is an object I usually do:

tmp := Qarray[QuaderNr]^.Teilung[R];

AusdehnungInMeter :=  (tmp.Feldgrenzen[length(tmp.Einteilungen)+1] -
tmp.Feldgrenzen[1]) *
EinheitAuswahl[Uebergabe^.Einheit.Koord[R]].Faktor;

It results in a similar code than using "with", because from what I
see "with" creates an implicit local var or uses a register to store
the address.

For records one could use tmp as a pointer to the record, and then
refer to tmp^.

--
cobines




More information about the Lazarus mailing list