Author Topic: Local SUB/FUNC variable declarations  (Read 4002 times)

0 Members and 1 Guest are viewing this topic.

Offline bobtheunplayer

  • Newbie
  • Posts: 7
    • View Profile
Local SUB/FUNC variable declarations
« 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.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Local SUB/FUNC variable declarations
« Reply #1 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.
« Last Edit: February 05, 2019, 10:56:04 am by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Local SUB/FUNC variable declarations
« Reply #2 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.  
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Local SUB/FUNC variable declarations
« Reply #3 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!
:-)
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Local SUB/FUNC variable declarations
« Reply #4 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.  
« Last Edit: February 05, 2019, 07:20:29 pm by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Local SUB/FUNC variable declarations
« Reply #5 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!
:-)))
Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Local SUB/FUNC variable declarations
« Reply #6 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bobtheunplayer

  • Newbie
  • Posts: 7
    • View Profile
Re: Local SUB/FUNC variable declarations
« Reply #7 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