Good Variable Names

A category for whatever! Can be Mudlet-related or offtopic.
User avatar
demonnic
Posts: 884
Joined: Sat Dec 05, 2009 3:19 pm

Re: Good Variable Names

Post 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

User avatar
Akaya
Posts: 414
Joined: Thu Apr 19, 2012 1:36 am

Re: Good Variable Names

Post by Akaya »

Well that will clear up any confusion! Hehe.

User avatar
Oneymus
Posts: 321
Joined: Thu Sep 17, 2009 5:24 am

Re: Good Variable Names

Post 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.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Good Variable Names

Post 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'.

Post Reply