QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: bobtheunplayer on February 05, 2019, 10:05:45 am

Title: Local SUB/FUNC variable declarations
Post by: bobtheunplayer on February 05, 2019, 10:05:45 am
Is it better to
Code: QB64: [Select]
  1. SUB Foo
  2.     bar$ = "baz"
  3.  
Or
Code: QB64: [Select]
  1. SUB Foo
  2.     DIM bar AS STRING
  3.     bar = "baz"
  4.  
Title: Re: Local SUB/FUNC variable declarations
Post by: bplus on February 05, 2019, 10:52:26 am
I think it is a preference thing but declaring variables with DIM before using with Option Explicit can save tons of time cost by typo.

OTOH, Strings with $ are self documenting, I like $ for strings (Explicit free) but it is an extra character to type. And in usually smaller SUBs and Functions there is not an overwhelming amount of variables to track.

Declaring in parameters list the type with AS might be handy with reusable routines.

Be careful with subs that will try to pass back arguments by ref. ie if you change a value of argument in a sub that change might effect main program with that variable.
Title: Re: Local SUB/FUNC variable declarations
Post by: Pete on February 05, 2019, 03:19:30 pm
Of course, there's always my way!...

Code: QB64: [Select]
  1. SUB Foo
  2.         bar = "baz"
  3.  
Title: Re: Local SUB/FUNC variable declarations
Post by: TempodiBasic on February 05, 2019, 05:01:17 pm
Hey boy, you forget that my way is better !
:-)
Code: QB64: [Select]
  1.  
  2. SUB Foo
  3.   SHARED Bar
  4.   Bar ="baz"
  5.  

It's better to share only what it is useful and/or necessary!
:-)
Title: Re: Local SUB/FUNC variable declarations
Post by: bplus on February 05, 2019, 07:17:12 pm
Hey boy, you forget that my way is better !
:-)
Code: QB64: [Select]
  1.  
  2. SUB Foo
  3.   SHARED Bar
  4.   Bar ="baz"
  5.  

It's better to share only what it is useful and/or necessary!
:-)


TempodiBasic, do you mean this? (Because the line Bar = "baz" is an error in the sub.)
Code: QB64: [Select]
  1. PRINT "Bar is "; Bar
  2. Foo
  3. PRINT "Bar is "; Bar
  4.  
  5. SUB Foo
  6.     SHARED Bar AS STRING " 'add as string
  7.    Bar = "baz"
  8. END SUB
  9.  
  10.  
Title: Re: Local SUB/FUNC variable declarations
Post by: TempodiBasic on February 06, 2019, 08:11:26 am
Yes you're right
but when I am hangry this is the result!
:-)))
 
Code: QB64: [Select]
  1. DEFSTR A-T
  2. Bar = ""
  3.  
  4. SUB Foo
  5.     SHARED Bar
  6.     Bar = "baz"
  7.  
  8.  

So I have had or AS STRING in the SUB
or  DEFSTR A-T
Bar = "" in the main!
:-)))
Title: Re: Local SUB/FUNC variable declarations
Post by: SMcNeill on February 06, 2019, 09:25:31 am
Is it better to
Code: QB64: [Select]
  1. SUB Foo
  2.     bar$ = "baz"
  3.  
Or
Code: QB64: [Select]
  1. SUB Foo
  2.     DIM bar AS STRING
  3.     bar = "baz"
  4.  

My personal preference here tends to lie towards: “DIM memorable variables, don’t worry with temp variables”

SUB Foo
    DIM Velocity AS _FLOAT
    DIM Gravity AS _FLOAT
    DIM RacecarDriver AS STRING

    c$ = “PRESS <ANY KEY> TO CONTINUE.”
    e$ = “<ESC> TO END.”

    ..... more stuff
END SUB

Important variables name and help document themselves.  Temp/Trash variables are suffixed, quickly used, and forgotten.

It’s generally the basic guideline I follow, myself.
Title: Re: Local SUB/FUNC variable declarations
Post by: bobtheunplayer on February 06, 2019, 11:53:34 am
Thanks for all the feedback guys.  I think what I meant was more about what is the cost of static vs dynamic typing.  Which is more efficient?  Or, does it not even matter?

Thanks,
Bob