给用torch的童鞋,lua代码规范


一、命名规范
(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:

-- hello/mytest.lua local M = {}

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.lua

local 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]

  for i,v  in  ipairs(t)  do
    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:

local  function test(x)
  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):

u = { unpack(t)}

(4)判断某个表是否为空

Determine if a table t is empty (including non-integer keys, which #t ignores):

if  next(t) ==  nil  then ...


(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://blog.csdn.net/xizero00



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值