Page 2 of 2

Re: Good Variable Names

Posted: Tue Jun 10, 2014 4:25 pm
by demonnic
I'll sometimes go one further, even
Code: [show] | [select all] lua
 if some_variable then
  for k,v in pairs(t) do
     if type(v) == "string" then
       dostuff()
    end -- if type(v) == "string"
  end -- for pairs(t)
end -- if some_variable

Re: Good Variable Names

Posted: Tue Jun 10, 2014 4:33 pm
by Akaya
Well that will clear up any confusion! Hehe.

Re: Good Variable Names

Posted: Wed Jun 11, 2014 2:02 am
by Oneymus
Comments at the end of blocks can be handy, but a better solution is to avoid large blocks as much as possible. Break up large, potentially confusing blocks of code into smaller, easier to handle functions. This helps promote code reuse as well reducing unnecessary code complexity.

Often, if I can identify an easily encapsulated bit of logic, especially within if statements or other forms of nesting, I break it out into a separate function to keep the original function clean and easy to read, regardless of an immediate reuse need.

Re: Good Variable Names

Posted: Thu Jun 12, 2014 6:51 am
by Vadi
Yeah, to extend that, defining local functions within a large one is useful. For example, sometimes I'd have:
Code: [show] | [select all] lua
function does_one_logical_thing()
  local function do_task_a() end
  local function do_task_b() end
  local function do_task_c() end

  do_task_a()
  do_task_b()
  do_task_c()
end
It is much easier to grok then when all the logical pieces within it are 'labelled'.