[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: optimization improves code readability
- From: Leo Razoumov <slonik.az@...>
- Date: Thu, 22 Jan 2009 07:37:20 -0500 (EST)
This message is about Lua advocacy.
It just crossed my mind that in most programming languages
optimizations make code _less_ readable. In Lua, on the other hand, some
optimizations make code *MORE* readable.
For examples, we all know that access to local variables is
substantially faster that access to globals or table hashes.
If I have to work with packet that contains a message represented by
an array of bytes an optimized way to use its content would be
something like this
local function parse(packet)
local header = packet.header
local msg_begin= packet.msg
local msg_type = packet.msg
local dev_id = packet.msg..packet.msg
local coorX = packet.msg
local coorY = packet.msg
Not only using local bindings to msg elements make the code run faster
(avoids repetitive packet.msg hash lookup) but, more importantly, such
code provides an explicit message format documentation. When the
message format changes, the code breaks. Fixing the broken code will
simultaneously update the documentation! How convenient.