[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua 5.2 and continue
- From: Martin Poelstra <martin@...>
- Date: Mon, 01 Feb 2010 10:06:25 +0100
What's the problem with this version?
while true do
-- Read 10 bytes of data.
-- Last 9 bytes of previous run are re-used first if needed.
data = socket:read(10, data:sub(2))
if not data then
-- Serious error, break the loop
-- Check packet start code
if data:byte(1) == 0x47 then -- inverted condition
-- Parse raw bytes into a table with keys
pkt = convert(data)
-- More packet validation checking
if verifyHeader(pkt) then -- inverted condition
-- We used all data of this packet, don't re-use any of it
-- in the next run
data = ""
-- Do something with the packet
if pkt.cmd == CMD_EXIT then
elseif pkt.cmd == CMD_FOO then
The code becomes more indented, but in this specific case, it is
not a problem to me. To me this version is clearer than yours, but I
am sure this is a matter of taste :-)
Well, the problem is indeed that my actual function was a little longer,
with a few more checks. This rapidly becomes unreadable due to the large
levels of indentation.
(And yes, I know long functions could better be split into multiple
functions. In this case, doing so would harm readability, to my taste.)
Anyway, I think everyone agrees that it's possible to circumvent the use
of continue by using if's. It's just that some programming constructs
are more easily done using continue's, especially if one's accustomed to
its existance in other languages.
Note that including a 'disabled-by-default' continue (using a #define)
is probably worse than not including it at all. I use Lua's standalone
interpreter, using things like LuaRocks. If the use of continue would
depend on a non-standard define, this would effectively make it
impossible to use continue. (The same reason why I won't use a patch to
Lua to implement continue, as standard distributions don't include such