Jump to content




[Answered] Global variables vs function parameters


5 replies to this topic

#1 Ulthean

  • Members
  • 171 posts

Posted 17 December 2012 - 10:44 PM

In my code I tried to pass all variables as function parameters rather than relying on global variables, since I feel this is cleaner.
But the more variables I add the more I regret doing this :) Right now I have functions like the next one:

function excavateAllShafts(num, interval, offset, radius, components, minFuel, fuelSuckDir, fuelDropDir, itemDropDir)
  local torchSlot=0
  if placeTorches then
	torchSlot=1
  end
  if tunSkip<4 then
	for i=1, tunSkip do
	  moveToNextTunnel(radius, fuelSuckDir, fuelDropDir, components+1+torchSlot)
	end
	excavateShafts(num-shSkip, interval, (offset+(shSkip*(interval+1))), radius, components, minFuel, fuelSuckDir, fuelDropDir, itemDropDir)
	moveToNextTunnel(radius, fuelSuckDir, fuelDropDir, components+1+torchSlot)
	for i=(tunSkip+2),4 do
	  excavateShafts(num, interval, offset, radius, components, minFuel, fuelSuckDir, fuelDropDir, itemDropDir)
	  moveToNextTunnel(radius, fuelSuckDir, fuelDropDir, components+1+torchSlot)
	end
  end
end

My question is: What do you think is the best option for me?
  • Individual function parameters
  • Global variables
  • grouped function parameters (example: configuration[1] = fuelSuckDir, configuration[2] = fuelDropDir, ... and passing configuration to all functions)


#2 Helfire

  • New Members
  • 8 posts

Posted 17 December 2012 - 10:56 PM

Why not use tables as parameters then? It's even cleaner and less of a chance you'll forget to pass a value to your function.
ie:

ExcavateVars = {}
function excavateAllShafts(ExcavateVars)

then access the parameters via:
ExcavateVars["num"] or ExcavateVars["interval"], or even ExcavateVars.radius ?

This way you don't need to number them 1,2,3... and you don't need to remember what configuration[5] was all about.


#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 December 2012 - 11:17 PM

for a general convention, in all my programming subjects at uni for Professional Software Development, told us to avoid using global variables if we can pass-by-reference to functions, which I believe Java does by default. although sometimes you need globals :)

#4 ChunLing

  • Members
  • 2,027 posts

Posted 17 December 2012 - 11:31 PM

I vote option three. Pass a table reference.

#5 CoolisTheName007

  • Members
  • 304 posts

Posted 17 December 2012 - 11:52 PM

Depending on the situation, e.g. a lot of functions may change their behavior depending on some variables, you may want to use locals?
local myvar1,myvar2,...
--private function,for exclusive use by the program
local function c(...)
if myvar1 then...
end
--public function, will still appear in the shell/API table
b=function ()
  if myvar2...
end
a=function setvar1to2(var1,var2)
  myvar1,myvar2=var1,var2
end

An improvement for using a table is to automate it with getfenv() ( I wrote sme function for it, I'll link t it later) (and don't forget that you can call functions without parentheses if the arguments are only strings or tables); known issues is that you can't override some default to be nil, and that all variables loaded stay inside the function not as locals, but as part of it's environment. So I wouldn't advise using them for extremely demanding computations. For turtle commands, that's usually not a problem.
defaults={var1=true,var3=2}
d=function(targs)
  nargs(targs,defaults)
  if var1 then...
end

--use
d{var1=false,var2=789,var3=nil}--var3 will be 2


#6 Ulthean

  • Members
  • 171 posts

Posted 18 December 2012 - 04:59 AM

Thanks for the tips everybody!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users