[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua 5.2 and continue
- From: Martin Poelstra <martin@...>
- Date: Sun, 31 Jan 2010 22:21:07 +0100
> <snip>
>
> Inclusion by "popular request" is not the same as inclusion by "me
> too" count threshold.
>
> I believe that providing good arguments for your position has much
> better potential.
You're right, an example would probably be better, so here goes.
I wrote a packet parser for a home-automation system. The system will
receive quite some (wireless) noise, thus retrying to match a packet
header on the next few bytes must be possible.
In my current implementation, I have 2 nested loops, and have to use the
'break' statement in places where my brain thinks 'continue'. I would
have written the function like the following, which better fits the
flow-of-thoughts in my brain:
-- pseudo-code...
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
break
end
-- Check packet start code
if data:byte(1) ~= 0x47 then
continue
end
-- Parse raw bytes into a table with keys
pkt = convert(data)
-- More packet validation checking
if not verifyHeader(pkt) then
continue
end
-- 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
break
elseif pkt.cmd == CMD_FOO then
foo()
end
end
On the other hand, I do appreciate less is more for Lua...
Grtz!
Martin
- References:
- Lua 5.2 and continue, Joshua Jensen
- Re: Lua 5.2 and continue, Natanael Copa
- Re: Lua 5.2 and continue, steve donovan
- Re: Lua 5.2 and continue, Klaus Ripke
- Re: Lua 5.2 and continue, Martin Poelstra
- Re: Lua 5.2 and continue, Fernando P. García
- Re: Lua 5.2 and continue, Olivier Hamel
- Re: Lua 5.2 and continue, Alexander Gladysh
- Re: Lua 5.2 and continue, Martin Poelstra
- Re: Lua 5.2 and continue, Alexander Gladysh