首页 > 学院 > 开发设计 > 正文

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

2019-11-06 08:16:54
字体:
来源:转载
供稿:网友
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     xxxend这样可以限制局部变量的作用范围全局变量的范围也是可以通过lua的模块系统去削减的三、模块-- hello/mytest.luamodule(..., package.seeall)local function test() print(123) endfunction test1() test() endfunction 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.lualocal M = {}local function test() print(123) endfunction M.test1() test() endfunction M.test2() M.test1(); M.test1() endreturn 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 = Mlocal function construct()  local self = setmetatable({balance = 0}, M)  return selfendsetmetatable(M, {__call = construct})function M:add(value) self.balance = self.balance + 1 endreturn 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 = requirelocal 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 == nillocal 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) endend(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://lua-users.org/wiki/ObjectOrientationTutorial闲来无事,就把以前翻译的规范发上来了。转载请注明出处:http://blog.csdn.net/xizero00
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表