[Gambas-user] variable declaration everywhere ???

Gianluigi bagonergi at gmail.com
Fri Dec 14 12:38:33 CET 2018


Il giorno ven 14 dic 2018 alle ore 00:52 Benoît Minisini <g4mba5 at gmail.com>
ha scritto:

> Le 14/12/2018 à 00:02, Christof Thalhofer a écrit :
> > Am 12.12.18 um 00:16 schrieb Benoît Minisini:
> >
> >> Because as soon as variables can be declared anywhere, people will start
> >> to use that for writing ugly code. :-)
> >
> > Exactly. I know it because I had a hard work with my own code which was
> > more than ten years old (VBA).
> >
> > If we have to declare variables only at the beginning we tend to write
> > shorter methods which is much easier to understand. Especially if the
> > code is a couple of weeks/months/years old.
> >
> > Since I program in Gambas my methods are smaller. Every time I see that
> > I had to declare many variables at the beginning I feel a kind of shame.
> > Because I know that the number of variables correlates with the
> > incomprehensibility of my own code.
> >
> > Alles Gute
> >
> > Christof Thalhofer
> >
>
> Since last commit:
>
> - Local variables can be declared anywhere in the function, but they
> always have a function scope.
>
> - Loop variables can be declared directly in the loop opening syntax.
> And they too have a function scope.
>
> - Loop variables can be redeclared as soon as it keeps its type.
>
> Now maybe I could start thinking releasing Gambas 3.12?
>
> Or should I remove the possibility of declaring variables anywhere before?
>
> --
> Benoît Minisini
>

Hi Benoit,

Public Sub Main()

   For n As Byte = 0 To 9
     Dim v As Byte
     Print v
     Inc v
     Print v
     Print n
   Next

   Print "==========================="

   Dim sStr As String[] = Split("Any phrase of this book", " ")
   For Each s As String In sStr
     Print s
   Next
   Print "---------------------------"
   Print n
   Print v
   Print s
   If sStr Then Print "sStr exist"

End

Would not it be more logical to expect that only sStr existed after the For
blocks?

Regards
Gianluigi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.gambas-basic.org/pipermail/user/attachments/20181214/32bdc1f8/attachment.html>


More information about the User mailing list