给用torch的童鞋,lua代码规范
Posted xizero00
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了给用torch的童鞋,lua代码规范相关的知识,希望对你有一定的参考价值。
http://lua-users.org/wiki/LuaStyleGuide一、命名规范 (1)变量长度: 有较大作用范围的变量应该具有更好的描述性,而具有较小作用范围的变量可以使用i这种命名。
(2)值和对象变量的命名: 通常是小写,比较短的 关于布尔型,则使用is_home这种来表示,加个is_xxxx (3)函数命名: 与值和对象的命名类似,有时候你可以使用下划线比如print_table (4)lua内部变量命名: 使用下划线_XXX这种类似的明明方式,而且是大写 (5)常量命名: 使用大写,可以使用下划线分隔开 MAX_LINE或者MAXLINE
(6)模块以及包的命名: 使用短名词,比如:luasql,luasocket等 M这个字母通常表示当前模块表(current moudle table)
(7)类名: 使用字母开头大写 比如:XmlDocument
二、作用范围
无论如何最好使用局部变量而不是全局变量 如何检查未定义的变量: http://lua-users.org/wiki/DetectingUndefinedVariables
使用 do xxx end 这样可以限制局部变量的作用范围
全局变量的范围也是可以通过lua的模块系统去削减的
三、模块 -- hello/mytest.lua module(..., package.seeall) local function test() print(123) end function test1() test() end function test2() test1(); test1() end
使用的时候
and use it like this:
require "hello.mytest"hello.mytest.test2()
但是这样写更好
These problems can be avoided by not using the module
function but instead simply defining modules in the following simple way:
local function test() print(123) end function M.test1() test() end function M.test2() M.test1(); M.test1() end
return M
and importing modules this way:
local MT = require "hello.mytest"MT.test2()
带有构造函数的类
A module containing a class with constructor (in the object-oriented sense) can be packaged in a number of ways in a module. Here is one reasonably good approach.[*2]
-- file: finance/BankAccount.lualocal M = ; M.__index = M
local function construct()
local self = setmetatable(balance = 0, M)
return self end setmetatable(M, __call = construct)
function M:add(value) self.balance = self.balance + 1 end
return M
A module defined in this way typically only contains a single class (or at least a single public one), which is the module itself.
It can be used like this:
local BankAccount = require "finance.BankAccount" local account = BankAccount()or even like this:
local new = require local account = new "finance.BankAccount" ()四、注释 在--后面加上空格
使用doxygen或者javadoc的注释方式
:
-- taken from cgilua/src/cgilua/session.lua ------------------------------------- -- Deletes a session. -- @param id Session identification. ------------------------------------- function delete (id) assert (check_id (id))remove (filename (id))
end
在某个函数终止的后面加上注释
Because "end
" is a terminator for many different constructs, it can help the reader (especially in a large block) if a comment is used to clarify which construct is being terminated: [*3]
if type(v) == "string" then
...lots of code here...
end -- if string
end -- for each t
五、lua的一些idioms
(1)测试某个变量是不是nil,直接用if var而不是使用if var == nil local line = io.read()
if line then -- instead of line ~= nil
...
end
...
if not line then -- instead of line == nil
...
end
(2)如果测试的变量包含false,那么就需要显式地区分false和nil
However, if the variable tested can ever contain false
as well, then you will need to be explicit if the two conditions must be differentiated: line == nil
v.s. line == false
.
and
and or
may be used for terser code:
x = x or "idunno"
-- rather than if x == false or x == nil then x = "idunno" end
print(x == "yes" and "YES!" or x)
-- rather than if x == "yes" then print("YES!") else print(x) end end
(3)复制一个表
Clone a small table t
(warning: this has a system dependent limit on table size; it was just over 2000 on one system):
(4)判断某个表是否为空
Determine if a table t
is empty (including non-integer keys, which #t
ignores):
(5)数组append一个值,用
t[#t+1] = 1更快速
To append to an array, it can be terser and more efficient to do
t[#t+1] = 1
rather than
table.insert(t, 1)
.
六、lua的面向对象 http://lua-users.org/wiki/ObjectOrientationTutorial
闲来无事,就把以前翻译的规范发上来了。 转载请注明出处:http://blog.csdn.net/xizero00
以上是关于给用torch的童鞋,lua代码规范的主要内容,如果未能解决你的问题,请参考以下文章
这个 C 代码(来自 lua 库,Torch)是如何编译/工作的?