[fpc-pascal] SetLength procedure

Andreas Berger Andreas at TheBergerClan.org
Sun Jul 27 19:28:08 CEST 2014


I very seldom comment on this, or any, forum unless I have a good answer 
and no one else seem to have one. This case is different. I have noticed 
quit often here that instead of answering the question people try to 
reason why in the world the author of the question would even want to do 
this. Would it not be better to tell him how to do it or at the most say 
"I don't know". I know there are times when you want to prevent someone 
from making a serious mistake, but this is not the case here.
Sometimes the question is placed by someone new to programming and it is 
important to learn from your own work, mistakes and all. Often the 
question also comes from people migrating from, or knowledgeable with, 
another language where a similar functionality exits. This type of 
argument will only discourage them away from our beloved Pascal.

Nur mein Senf.

On Sun 27/07/2014 11:18, Jürgen Hestermann wrote:
>
> 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.
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>




More information about the fpc-pascal mailing list