VBAndCs / sVB-Small-Visual-Basic

Small Visual Basic (sVB) is an educational programming language, created by Eng. Mohammad Hamdy as an evolved version of Microsoft Small Basic (SB). It is meant to be easier and more powerful at the same time, to introduce programming basics to kids and beginners of any age, provided that they can use the English keyboard on the Windows OS.
Other
222 stars 16 forks source link

The different handling of variables in Small Basic and Small Visual Basic ... #23

Closed boa2145 closed 1 year ago

boa2145 commented 1 year ago

The scope of variables is different in SB and sVB. With SB, all variables are global, regardless of whether they were initialized. Variables are managed dynamically by SB and sVB and do not have to be declared. Side note: I prefer explicit declaration of variables such as in QuickBasic. But with sVB, if I understand correctly, the variables that were used for the first time in the main program are also valid in all procedures. However, variables that were initialized in a procedure or function in sVB are only known in these subprograms, so they are only valid locally there. At this point, programs created with sVB become incompatible with SB.

I think the undifferentiated handling of variables is not well solved in SB. This is child's play for me, since how and where you handle variables in a program is of fundamental importance. So, I prefer the following handling in sVB:

Your recommendation, Mohammad: Don't use global variables unless there is no other solution. Instead, pass values to subroutines and functions through parameters, and receive values from functions through their return values.

At this point, the question arises for me that I then have to decide whether to continue programming with SB or sVB? This also affects, for example, the possibilities of procedural programming with subroutines and functions, which is solved much better with sVB than with SB.

Finally, I would like to know how the keyword "global" is used in sVB? How can I change the scope of a local variable inside or outside a subroutine using "global"?

What is your opinion on these fundamental programming topics?

Regards ... Gregor

VBAndCs commented 1 year ago

how the keyword "global" is used in sVB? How can I change the scope of a local variable inside or outside a subroutine using "global"?

The Global keyword is used only to refer to variables, subroutines and functions defined in the Global.sb file, so that you can access them from any form in the project. The Global file also allows you to reuse the project as an sVB lib as explained in the book. This is how the Graphics and UnitTest libraries used in sVB are created (see their source codes in the samples path). Using only global vars in SB was a bad design, and this is so clear in all the games written using subroutines and global variables, where 5 variables in average are needed for each subroutine to act as parameters and a return value for the simulated function, which makes one defines too much global variables with too many comments to remember to which method they belong! In my opinion, there is no purpose in teaching kids bad programming habits. I see no need to declare variables, since this is already a valid VB6 and VB .NET dynamic mode. SB doesn't have a type system. It only uses the Primitive structure to carry all data types. Changing that in the sVB compiler will make things complicated, and the ByVal and ByRef topic can't be avoided then. I appended a backdoor for byref operations by allowing calling the Array.AddNextItem and Array.SetItemAt methods for fast building and modifying the array, which is not advised for kids. Se the Array Performance paragraph in page 102 in the book. But sVB has some virtual typing system, to make it easy to write win forms apps. You can use Naming Conventions (see page 85) to specify the initial variable type, or in many cases, you can just let sVB infer that type form the assigned expression, or the function return value.

boa2145 commented 1 year ago

Hi Mohammad, Thanks for your reply. I am going to answer later.

Have a look at my new exercise "The Flag Of China" by using the turtle object and to fill the drawings with color. I posted it on the issue "sVB and turtle programming with colored shapes". How is it possible to post formatted source code on GitHub?

Regards ... Gregor

VBAndCs commented 1 year ago

@boa2145 I am got notifications about replies, and I already replied there, and explained how to format the code even before seeing your question here :D Thanks.