[Gambas-user] variable declaration everywhere ???

Christof Thalhofer chrisml at deganius.de
Fri Dec 14 09:56:49 CET 2018


Am 14.12.18 um 00:51 schrieb Benoît Minisini:

> Or should I remove the possibility of declaring variables anywhere before?

I would remove that.

Option explicit and the decision to allow the declaration of variables
only at top in my eyes are the main advantages of Gambas vs VB(A)
concerning cleaner code.

Basic seems to be an easy language for beginners, but – as always –
allows a lot of very ugly code constructions.

Because at the end code is always procedural, it can be distributed
either over one long method or several different methods. The latter is
usually much better, because small functions can be understood and
debugged more easily. Also functions can be held stateless and so much
more secure.

Long method bodies for me are a clear sign for bad code.

As I started programming long ago I did not understand the advantages of
declaration of variables, and later – as I understood – I declared a lot
of variables within the body of my methods. As I had to refactor that
code mess later I found out that I packed a lot of functionality in
loooong methods which were quite hard to understand.

As I switched over to Gambas all these things did not work any more as
the Gambas compiler complained about declarations within the body of
these long methods. I had to refactor (bring all the variables to top)
and found out that I did a lot of senseless things with variables:

Often I packed content of one variable in a new one just because the
name of the first variable did not fit into the current environment
(scope) inside a long method. I did this, because I could declare the
new variable within the body.

I also tended to pack a lot of different functionality in one long
method and I also had many headlines in my comments, each of which
referred to a new section of the body that contained new functionality
and new variables.

Long Methods with a lot of functionality inside are bad. Really bad! You
will find this out if you want to understand what you have written two
weeks later. Or a year later.

If variable declaration inside the body would be needed it is much
better to think about it a while and then export the functionality to
another method.

If the declaration is at the top, at first it may be a bit inconvenient,
but it will always remind you that it is much better to outsource
functionality. And you will, simply because it will be more convenient.

So this decision of Gambas leads to better code!

I've put a lot of work into a lot of code and the older I get the
smaller and clearer my methods become. I'm against making it easier for
programmers to create huge functions full of spaghetti.


Alles Gute

Christof Thalhofer

-- 
Dies ist keine Signatur

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://lists.gambas-basic.org/pipermail/user/attachments/20181214/035de345/attachment.sig>


More information about the User mailing list