[fpc-pascal] SetLength procedure

Jürgen Hestermann juergen.hestermann at gmx.de
Sun Jul 27 16:18:43 CEST 2014


Am 2014-07-27 12:51, schrieb Steve Gatenby:
> I would find code in the form of
>   ArrayLen := IncLength(ArrayName, 1);
> to be much more readable then
>    SetLength(ArrayName, Length(ArrayName)+1);
>    ArrayLen:= High(ArrayName);
>

Why? It is not clear which index ArrayLen will receive from IncLength.
You need to look up (or remember) what exactly this function does
while in the two-line version it is all clear and unambiguous.
Especially, if you not only add 1 element but more as in

   ArrayLen := IncLength(ArrayName,2);

    SetLength(ArrayName, Length(ArrayName)+2);
    ArrayLen:= High(ArrayName);

In the first version, which value will ArrayLen be set to?
High(ArrayName) or High(ArrayName)-1?
This is very clear in the second version.

IMO it is not worth investing time into writing a function
that obscures the code. If you look at it months later
you may no longer know what IncLength does exactly,
not to talk about if someone else has/wants to read
your code.



More information about the fpc-pascal mailing list