[fpc-pascal] Can variables be declared within a block?

Frank Church vfclists at gmail.com
Mon Oct 18 15:55:24 CEST 2010


On 18 October 2010 14:42, Florian Klaempfl <florian at freepascal.org> wrote:

> Am 18.10.2010 14:58, schrieb Frank Church:
> >
> >
> > On 18 October 2010 07:49, Florian Klaempfl <florian at freepascal.org
> > <mailto:florian at freepascal.org>> wrote:
> >
> >     Am 18.10.2010 08:17, schrieb Zaher Dirkey:
> >     >
> >     >
> >     > On Thu, Oct 14, 2010 at 11:21 AM, Michael Van Canneyt
> >     > <michael at freepascal.org <mailto:michael at freepascal.org>
> >     <mailto:michael at freepascal.org <mailto:michael at freepascal.org>>>
> wrote:
> >     >
> >     >
> >     >
> >     >     On Thu, 14 Oct 2010, Frank Church wrote:
> >     >
> >     >         Can variables be declared within a begin end block?
> >     >
> >     >
> >     >     No.
> >     >
> >
> >
> > That is a rather emphatic no :).
> >  Is there some performance or other related reason why it is a bad idea?
>
> It's just a matter of readability, e.g. having a variable declared in
> two blocks with the same name can cause you a lot of headache:
>
> procedure p;
>
> begin
>  ...
>  begin
>    var i : integer;
>    ...
>    ...
>    ...
>    i:=12341234;
>    ...
>    ...
>    ...
>    ...
>  end;
>  ...
>  ...
>  ...
>  begin
>    var i : integer;
>    ...
>    writeln(i); <--- this i is not the same as above so i does not
> contain 12341234 but it contains probably some garbage.
>  end;
>


My need is not for such kind or variables - it is for something like this.

say I have some thing like:
 StringList.NameOfIndex(TableName.FieldByName('xxxx').AsString) >
AnotherEqualLongExpression + SomeOtherEvenLongerExpression.

I just want to create shorter variables like
si :=  StringList.NameOfIndex(TableName.FieldByName('xxxx').AsString);
ae := xxx;
so:= yyy

to express complicated logic with them.

When I put them all at the top of the procedure, they are extra noise, which
is not necessary for the understanding the purpose at the top level of the
procedure.

I understand the need to avoid stuff that could create unintended errors and
have experienced such issues before. I will be careful not to use the more
than enough rope I have been given to hang myself. I promise :)


> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>



-- 
Frank Church

=======================
http://devblog.brahmancreations.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20101018/210ee8b4/attachment.html>


More information about the fpc-pascal mailing list