lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, 13 Jun 2014, Roberto Ierusalimschy wrote:

I guess I do not understand what you mean by your above comments, sorry. :(

Bad code:

 if x == 43 then ...
 elseif x == 67 then ...
 ...
 end


Good code:

 -- This is somewhere in your code (probably near the top)
 -- Code for Events
 MOUSECLICK = 43
 KEYPRESS = 67
 ...

 -- this is somewhere else
 if x == MOUSECLICK then ...
 elseif x == KEYPRESS then ...
 ...
 end

(If everything is in the same module, you can declare those names as
locals.)
	I don't need switch:

local event_handler = {
	MOUSECLICK = function (...) ... end,
	KEYPRESS = function (...) ... end,
	...
}
...
local callback = event_handler[x]
if callback then
  callback(...)
end

	Don't you use that kind of construction?

	Regards,
		Tomás