一、編輯器配置
[*]
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
三萤皂、空格
在操作符的兩側(cè),增加一個空格
--No
local i=1
local s = "apisix"
--Yes
local i = 1
local s = "apisix"
四匣椰、空行
使用一個換行來分隔兩個不同的函數(shù)
--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")
六、變量
大多數(shù)情況下窝爪,應(yīng)該全部使用局部變量,不要使用全局變量
--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
優(yōu)化table的內(nèi)存申請:
--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
在數(shù)組中不要使用 nil
:
--No
local t = {1, 2, nil, 3}
如果你必須要使用null值, 請使用 ngx.null
來代替:
--Yes
local t = {1, 2, ngx.null, 3}
八蒲每、字符串
不要拼接字符串,而應(yīng)該使用數(shù)組來優(yōu)化執(zhí)行效率
--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, "")
九喻括、函數(shù)
用蛇形命名法(snake case)為函數(shù)起名字,即用下劃線將單詞連接起來
--No
local function testNginx()
end
--Yes
local function test_nginx()
end
函數(shù)應(yīng)該盡量早的返回執(zhí)行結(jié)果
--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
十一、錯誤處理
所有函數(shù)都必須返回錯誤信息唬血,錯誤信息可以判斷函數(shù)是否調(diào)用成功
--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