PDA

View Full Version : Suggestion: new for commands



kryton9
14-05-2017, 21:29
Easier to explain via an example. In C++ they do this:
for ( int i = 0; i < limit; i++ )

What is neat is that i is local for that loop. Right now we need to declare i outside of the loop as they do in C and it is not local

The idea, 6 new commands:
Fori, Forj, Fork, fFori , fForj, fFork each used with an optional stepType (boolean false or true) as following:

optional stepType not used, steps in normal manner or as "false" was passed in.
Fori 1, 10, 2

In TB:
long i
for i = 1 to 10 step 2


fFori 0.1, 0.9, 5, true

in TB:
single i
for i = 0.1 to 0.9 step (0.9 - 0.1)/5

Petr Schreiber
15-05-2017, 09:06
Hi Kent,

the idea with number of iterations instead of step for some cases is neat, the local variable as well!

But I have to add few insights regarding the syntax.

Functions with multiple parameters are confusing, especially those booleans. It is mentioned multiple times in Pragmatic Programmer and Code Complete - I would also vote to avoid such a designs, although I made similar mistakes in the past. Just have a look at TBGL_SetupLightSource... yikes!

Also - while I am the last person to insist on inheriting legacy BASIC syntax, I like to keep "basic" as the design rule to lead us to the future.

When you look at these:


for i = 2 to 8 step 4

print "Ciao" At 10, 5


... you are immediately oriented.

When you see:

fFori 0.1, 0.9, 5, true

... you need to think twice.

So for the local variable, I would go this route:


for single f = 1 to 10


...and for the implied number of iterations:


for f = 1 to 10 in 5 steps

(for example)

-

Do you remember? ThinBASIC had this implemented, not sure where it got lost (Eros?):


for i as long = 1 to 10
next

for i = 2 to 8
next

So... you see, the i gets declared as part of first cycle and can be recycled in second.

Advantages?

No variable deallocation at end of each cycle brings more performance.


Petr

kryton9
15-05-2017, 19:00
You make good points Petr for BASIC like syntax.

I don't remember the older thinBasic for's that you wrote about, no surprise there with my bad memory :D

I am in favor of your suggestions if possible:



for i = 2 to 8 step 4

for single f = 1 to 10

for f = 1 to 10 in 5 steps

ErosOlmi
16-05-2017, 11:39
Ciao Petr, what do you mean with "lost" ?


Do you remember? ThinBASIC had this implemented, not sure where it got lost (Eros?):

http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?fornext.htm

Loop variable supporting FOR/NEXT can be declared on the fly but it is not removed afterwards due to time it takes to de-allocate it.

Personally I'm with Petr when he says we need to remain with BASIC syntax as much as possible even if I like to add some "contamination" if they are useful to the programmer and bring some execution advantages/speed.
I hate short variable declaration, I like long names for variables :)
I like code to be able to be read after many years without double concentration to follow variable meaning.

Petr Schreiber
16-05-2017, 18:43
Hi Eros,

I am sorry for confusion - I checked and it works in the latest thinBASIC indeed.
I remember I tried to use it at work and it caused runtime error, but I probably run into some issue not present now.


Petr

kryton9
16-05-2017, 19:00
I get an error here guys.
9704
9705

Michael Hartlef
16-05-2017, 19:13
You forgot to declare the variable.

This works


Uses "console"

For i As Long = 1 To 10


PrintL Str$(i)

Next

kryton9
16-05-2017, 19:15
Thanks Mike! User error strikes again :D
I got too used to brevity the last few years.