Dim/Data block syntax

For other topics related to the FreeBASIC project or its community.
Tourist Trap
Posts: 2817
Joined: Jun 02, 2015 16:24

Dim/Data block syntax

Postby Tourist Trap » Jun 09, 2015 14:16

Hi,

As far as I've been able to see, there are 2 ways to declare a variable ( leave aside Redim , or additionnal keywords like Shared, or variable/type versus type/variable order ) . Those are :


    Dim As SomeType someVariable , or

    As SomeType someVariable , only for fields sitting inside UserDefined Type


Let me know if I missed something here. Ok, suppose I'm right. It can lead to such a series :
Dim As Double testProbability = 0
Dim As Long nextPtrTableUpperBound = 1
Dim As Any Ptr nextPtrTABLE(1 To nextPtrTableUpperBound)
Dim As Long nextPtrOccurenceCountTABLE(1 To nextPtrTableUpperBound)
Dim As Long totalOccurenceCountFromTABLE = 0
Dim As Long totalAmountOfCoupleGenerated = totalizer \ 2


I suggest that one could make it a block like below :

Code: Select all

Dim As                             ''Dim Block start
      Double testProbability = 0
      Long nextPtrTableUpperBound = 1
      Any Ptr nextPtrTABLE(1 To nextPtrTableUpperBound)
      Long nextPtrOccurenceCountTABLE(1 To nextPtrTableUpperBound)
      Long totalOccurenceCountFromTABLE = 0
      Long totalAmountOfCoupleGenerated = totalizer \ 2
End Dim                            ''Dim Block end


I can list this series of advantages :

Advantages :

1/ In general , less typing and readability increased .

2/ In particular :
_ _ Possibility to comment the start of the block , example : 'global variables
_ _ Possibility to Copy/Paste easier if we wish to put this in a UDT (As Block?), or even in functions arguments
_ _ With an IDE like FBEdit, arises further possibility to Fold/Unfold the block
(and in others IDE folded block content is displayed in a yellow tip box when flying over, wich is very convenient)
_ _ Greater compatibility with C syntax with no sacrifying basic specificity

In the other hand I don't find any disavantage.

Disadvantages :

1/ It would break Scope rule of so called variable Block Visibility. . .
However, this is not inherent to blocks in an absolute maneer. A block could also be disconnected to any Scope behaviour if proved useful.

2/ It will ask for filtering nesting Block ability wich has no sense in this context
It's not a big problem to loose 1ms of syntactic check up for that, when you avoid so many compilation warnings and errors due to difficulty to write clean code.

However things would have to be put into the balance.

About DATA, I've been conviced that a Data Block/End Block would also be salvation when viewing this nice example code : viewtopic.php?f=3&p=208361#p208358

Thanx for giving attention.
(By advance, sorry if this issue had already been discussed. From my side view, it remains a recurrent concern.)
Last edited by Tourist Trap on Jun 10, 2015 1:10, edited 3 times in total.
Drago
Posts: 116
Joined: Aug 10, 2005 13:15

Re: Dim block syntax

Postby Drago » Jun 09, 2015 15:00

Code: Select all

      Long nextPtrOccurenceCountTABLE(1 To nextPtrTableUpperBound)
      Long totalOccurenceCountFromTABLE = 0


snip snap.... unable to cut and paste without retyping Dim
Tourist Trap
Posts: 2817
Joined: Jun 02, 2015 16:24

Re: Dim block syntax

Postby Tourist Trap » Jun 09, 2015 15:26

Hello Drago (MaLefoy?) ;-)

For me Cut/Paste is a big affair for types and variables names, lesser for "Dim As" statement itself. This is the first remark.

The other remark follows immediatly from my experience. When you want to perform Cut for example or Copy, you have to grasp the text first, the same is true for drag/drop. If this text comes isolated enough you wont have to be accurate with the mouse, just select the whole line and you get what you need with success. If this is inside a bigger instruction, you will fail unless you start and stop selecting very carefully (think of a jeweller) and you will much often waste so much time that many people would prefere retyping. I mean that the increase of performance of cut/paste is also linked to the readability.

Last example. If you want to transfer the significant part of a type/variable from a point to another, in order to make a later use of it. For example from main loop to a function block, you certainly dont need to take the dim statement with you . In another hand, if you need to keep the Dim statement and are lazy, just copy the whole block and delete the variables that you finally dont need.

An empty Dim Block could even compile why not? So that you could even make an empty code template, with such a placeholder.

Code: Select all

'
'Do Loop Template
''Shows a folded Dim block ready to incorporate initialization variables (if needed) for a Do loop block

[+] Dim As...                    ''Loop Init variables
[+] Do ...                         ''Do Block

''The 2 blocks come folded, say as a matter of template.


This said. You are not wrong absolutely of course, but in my opinion what you showed in your example is not an issue. A bigger issue is to perform cut/paste between C code to FB, if not the biggest.
RockTheSchock
Posts: 226
Joined: Mar 12, 2006 16:25

Re: Dim block syntax

Postby RockTheSchock » Jun 09, 2015 15:48

Tourist Trap wrote:Let me know if I missed something here.

Well, you shouldn't use many global vars at all. It's cleaner to encapsulate them in a Type and define one global var/object or maybe a local var/object which you can initialise with a constrcutor and pass as parameter to your mainprocessingfunction. Such code tend to be more reusable.

Types should go to .bi headers while Dims go to the .bas modules. The header file should be commented enough to use the code without knowing the implementation.
Last edited by RockTheSchock on Jun 09, 2015 15:52, edited 1 time in total.
Tourist Trap
Posts: 2817
Joined: Jun 02, 2015 16:24

Re: Dim block syntax

Postby Tourist Trap » Jun 09, 2015 16:02

RockTheSchock wrote:It's cleaner to encapsulate them in a Type and define one global var/object or maybe a local var/object which you can initialise with a constrcutor and pass as parameter to your mainprocessingfunction. Such code tend to be more reusable.

Nice tip. Thanks.
RockTheSchock wrote:Types should go to .bi headers while Dims go to the .bas modules. The header file should be commented enough to use the code without knowing the implementation.

Here this is much more complicated. Whatever I tend to approve since it leads to a structured source code.

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest