When to use global variables vs using method RETURN variables
I am slowly working my way into the SPIN language and was wondering if there is any accepted good programming practice of when to use a method RETURN variable vs just declaring the variable as a global.
I was writing a simple object to monitor battery voltage (prop system supply) using a RC circuit and RC time counter. It became apparent that this simple object (as practically any programming task) could be written either way:
Using Global Vars:
Using Return Values:
I think the global var procedure is a little easier to read and understand, but I am wondering if there is a better chance of problems due to the fact that any method could conceivably access the vars meant for use by the voltage measuring object? Less global variables the better?
Any opinions/ anecdotal good/bad knowledge/stories?
I was writing a simple object to monitor battery voltage (prop system supply) using a RC circuit and RC time counter. It became apparent that this simple object (as practically any programming task) could be written either way:
Using Global Vars:
CON
_clkmode = xtal1 '+ pll16x 'Using no PLL so I can use damaged PPB
_xinfreq = 5_000_000
'PIN CONNECTIONS
debugLED = 0 'FOR TESTING
Vdd = 14 'voltage to monitor
'calculated constants for calculating Vx
c1 = 26615
c2 = 18
VAR
long rct
long Vx
OBJ
PST: "Parallax Serial Terminal" 'FOR TESTING
PUB MAIN
'init
PST.start(9600) 'TEST
'dira[debugLED] := 1 'TEST
repeat
getVoltage(vdd)
PST.dec(Vx)
PST.newline
waitcnt(clkfreq + cnt)
!outa[debugLED]
PUB getVoltage(inPin)
getRCT(inPin)
Vx := c1/rct*10+(c1//rct*10/rct) + (10 * c2)
PUB getRCT(inPin)
'init
outa[inPin] := 0 'low
dira[inPin] := 1 'output
rct := 0 'zero counter
waitcnt(clkfreq + cnt) 'pause 1 sec to drain cap
dira[inPin] := 0 'input
repeat until ina[inPin] <> 0
rct++
Using Return Values:
CON
_clkmode = xtal1 '+ pll16x 'Using no PLL so I can use damaged PPB
_xinfreq = 5_000_000
'PIN CONNECTIONS
debugLED = 0 'FOR TESTING
Vdd = 14 'voltage to monitor
'calculated constants for calculating Vx
c1 = 26615
c2 = 18
OBJ
PST: "Parallax Serial Terminal" 'FOR TESTING
PUB MAIN
'init
PST.start(9600) 'TEST
'dira[debugLED] := 1 'TEST
repeat
PST.dec(getVoltage(vdd))
PST.newline
waitcnt(clkfreq + cnt)
'!outa[debugLED]
PUB getVoltage(inPin) : Vx | rct
rct := getRCT(inPin)
Vx := c1/rct*10+(c1//rct*10/rct) + (10 * c2)
PUB getRCT(inPin) : rct
'init
outa[inPin] := 0 'low
dira[inPin] := 1 'output
rct := 0 'zero counter
waitcnt(clkfreq + cnt) 'pause 1 sec to drain cap
dira[inPin] := 0 'input
repeat until ina[inPin] <> 0
rct++
I think the global var procedure is a little easier to read and understand, but I am wondering if there is a better chance of problems due to the fact that any method could conceivably access the vars meant for use by the voltage measuring object? Less global variables the better?
Any opinions/ anecdotal good/bad knowledge/stories?
Comments
I just realized that global vars are not accessible from other objects, so I think the RETURN variable is the way to go as to exploit the power of spin (ie build lots of useful objects that can be easily thrown together to make a quick solution to the task at hand....)