This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
lua_conventions:zh [2023/10/15 08:28] hfsr [Lua 代码约定] |
lua_conventions:zh [2023/11/27 06:10] (current) hfsr [Lua 代码规范] |
||
---|---|---|---|
Line 9: | Line 9: | ||
* 最好按字母顺序排列require(强迫症!) | * 最好按字母顺序排列require(强迫症!) | ||
* 编写注释时请放宽心。文件加载完成后由于大量使用注释占用的额外内存会很快释放。 | * 编写注释时请放宽心。文件加载完成后由于大量使用注释占用的额外内存会很快释放。 | ||
- | * 如果你需要验证参数,请使用内建的 checkArg 函数。尽量产生同类的报错信息是好事!其使用方法为 `checkArg(n, value, type1,...)`,n 是参数的编号,value 是参数的值,type1 和后面的内容为允许的变量类型,变量类型可通过 `type(value)` 获得。报错信息中,前文的编号以如下方式出现:“bad argument #n (type1 expected, got type(value))”。例如,若要求第一个参数为数字,你可以写 `checkArg(1, arg, "number")`。 | + | * 如果你需要验证参数,请使用内建的checkArg函数。尽量产生同类报错信息是好事!其用法为`checkArg(n, value, type1,...)`,n是参数的编号,value是参数的值,type1和后面的内容为允许的变量类型,变量类型可通过`type(value)` 获得。报错信息中,前文的编号以如下方式出现:“bad argument #n (type1 expected, got type(value))”。例如,若要求第一个参数为数字,你可以写 `checkArg(1, arg, "number")`。 |
不好的代码: | 不好的代码: | ||
Line 17: | Line 17: | ||
if sArg1 then | if sArg1 then | ||
local nResult = 1 | local nResult = 1 | ||
- | -- 做更多工作 | + | -- 做更多任务 |
return nResult | return nResult | ||
end | end | ||
Line 32: | Line 32: | ||
if name then | if name then | ||
local result = 1 | local result = 1 | ||
- | -- 我们将非欧利几得空间的 B-样条算法外推来得出潜在的失败概率 | + | -- 我们将非欧利几得空间的B-样条算法外推来得出潜在的失败概率 |
return result | return result | ||
end | end |