Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should parameters be used as variables in Lua?

Tags:

lua

convention

I've been told in Java that I should avoid modifying the original parameters such as

public int doStuff(int begin, int end) {
  /* loop or something */
  begin++; //bad
  end--; //also bad
  /* end loop */
  return
}

instead, I should do something like

public int doStuff(int begin, int end) {
  int myBegin = begin; //something like this
  int myEnd = end;
  /* stuff */
  return
}

So, I've been doing this in lua

function do_stuff(begin, last)
  local my_begin = begin
  local my_last = last
  --stuff
  my_begin = my_begin + 1
  my_last = my_last - 1
  --stuff
end

But, I'm wondering if

function do_stuff(begin, last)
  --stuff
  begin = begin + 1
  last = last - 1
  --stuff
end

is also discouraged, or is it nice and concise?

like image 331
Blubber Avatar asked May 31 '26 19:05

Blubber


1 Answers

There are no rules. Let taste, clarity, and need decide.

Nevetheless, a common idiom is to provide default values for parameters as in

function log(x,b)
   b = b or 10
   ...
end
like image 94
lhf Avatar answered Jun 02 '26 19:06

lhf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!