一、编辑器配置
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
[*.lua]
indent_style = space
indent_size = 2
[kong/templates/nginx*]
indent_style = space
indent_size = 4
[*.template]
indent_style = space
indent_size = 4
[Makefile]
indent_style = tab
二、缩进
缩进使用两个空格(虽然Lua语言没有这种语法要求)
--No
if a then
ngx.say("hello")
end
--yes
if a then
ngx.say("hello")
end
三、空格
在操作符的两侧,增加一个空格
--No
local i=1
local s = "apisix"
--Yes
local i = 1
local s = "apisix"
四、空行
使用一个换行来分隔两个不同的函数
--No
local function foo()
end
local function bar()
end
--Yes
local function foo()
end
local function bar()
end
五、每行最大长度
每行代码不超过80个字符
--No
return limit_conn_new("plugin-limit-conn", conf.conn, conf.burst, conf.default_conn_delay)
--Yes
return limit_conn_new("plugin-limit-conn", conf.conn, conf.burst,
conf.default_conn_delay)
连接符放在下一行
--No
return limit_conn_new("plugin-limit-conn" .. "plugin-limit-conn" ..
"plugin-limit-conn")
--Yes
return limit_conn_new("plugin-limit-conn" .. "plugin-limit-conn"
.. "plugin-limit-conn")
六、变量
大多数情况下,应该全部使用局部变量,不要使用全局变量
--No
i = 1
s = "kong"
--Yes
local i = 1
local s = "kong"
用蛇形命名法(snake case)为变量起名字,即用下划线将单词连接起来
--No
local IndexArr = 1
local str_Name = "kong"
--Yes
local index_arr = 1
local str_name = "kong"
所有常量使用大写字母
--No
local max_int = 65535
local http_ok = 200
--Yes
local MAX_INT = 65535
local HTTP_OK = 200
七、Table
使用 table.new
优化table的内存申请:
--No
local t = {}
for i = 1, 100 do
t[i] = i
end
--Yes
local new_tab = require "table.new"
local t = new_tab(100, 0)
for i = 1, 100 do
t[i] = i
end
在数组中不要使用 nil
:
--No
local t = {1, 2, nil, 3}
如果你必须要使用null值, 请使用 ngx.null
来代替:
--Yes
local t = {1, 2, ngx.null, 3}
八、字符串
不要拼接字符串,而应该使用数组来优化执行效率
--No
local s = ""
for i = 1, 100000 do
s = s .. "a"
end
--Yes
local t = {}
for i = 1, 100000 do
t[i] = "a"
end
local s = table.concat(t, "")
九、函数
用蛇形命名法(snake case)为函数起名字,即用下划线将单词连接起来
--No
local function testNginx()
end
--Yes
local function test_nginx()
end
函数应该尽量早的返回执行结果
--No
local function check(age, name)
local ret = true
if age < 20 then
ret = false
end
if name == "a" then
ret = false
end
-- do something else
return ret
end
--Yes
local function check(age, name)
if age < 20 then
return false
end
if name == "a" then
return false
end
-- do something else
return true
end
十、模块
所有的外部模块必须使用局部变量缓存
--No
local function foo()
local ok, err = ngx.timer.at(delay, handler)
end
--Yes
local timer_at = ngx.timer.at
local function foo()
local ok, err = timer_at(delay, handler)
end
十一、错误处理
所有函数都必须返回错误信息,错误信息可以判断函数是否调用成功
--No
local sock = ngx.socket.tcp()
local ok = sock:connect("www.google.com", 80)
ngx.say("successfully connected to google!")
--Yes
local sock = ngx.socket.tcp()
local ok, err = sock:connect("www.google.com", 80)
if not ok then
ngx.say("failed to connect to google: ", err)
return
end
ngx.say("successfully connected to google!")
错误信息作为返回值的第二部分返回,并且要包含详细错误信息
--No
local function foo()
local ok, err = func()
if not ok then
return false
end
return true
end
--No
local function foo()
local ok, err = func()
if not ok then
return false, {msg = err}
end
return true
end
--Yes
local function foo()
local ok, err = func()
if not ok then
return false, "failed to call func(): " .. err
end
return true
end
网友评论